package Utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
static Properties properties = null;
static DataSource ds = null;
static {
try {
properties = new Properties();
properties.load(new FileInputStream("src\\druid.properties"));
//1、创建了一个指定参数的数据库连接池
ds = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getconnection() throws Exception {
//从数据库连接池中获取可用的连接对象
return ds.getConnection();
}
public static void main(String[] args) throws Exception {
getconnection();
}
public static void close(ResultSet set, Statement statement, Connection con) {
if (set != null) {
try {
set.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
配置文件(druid.properties)
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/book?rewriteBatchedStatements=true
username=root
password=qq1802702683
initialSize=10
minIdle=5
maxActive=20
maxWait=5000
basic类
package JDBC.Dao;
import JDBC.Utils.JDBCUtils;
import JDBC.Utils.JDBCUtilsByDurid;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.Connection;
import java.util.List;
public class BasicDao <T> {
private QueryRunner qr = new QueryRunner();
public int update(String sql, Object... params) {
Connection con = null;
try {
con = JDBCUtilsByDurid.getconnection();
int update = qr.update(con, sql, params);
return update;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JDBCUtils.close(null, null, con);
}
}
public T querySingle(String sql, Class<T> clazz, Object... params) {
Connection con = null;
try {
con = JDBCUtilsByDurid.getconnection();
return qr.query(con, sql, new BeanHandler<>(clazz), params);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JDBCUtils.close(null, null, con);
}
}
public List<T> queryMulti(String sql, Class<T> clazz, Object... params) {
Connection con = null;
try {
con = JDBCUtilsByDurid.getconnection();
return qr.query(con, sql, new BeanListHandler<>(clazz), params);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JDBCUtils.close(null, null, con);
}
}
public Object scalar(String sql, Object... params) {
Connection con = null;
try {
con = JDBCUtilsByDurid.getconnection();
return qr.query(con, sql, new ScalarHandler<>(), params);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JDBCUtils.close(null, null, con);
}
}
}
CrudUtils
package JDBC.Utils;
import JDBC.JavaBean.Boys;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
/*
* 此类用于封装通用的增删改查方法
* 功能:
* 1、执行增删改
* 2、执行查询
* */
public class CRUDUtils {
/*功能:增删改
针对于任何表增删改语句
* */
public static int update(String sql, Object... params) {//params可变长度参数
//1、获取连接
try {
Connection con = JDBCUtilsByDurid.getconnection();
//2、执行sql语句
PreparedStatement pst = con.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pst.setObject(i + 1, params[i]);
}
int update = pst.executeUpdate();
return update;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Boys querySingle(String sql, Object... params) {
Connection con = null;
PreparedStatement pst = null;
ResultSet res = null;
try {
//1、获取连接
con = JDBCUtilsByDurid.getconnection();
//2、执行查询
pst = con.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pst.setObject(i + 1, params[i]);
}
res = pst.executeQuery();
if (res.next()) {
int id = res.getInt("id");
String boyname = res.getString("boyName");
int userCP = res.getInt("userCP");
Boys bo = new Boys(id, boyname, userCP);
return bo;
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JDBCUtils.close(res, pst, con);
}
return null;
}
public static List<Boys> queryMutil(String sql, Object... params) {
Connection con = null;
PreparedStatement pst = null;
ResultSet res = null;
List<Boys> list=null;
try {
//1、获取连接
con = JDBCUtilsByDurid.getconnection();
//2、执行查询
pst = con.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pst.setObject(i + 1, params[i]);
}
res = pst.executeQuery();
list=new ArrayList<>();
while (res.next()) {
int id = res.getInt("id");
String boyname = res.getString("boyName");
int userCP = res.getInt("userCP");
Boys bo = new Boys(id, boyname, userCP);
list.add(bo);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JDBCUtils.close(res, pst, con);
}
return list;
}
}