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();
}
}
}
}