1.使用ThreadLocal整合jdbc工具类:
注意事项:tomcat线程复用可能导致,线程获取到已经关闭的连接,所以需要在获取连接时判断是否关闭,如果已关闭重新创建连接;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
//使用ThreadLocal完成操作
public class JdbcUtils {
public static final String DRIVERCLASS;
public static final String URL;
public static final String USERNAME;
public static final String PASSWORD;
static {
// 从配置文件中加载数据
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
DRIVERCLASS = bundle.getString("driver");
URL = bundle.getString("url");
USERNAME = bundle.getString("username");
PASSWORD = bundle.getString("password");
}
static {
// 1.注册驱动
try {
Class.forName(DRIVERCLASS);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private static final ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
public static Connection getConnection() throws ClassNotFoundException,
SQLException {
// 2.获取连接对象Connection
Connection con = tl.get(); // 从ThreadLocal中获取,那么第一次获取时,得到的是null 代码
// map.get(Thread.currentThread());
//tomcat线程复用可能导致,线程获取到已经关闭的连接,所以需要判断是否关闭
if (con == null || con.isClosed()) {
con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
tl.set(con);// 这时就相当于 map.put(Thread.currentThead(),con);
}
return con;
}
// 做一个开启事务的方法
public static void startTransaction() throws ClassNotFoundException,
SQLException {
Connection con = JdbcUtils.getConnection();
if (con != null)
con.setAutoCommit(false);
}
// 事务回滚
public static void rollback() throws SQLException, ClassNotFoundException {
Connection con = JdbcUtils.getConnection();
if (con != null) {
con.rollback();
}
}
// 事务提交及关闭资源
public static void commitAndClose() throws SQLException,
ClassNotFoundException {
Connection con = JdbcUtils.getConnection();
if (con != null) {
con.commit();
con.close();
}
}
public static void Close() throws SQLException,
ClassNotFoundException {
Connection con = JdbcUtils.getConnection();
if (con != null) {
con.close();
}
}
// 提供关闭操作
public static void closeConnection(Connection con) throws SQLException {
if (con != null) {
con.close();
}
}
public static void closeStatement(Statement st) throws SQLException {
if (st != null)
st.close();
}
public static void closeResultSet(ResultSet rs) throws SQLException {
if (rs != null)
rs.close();
}
}
2.简单jdbc工具类:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
public class JdbcUtils {
public static final String DRIVERCLASS;
public static final String URL;
public static final String USERNAME;
public static final String PASSWORD;
static {
// 从配置文件中加载数据
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
DRIVERCLASS = bundle.getString("driverClass");
URL = bundle.getString("url");
USERNAME = bundle.getString("username");
PASSWORD = bundle.getString("password");
}
static {
// 1.注册驱动
try {
Class.forName(DRIVERCLASS);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws ClassNotFoundException,
SQLException {
// 2.获取连接对象Connection
Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
return con;
}
// 提供关闭操作
public static void closeConnection(Connection con) throws SQLException {
if (con != null) {
con.close();
}
}
public static void closeStatement(Statement st) throws SQLException {
if (st != null)
st.close();
}
public static void closeResultSet(ResultSet rs) throws SQLException {
if (rs != null)
rs.close();
}
}
3.配置文件,classPath根路径下:
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql:///day17
username=root
password=root