Mysql不同版本的链接
mysql5.0
// 数据库驱动(MySql5.x)
private static final String DRIVER = "com.mysql.jdbc.Driver";
// 数据库url
private static final String URL = "jdbc:mysql://localhost:3306/bank?useUnicode=true&characterEncoding=utf8";
// 数据库名称
private static final String USERNAME = "root";
// 数据库密码
private static final String PASSWORD = "";
mysql8.0
// 数据库驱动(MySql8.x)
private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
// 数据库url
private static final String URL = "jdbc:mysql://localhost:3306/bank?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=false";
// 数据库名称
private static final String USERNAME = "root";
// 数据库密码
private static final String PASSWORD = "";
package com.llk.bk.common;
import java.sql.*;
/**
* 数据库连接的工具类
*/
public class DBHelper {
// 数据库驱动
private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
// 数据库url
private static final String URL = "jdbc:mysql://localhost:3306/bank?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=false";
// 数据库名称
private static final String USERNAME = "root";
// 数据库密码
private static final String PASSWORD = "123456";
// 加载驱动
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取数据库的连接
*
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
/**
* 关闭数据库(针对增删改)
* @param conn
* @param pst
*/
public static void close(Connection conn, Statement pst) {
try {
if (pst != null) {
pst.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
/**
* 关闭数据库(针对查询操作)
* @param conn
* @param pst
* @param rs
*/
public static void close(Connection conn, Statement pst, ResultSet rs){
try {
if(rs!=null){
rs.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
close(conn,pst);
}
}