JDBC: JDBC tool class JDBCUtils

JDBC tool class: JDBCUtils

1. Create a file in the src directory: jdbc.properties

url=jdbc:mysql://localhost/stu_db  ---数据库的url地址
user=root                          ---数据库的user名
pwd=123456						---数据库的密码
driver=com.mysql.jdbc.Driver		  ---加载JDBC的驱动

2. Create a util package, and create a JDBCUtils.java file under the util package as the tool class of JDCB

3. In the JDBCUtils tool class, two methods are encapsulated: getConnection(), close(Statement stat, Connection conn, ResultSet rs).

  • getConnection() method: get JDBC connection, load JDBC driver
public static Connection getConnection() throws SQLException {
    return DriverManager.getConnection(url, user, pwd);
}
  • close (Statement stat, Connection conn, ResultSet rs) method: release resources
public static void close(Statement stat, Connection conn, ResultSet rs) {
    if (stat != null) {
        try {
            stat.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if (conn != null) {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if (rs != null) {
        try {
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

4. Write a static code block to automatically obtain the configuration information in the jdbc.properties file

  • Create the Properties collection class
//1.创建Properties集合类
Properties pro = new Properties();
  • Load file
pro.load(new FileReader(path));
  • Get object assignment
url = pro.getProperty("url");
user = pro.getProperty("user");
pwd = pro.getProperty("pwd");
driver = pro.getProperty("driver");
  • Register driver
Class.forName(driver);

JDBCUtils call:

  • Get connection object
conn = JDBCUtils.getConnection();
  • Release resources
JDBCUtils.close(statement,conn,resultSet); 

Complete code:

public class JDBCUtils {
    private static String url;
    private static String user;
    private static String pwd;
    private static String driver;

    static {

        try {
            //1.创建Properties集合类
            Properties pro = new Properties();
            //获取src目录下文件的方式 ClassLoader 类加载器
            ClassLoader classLoader = JDBCUtils.class.getClassLoader();
            URL resource = classLoader.getResource("jdbc.properties");
            String path = resource.getPath();
            System.out.println(path);
            //2.加载文件
            pro.load(new FileReader(path));
            //3.获取对象赋值
           url = pro.getProperty("url");
           user = pro.getProperty("user");
           pwd = pro.getProperty("pwd");
           driver = pro.getProperty("driver");
//            4.注册驱动
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }
/*
* 获取链接对象
* */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, pwd);
    }

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

Guess you like

Origin blog.csdn.net/weixin_45088667/article/details/108897779