- 初始版本
public final class JDBCUtil{
private static String driver = "";
private static String url = "";
private static String user = "";
private static String password = "";
private JDBCUtil(){}
static {
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
driver = bundle.getString("driver");
url = bundle.getString("url");
user = bundle.getString("user");
password = bundle.getString("password");
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void closeAll(Connection con, Statement stmt, ResultSet rs){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace():
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
- 为了保证每个能够开启以及回滚事务,需要改进的JDBCUtil
public final class JDBCUtil {
private static String driver = "";
private static String url = "";
private static String user = "";
private static String password = "";
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();
static {
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
driver = bundle.getString("driver");
url = bundle.getString("url");
user = bundle.getString("user");
password = bundle.getString("password");
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
if (threadLocal.get() == null) {
try {
threadLocal.set(DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
}
return threadLocal.get();
}
public static void closeAll(Connection con, Statement stmt, ResultSet rs){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace():
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
threadLocal.remove();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
- 以上两种方式的配置文件
jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jgs1904?useSSL=true&serverTimezone=GMT%2B8
user=root
password=root
- 使用alibaba的druid数据库连接池
public final class DataSourceUtil {
private DataSourceUtil() {};
private static Properties pro = new Properties();
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();
private static DataSource dataSource = null;
static {
InputStream is = Demo02.class.getClassLoader().getResourceAsStream("druid.properties");
try {
pro.load(is);
} catch (IOException e) {
e.printStackTrace();
}
try {
dataSource = DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
}
public static DataSource getDataSource() {
return dataSource;
}
public static Connection getConnection() {
if (threadLocal.get() == null) {
try {
threadLocal.set(dataSource.getConnection());
} catch (SQLException e) {
e.printStackTrace();
}
}
return threadLocal.get();
}
public static void closeAll(Connection con, Statement stmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
threadLocal.remove();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
- 配置文件 druid.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/jgs1904?useSSL=true&serverTimezone=GMT%2B8
username=root
password=root
#初始化时池中建立的连接数。
initialSize=2
#最大的可活跃的连接池数量
maxActive=300
#最大等待时间
maxWait=60000
#是否缓存preparedStatement
poolPreparedStatements=true
#池中能够缓冲的preparedStatement语句数量
maxPoolPreparedStatementPerConnectionSize=200