对数据库连接池封装的curd工具类
在不使用持久化框架的情况下,使用数据库连接池,同时对CURD进行一定的封装,简化jdbc的使用 数据库连接池工具类,可以根据自己的情况使用其他数据库连接池
public class MyDatabaseLinkPool {
private static ComboPooledDataSource dataSource;
private static String user="root";
private static String password="123456";
private static String url="jdbc:mysql://127.0.0.1:3306/flink?useUnicode=true&characterEncoding=utf-8";
static {
dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(password);
dataSource.setJdbcUrl(url);
try {
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
} catch (PropertyVetoException e) {
System.out.println("jdbc驱动加载异常");
e.printStackTrace();
}
dataSource.setInitialPoolSize(50);
dataSource.setAcquireIncrement(50);
dataSource.setMinPoolSize(50);
dataSource.setMaxPoolSize(1000);
dataSource.setMaxStatements(50);
dataSource.setMaxIdleTime(20);
dataSource.setIdleConnectionTestPeriod(20);
dataSource.setAcquireRetryAttempts(5);
System.out.println("数据库连接池初始化");
}
public static synchronized Connection getConnection() throws SQLException {
System.out.println("获取数据库连接");
return dataSource.getConnection();
}
}
jdbc工具类,简化curd操作
public class JdbcUtil {
private PreparedStatement ps=null;
private Connection connection=null;
private ResultSet resultSet=null;
private PreparedStatement setPreparedStatement(PreparedStatement ps, String sql, Object ...objects) throws SQLException {
ps = connection.prepareStatement(sql);
for (int i=1;i<=objects.length;i++)
{
ps.setObject(i,objects[i-1]);
}
return ps;
}
private Object setObjectValue(Object object, Field[] fields, Map map) throws NoSuchFieldException, IllegalAccessException {
for (int i = 0; i < fields.length; i++) {
Field field = object.getClass().getDeclaredField(fields[i].getName());
field.set(object,null);
if(map.get(fields[i].getName())!=null)
{
field.set(object, map.get(fields[i].getName()));
}
}
return object;
}
public void Commit( ) throws SQLException {
connection.commit();
}
public void open() throws SQLException {
connection= MyDatabaseLinkPool.getConnection();
}
public void insert(String sql,Object ...objects) throws SQLException {
ps=setPreparedStatement(ps,sql,objects);
ps.executeUpdate();
}
public void delete(String sql,Object ...objects) throws SQLException {
ps =setPreparedStatement(ps,sql,objects);
ps.executeUpdate();
}
public int update(String sql,Object ...objects) throws SQLException {
int rows=-1;
ps = ps=setPreparedStatement(ps,sql,objects);
ps.executeUpdate();
return rows;
}
public List<Map> query(String sql, Object ...objects) throws SQLException {
List<Map> list=new ArrayList<Map>();
ps =setPreparedStatement(ps,sql,objects);
resultSet= ps.executeQuery();
ResultSetMetaData rsmeta = resultSet.getMetaData();
int length = rsmeta.getColumnCount();
while(resultSet.next()) {
Map<String,Object> map = new HashMap<String,Object>();
for(int i=0;i<length;i++) {
map.put(rsmeta.getColumnLabel(i+1), resultSet.getObject(rsmeta.getColumnLabel(i+1)));
}
list.add(map);
}
return list;
}
,"运动");
* **/
public List<Object> queryObject(String sql,Object queryObject,Object ...objects) throws SQLException, NoSuchFieldException, IllegalAccessException, ClassNotFoundException, InstantiationException {
List<Object> list=new ArrayList<Object>();
Field[] fields= queryObject.getClass().getFields();
ps =setPreparedStatement(ps,sql,objects);
resultSet= ps.executeQuery();
ResultSetMetaData rsmeta = resultSet.getMetaData();
int length = rsmeta.getColumnCount();
int i=0;
HashMap<String,Object> map = new HashMap<String,Object>();
while(resultSet.next()) {
Object row=Class.forName(queryObject.getClass().getName()).newInstance();
for(i=0;i<length;i++) {
map.put(rsmeta.getColumnLabel(i+1), resultSet.getObject(rsmeta.getColumnLabel(i+1)));
}
row=setObjectValue(row,fields,map);
list.add(row);
}
return list;
}
public void close() throws SQLException {
if(ps!=null)
ps.close();
if (connection!=null)
connection.close();
}
}
|