8.0+版本mysql-connector-java的驱动注册和url设置以及JDBCUtils工具类抽取

之前版本的驱动加载是:
Class.forName("com.mysql.jdbc.Driver");
8.0+之后是:
Class.forName("com.mysql.cj.jdbc.Driver"); //5之后驱动版本可以省略驱动注册环节

DriverManager.getConnection(url,user,password)参数中的url之前版本是:
url="jdbc:mysql://localhost:3306/db1"
而现在是:
url="jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8"

URL基本格式是这样的:
连接地址+ssl连接关闭+字符集为utf-8+时区设置

否则按之前版本的驱动设置会报错:
Loading class `com.mysql.jdbc.Driver’. This is deprecated.

参考文章地址:https://blog.csdn.net/qq_41943867/article/details/90574135
建议抽取JDBCUtils类来注册驱动、关闭资源和创建Connection对象。
这里提供一个自己的jdbcutills工具类源码:
注意:利用类加载器加载了一个jdbc.properties的配置文件的路径,用来动态加载DriverManager.getConnection(url,user,password)的这三个参数,properties文件内容:

url=jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8

user=root

password=root

driver=com.mysql.cj.jdbc.Driver

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 {
        Properties properties = new Properties();
        ClassLoader classLoader=JDBCUtils.class.getClassLoader();
        URL res =classLoader.getResource("jdbc.properties");
        String path=res.getPath();
        System.out.println(path);
        properties.load(new FileReader(path));

             url =properties.getProperty("url");
             user =properties.getProperty("user");
             password =properties.getProperty("password");
             driver =properties.getProperty("driver");
            try {
                Class.forName(driver);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    /*获取连接*/
    public static Connection getConnection() throws SQLException {

            return DriverManager.getConnection(url,user,password);

    }
    //关闭资源
   public static void close(Statement stmt,Connection conn){
          if (stmt!=null){
              try {
                  stmt.close();
              } catch (SQLException e) {
                  e.printStackTrace();
              }
          }
       if (conn!=null){
           try {
               conn.close();
           } catch (SQLException e) {
               e.printStackTrace();
           }
       }
   }
    public static void close(ResultSet rs, Statement stmt, Connection conn){
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

你可能感兴趣的:(8.0+版本mysql-connector-java的驱动注册和url设置以及JDBCUtils工具类抽取)