1、JDBC配置文件
driver=com.mysql.cj.jdbc.Driver
userName=root
passWord=root
url=jdbc:mysql://localhost:3306/java52_jdbc?useUnicode=true&charset=utf8&useSSL=true&serverTimezone=GMT%2B8
2、數據庫連接池配置
#數據庫連接驅動
driverClassName=com.mysql.cj.jdbc.Driver
#數據庫連接url
url=jdbc:mysql://localhost:3306/java52_jdbc
#數據庫連接用戶名
username=root
#數據庫連接密碼
password=root
#初始化連接數
initialSize=10
#最大連接數量
maxActive=200
#最小空閑連接數量
maxIdle=5
#最大等待時間 ms,
maxWait=60000
#數據庫連接屬性
connectionProperties=useUnicode=true;charset=utf8;useSSL=true;serverTimezone=CST
#開啟事務管理:關閉自動提交
defaultAutoCommit=false
#設置事務隔離級別:NONE,READ_COMMITTED,READ_UNCOMMITTED,REPEATABLE_READ,SERIALIZABLE
defaultTransactionIsolation=REPEATABLE_READ
#默認只讀
#defaultReadOnly=
3、JDBC工具類
public class JDBCUtil {
private static String driver;// 驅動
private static String url;// 數據庫連接協議地址
private static String userName;// 數據登錄用戶名
private static String passWord;// 數據庫登錄的密碼
// 1、加載驅動:com.mysql.jdbc.Driver,com.mysql.cj.jdbc.Driver(8.0)
static {
// 讀取配置文件獲得屬性值
Properties p = new Properties();
InputStream in = null;
try {
in = new FileInputStream("resource/jdbc.properties");
p.load(in);
driver = p.getProperty("driver");
url = p.getProperty("url");
userName = p.getProperty("userName");
passWord = p.getProperty("passWord");
// com.mysql.cj.jdbc.Driver
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
// 2、打開連接開啟事務
public Connection getConnection() {
try {
Connection ct = DriverManager.getConnection(url, userName, passWord);// 打開連接
ct.setAutoCommit(false);// 開啟事務
return ct;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 3、關閉資源
public void release(Connection ct, Statement ps, ResultSet st) {
if (st != null) {
try {
st.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ct != null) {
try {
ct.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 4、通過的cud方法,5星*****
*
* @param clazz數據表對應實體類類型
* @param sql要執行的DML語句
* @param args:sql語句的占位符對應的參數
* @return
*/
public <T> Integer cud(Class<T> clazz, String sql, Object... args) {
Connection ct = getConnection();
PreparedStatement ps = null;
try {
ps = ct.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject((i + 1), args[i]);
}
int i = ps.executeUpdate();
ct.commit();
return i;
} catch (SQLException e) {
e.printStackTrace();
try {
ct.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
} finally {
release(ct, ps, null);
}
return null;
}
}
4、數據庫連接池工具類
public class JDBCPoolUtil {
private static DataSource ds;
static {
Properties p = new Properties();
InputStream in = null;
try {
in = new FileInputStream("resource/jdbcpool.properties");
p.load(in);
ds = BasicDataSourceFactory.createDataSource(p);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public Connection getConnection() {
try {
Connection ct = ds.getConnection();
return ct;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void release(Connection ct, Statement ps, ResultSet st) {
if (st != null) {
try {
st.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ct != null) {
try {
ct.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}