package cn.mldn.dbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnection {
private static final String DBDRIVER = "com.mysql.jdbc.Driver";
private static final String DBURL = "jdbc:mysql://localhost:3306/c";
private static final String DBUSER = "root";
private static final String PASSWORD = "";
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
private static Connection rebuildConnection() throws Exception {
Class.forName(DBDRIVER);
return DriverManager.getConnection(DBURL, DBUSER, PASSWORD);
}
public static Connection get() {
Connection conn = threadLocal.get();
try {
if (conn == null) {
conn = rebuildConnection();
threadLocal.set(conn);
}
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void close() {
Connection conn = threadLocal.get();
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
threadLocal.remove();
}
}
}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦