JDBC登录案例

创建db4数据库,创建user表,添加数据

 

 JDBCUtiles工具类

public class JDBCUtils {
    private static String url;
    private static String user;
    private static String password;
    private static String driver;
    /**
     * 文件的读取,只需要读取一次即可拿到这些值,使用静态代码块
     */
    static{
        //读取资源文件,获取值

        try {
            //1.创建Properties集合
            Properties properties = new Properties();
            //获取src路径下的文件的方式---》ClassLoader 类加载器
            ClassLoader classLoader = JDBCUtils.class.getClassLoader();
            URL resource = classLoader.getResource("jdbc.properties");
            String path = resource.getPath();
            //System.out.println(path);
            //2.加载文件
            properties.load(new FileReader(path));
            //3.获取数据,赋值
            url=properties.getProperty("url");
            user=properties.getProperty("user");
            password=properties.getProperty("password");
            driver=properties.getProperty("driver");
            //4.注册驱动
            Class.forName(driver);

        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 获取连接
     * @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 e) {
                throw new RuntimeException(e);
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 释放资源
     * @param stmt
     * @param conn
     */
    public static void close(ResultSet re, Statement stmt, Connection conn){
        if (re!=null){
            try {
                re.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

 

JDBCDemo9类

ublic class JDBCDemo9 {
    public static void main(String[] args) throws SQLException {
        //1.键盘录入,接受用户和密码
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username=sc.nextLine();
        System.out.println("请输入密码:");
        String password=sc.nextLine();

        //2.调用方法,非静态方法new一个对象
        boolean flag = new JDBCDemo9().login(username, password);

        //3.判断结果,输出不同语句
        if (flag){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }
    }
    /*写一个登陆方法,有参数用户和密码,返回是否登陆成功*/
    public boolean login(String username, String password) throws SQLException {
        //判断录入的密码和用户名是否为空
        if (username == null || password == null) {
            return false;
        }
        //连接数据库判断是否登陆成功
        Connection conn = null;
        Statement stmt=null;
        ResultSet rs=null;

        //1.获取链接

        try {
            conn = JDBCUtils.getConnection();
            //2.定义sql语句
            String sql="select * from user where username='"+username+"' and password='"+password+"'";
            //3.获取执行sql的对象
            stmt = conn.createStatement();
            //4.执行查询
            rs = stmt.executeQuery(sql);
            //5.判断结果集是不是有数据
            return rs.next(); //返回为布尔型数据,如果有下一行就返回true
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //6.释放资源
            JDBCUtils.close(rs,stmt,conn);
        }
        return false;
    }

 

把配置文件的数据库改为db4

 

 

 

posted @ 2022-10-17 10:19  漁夫  阅读(29)  评论(0)    收藏  举报