ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

登录案例

2022-08-01 09:34:47  阅读:126  来源: 互联网

标签:null String 登录 stmt 案例 SQLException password conn


练习:登录案例

需求:
1.通过键盘录入用户名和密码
2.判断用户是否登录成功

select * from user where username = "" and password = ""

如果这个sql有查询结果,则成功,反之,则失败

步骤:
1.创建数据库表 user

create table user(
id int primary key auto_increment,
username varchar(32) not null,
password varchar(32) not null
);

insert into user values(null,"root","root");

select * from user;

工具类:

package cn.lhy.Utils;

import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

/**
 * JDBC工具类
 */
public class JDBCUtils {
    private static String url;
    private static String user;
    private static String password;
    private static String driver;
    /**
     * 文件的读取,只需要读取一次即可拿到这些值,使用静态代码块
     */
    static {
        try {
            //读取资源文件,获取值
            //1.创建Properties对象
            Properties pro = new Properties();

            //获取src路径下的文件方式--->ClassLoader 类加载器
            ClassLoader classLoader = JDBCUtils.class.getClassLoader();
            URL res = classLoader.getResource("jdbc.properties");
            String path = res.getPath();
            //2.加载文件
            pro.load(new FileReader(path));
            //3.获取数据,赋值
            url = pro.getProperty("url");
            user = pro.getProperty("user");
            password = pro.getProperty("password");
            driver = pro.getProperty("driver");
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取连接
     * @return
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }
    /**
     * 释放资源
     * @param stmt
     * @param conn
     */
    public static void close(Statement stmt,Connection conn){
        if (stmt!=null){
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    /**
     * 释放资源
     * @param stmt
     * @param conn
     */
    public static void close(ResultSet rs, Statement stmt, Connection conn){
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (stmt!=null){
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
package cn.lhy.jdbc;

import cn.lhy.Utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 需求:
 * 1.通过键盘录入用户名和密码
 * 2.判断用户是否登录成功
 */
public class Test {

    public static void main(String[] args) throws SQLException {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入账号和密码");
        //键盘录入接受用户名和密码
        String username = in.next();
        String password = in.next();
        //调用方法
        boolean login = login(username, password);
        //判断结果返回不同语句
        if (login) {
            System.out.println("登陆成功");
        } else {
            System.out.println("登录失败");
        }

    }

    /**
     * 登录方法
     */
    public static boolean login(String username, String password) {
        if (username == null || password == null || username == "" || password == "") {
            return false;
        }

        //连接数据库判断是否登陆成功
        Connection conn = null;
        try {
            conn = JDBCUtils.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //定义sql语句
        String sql = "select * from user where username = '" + username + "' and password = '" + password + "' ";
        //获取执行sql语句的对象
        Statement stmt = null;
        try {
            stmt = conn.createStatement();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //执行语句
        ResultSet rs = null;
        try {
            rs = stmt.executeQuery(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //判断
        /*if (rs.next()) {//如果有下一行则返回true即登陆成功
            return true;
        }else{
            return false;
        }*/

        try {
            return rs.next();//如果有下一行则返回true即登陆成功
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtils.close(rs,stmt,conn);
        }
    }
}

标签:null,String,登录,stmt,案例,SQLException,password,conn
来源: https://www.cnblogs.com/ailhy/p/16538182.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有