版权声明:本文为小斑马伟原创文章,转载请注明出处!
主要有以下功能
- 1 返回某条记录的某一个字段的值 或 一个统计的值(一共有多少条记录等.)
- 2 获取结果集的 ColumnLabel 对应的 List
- 3 处理结果集, 得到 Map 的一个 List, 其中一个 Map 对象对应一条记录
- 4 传入 SQL 语句和 Class 对象, 返回 SQL 语句查询到的记录对应的 Class 类的对象的集合
- 5 INSERT, UPDATE, DELETE 操作都可以包含在其中
INSERT, UPDATE, DELETE 操作都可以包含在其中
public void update(String sql, Object... args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JDBCTools.getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.releaseDB(null, preparedStatement, connection);
}
}
查询一条记录, 返回对应的对象
public T get(Class clazz, String sql, Object... args) {
List result = getForList(clazz, sql, args);
if(result.size() > 0){
return result.get(0);
}
return null;
}
/**
* 传入 SQL 语句和 Class 对象, 返回 SQL 语句查询到的记录对应的 Class 类的对象的集合
* @param clazz: 对象的类型
* @param sql: SQL 语句
* @param args: 填充 SQL 语句的占位符的可变参数.
* @return
*/
public List getForList(Class clazz,
String sql, Object... args) {
List list = new ArrayList<>();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//1. 得到结果集
connection = JDBCTools.getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
resultSet = preparedStatement.executeQuery();
//2. 处理结果集, 得到 Map 的 List, 其中一个 Map 对象
//就是一条记录. Map 的 key 为 reusltSet 中列的别名, Map 的 value
//为列的值.
List
获取结果集的 ColumnLabel 对应的 List
private List getColumnLabels(ResultSet rs) throws SQLException {
List labels = new ArrayList<>();
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 0; i < rsmd.getColumnCount(); i++) {
labels.add(rsmd.getColumnLabel(i + 1));
}
return labels;
}
返回某条记录的某一个字段的值 或 一个统计的值(一共有多少条记录等.)
public E getForValue(String sql, Object... args) {
//1. 得到结果集: 该结果集应该只有一行, 且只有一列
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//1. 得到结果集
connection = JDBCTools.getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
return (E) resultSet.getObject(1);
}
} catch(Exception ex){
ex.printStackTrace();
} finally{
JDBCTools.releaseDB(resultSet, preparedStatement, connection);
}
//2. 取得结果
return null;
}
测试:
DAO dao = new DAO();
@Test
public void testUpdate() {
String sql = "INSERT INTO customers(name, " +
"email, birth) VALUES(?,?,?)";
dao.update(sql, "XiaoMing", "[email protected]",
new Date(new java.util.Date().getTime()));
}
@Test
public void testGet() {
String sql = "SELECT flow_id flowId, type, exam_card examCard, " +
"id_card idCard, student_name studentName, location, " +
"grade FROM examstudent WHERE flow_id = ?";
Student student = dao.get(Student.class, sql, 5);
System.out.println(student);
}
@Test
public void testGetForList() {
String sql = "SELECT flow_id flowId, type, exam_card examCard, " +
"id_card idCard, student_name studentName, location, " +
"grade FROM examstudent";
List students = dao.getForList(Student.class, sql);
System.out.println(students);
}
@Test
public void testGetForValue() {
String sql = "SELECT exam_card FROM examstudent " +
"WHERE flow_id = ?";
String examCard = dao.getForValue(sql, 5);
System.out.println(examCard);
sql = "SELECT max(grade) FROM examstudent";
int grade = dao.getForValue(sql);
System.out.println(grade);
}