通过构造jdbc.properties和导入mysql-connector-java-5.1.37-bin.jar包来连接数据库

Java程序
DBU1.java

在这里插入代码片
```import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

public class DBU1 {

    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    static {
        Properties pro=new Properties();
        try {
            //创建类加载器
            ClassLoader classLoader=DBU1.class.getClassLoader();
            URL res=classLoader.getResource("jdbc.properties");
            String path=res.getPath();
            System.out.println(path);
            pro.load(new FileReader(path));

            //pro.load(new FileReader("src/db.properties"));
            driver=pro.getProperty("driver");
            url=pro.getProperty("url");
            username=pro.getProperty("user");
            password=pro.getProperty("password");
            System.out.println(url+"   "+driver+"  "+username+"  "+password);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public  static  Connection getConn() throws SQLException {
        return DriverManager.getConnection(url,username,password);

    }
    public  static void close(ResultSet rs,Statement stat,Connection conn){
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stat!=null){
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
    public static void close(Connection conn, Statement stat) {
        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

}

jdbc.properties文件

在这里插入代码片

driver:com.mysql.jdbc.Driver
url:jdbc:mysql://localhost:3306/mydb1
user:root
password:123456

``Cs.java`
在这里插入代码片

import java.sql.*;
import java.util.Scanner;

public class Cs {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();
        boolean flag = login(username, password);
        if (flag == true) {
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败");
        }
    }

    private static boolean login(String username, String password) {
        PreparedStatement pst;
        ResultSet rs;
        try {
            Connection conn=DBU1.getConn();
            System.out.println("连接成功");
            String sql="select * from user where username=?and password=?";
           pst=conn.prepareStatement(sql);
           pst.setString(1,username);
           pst.setString(2,password);
           rs=pst.executeQuery();
          return   rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
            return false;
    }

}

mysql-connector-java-5.1.37-bin.jar包和dbc.properties,class文件放在同一目录下
MySQL数据库
通过构造jdbc.properties和导入mysql-connector-java-5.1.37-bin.jar包来连接数据库_第1张图片
测试结果截图
失败
通过构造jdbc.properties和导入mysql-connector-java-5.1.37-bin.jar包来连接数据库_第2张图片
成功
通过构造jdbc.properties和导入mysql-connector-java-5.1.37-bin.jar包来连接数据库_第3张图片

你可能感兴趣的:(通过构造jdbc.properties和导入mysql-connector-java-5.1.37-bin.jar包来连接数据库)