• Import the JAR package before using it

    • Mysql8 import jar package of 8
    • Mysql5 import jar package of 5
  • Configuration file location, SRC root directory of the project:

    Such as:

  • Configuration file: jdbc.properties
#mysql8
url=jdbc:mysql://localhost:3306/test? serverTimezone=GMT%2B8
#mysql5
url=jdbc:mysql://localhost:3306/test
# values can also be used without single quotes
Database connection user name
user='root'
Database user password
password=root
#mysql8
driver=com.mysql.cj.jdbc.Driver
#mysql5
#driver=com.mysql.jdbc.Driver
Copy the code
  • JDBC utility class: jdbcutils.java
package com.jdbc.util;

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

/ / JDBC tools
public class JDBCUtils {
    // Define the connection database properties
    private static String url = null;
    private static String user = null;
    private static String password = null;
    private static String driver = null;

    // Read configuration file, static, only need one time to get the connection required properties
    static{
        try {
            // Use the classloader to get the current classpath
            // Get the ClassLoader object
            ClassLoader classLoader = JDBCUtils.class.getClassLoader();
            // Find the resource with the specified name through the class loader's getResource()
            URL resource = classLoader.getResource("jdbc.properties");
            //System.out.println(resource); //file:/D:/java_work/demo4_project/out/production/demo2_jdbc/jdbc.properties
            // Use getPath() to get the path part of the URL
            String path = resource.getPath();
            Println ("path: "+ path); // system.out. println("path:" + path); ///D:/java_work/demo4_project/out/production/demo2_jdbc/jdbc.properties
            // Load the configuration file
            // Create an object
            Properties pro = new Properties();
            // Load the configuration file key-value pair data, using load() to read the property list from the input stream or character stream
            pro.load(new FileReader(path));
            // Get the property value based on the key, using getProperty()
            url = pro.getProperty("url");
            user = pro.getProperty("user");
            password = pro.getProperty("password");
            driver = pro.getProperty("driver");
            // Register the driver and the class loading will take place
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch(ClassNotFoundException e) { e.printStackTrace(); }}// Define the method to get the database Connection object Connection
    public static Connection getConnection(a) throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }

    // Release resources
    public static void close(ResultSet rs, Statement stat, Connection conn){
        if(rs ! =null) {try {
                rs.close();
            } catch(SQLException throwables) { throwables.printStackTrace(); }}if(stat ! =null) {try {
                stat.close();
            } catch(SQLException throwables) { throwables.printStackTrace(); }}if(conn ! =null) {try {
                conn.close();
            } catch(SQLException throwables) { throwables.printStackTrace(); }}}public static void close(Statement stat, Connection conn){
        if(stat ! =null) {try {
                stat.close();
            } catch(SQLException throwables) { throwables.printStackTrace(); }}if(conn ! =null) {try {
                conn.close();
            } catch(SQLException throwables) { throwables.printStackTrace(); }}}}Copy the code