jdbc ---- DBUTilDao 类

1, 列用工具包  阿里的

DbUtils: JDBC Utility Component Examples

再次封装成通用的 update, query

package com.ljs.dao;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List; 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 com.ljs.util.JDBCUtil; public class DBUtilDao{ QueryRunner qRunner = new QueryRunner();
Connection connection = null;
/*
* 查询多条结果。
*/
public<T> List<T> queryMutil(Class<T> clazz, String sql, Object...object){
List<T> list = new ArrayList<>();
try {
connection = JDBCUtil.getConn();
list = qRunner.query(connection, sql,new BeanListHandler<T>(clazz),object);
}catch (Exception e) { e.printStackTrace();
}finally {
JDBCUtil.close(null, null, connection);
} return list;
} /*
* 查询一条结果。
*/
public<T> T queryOne(Class<T> clazz, String sql, Object...objects){ try {
connection = JDBCUtil.getConn();
T t = clazz.newInstance();
t = qRunner.query(connection, sql, new BeanHandler<T>(clazz), objects); return t;
} catch (Exception e) { e.printStackTrace();
}
return null;
}
/*
* 更新表
*/
public<T> void update(String sql, Object...objects){ try {
connection = JDBCUtil.getConn();
qRunner.update(connection, sql, objects);
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 查询某个数scalar
*/
public String scalar(String sql, Object...objects){ try {
connection = JDBCUtil.getConn();
return qRunner.query(connection, sql,new ScalarHandler<>(), objects).toString();
} catch (Exception e) { e.printStackTrace();
} return null;
} }
上一篇:UDP反射DDoS攻击原理和防范


下一篇:[Algorithm] Circular buffer