注:在项目里面需要引入BeanUtil,Logging jar 包
create table student
(
flow_id int,
type int,
id_card varchar(50),
student_name varchar(50),
location varchar(80),
grade int
)
insert into student values(1000,1,'429004','小明','深圳',1);
insert into student values(1001,1,'429005','xiaoming','深圳',1);
insert into student values(1002,1,'429006','小强','深圳',1);
insert into student values(1003,1,'429007','xiaoqiang','深圳',1);
insert into student values(1004,1,'429008','curtis','深圳',1);
package com.curtis.student4;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
import com.curtis.jdbc.JDBCTools;
public class GetListStudent {
@Test
public void getStudent() {
String sql = "select * from student where flow_id=?";
Student student = getMessage(Student.class, sql, 1000);
System.out.println(student);
}
@Test
public void getStudents() {
String sql = "select * from student";
List<Student> students = getMessages(Student.class, sql);
System.out.println(students);
}
@Test
public void getValue() {
String sql = "select flow_id from student where flow_id=?";
int flow_id = getOnlyValue(sql, 1000);
System.out.println(flow_id);
}
// 查询一行记录
public <T> T getMessage(Class<T> clazz, String sql, Object... args) {
List<T> result = getMessages(clazz, sql, args);
if (result.size() > 0) {
return result.get(0);
}
return null;
}
// 查询多行记录
public <T> List<T> getMessages(Class<T> clazz, String sql, Object... args) {
List<T> list = new ArrayList<>();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCTools.getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
resultSet = preparedStatement.executeQuery();
List<Map<String, Object>> values = handleResultSetToMapList(resultSet);// 调用下面的方法
list = transfterMapListToBeanList(clazz, values);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.releaseDB(resultSet, preparedStatement, connection);
}
return list;
}
public List<Map<String, Object>> handleResultSetToMapList(
ResultSet resultSet) throws SQLException {
List<Map<String, Object>> values = new ArrayList<>();
List<String> columnLabels = getColumnLabels(resultSet);
Map<String, Object> map = null;
while (resultSet.next()) {
map = new HashMap<>();
for (String columnLabel : columnLabels) {
Object value = resultSet.getObject(columnLabel);
map.put(columnLabel, value);
}
values.add(map);
}
return values;
}
private List<String> getColumnLabels(ResultSet rs) throws SQLException {
List<String> labels = new ArrayList<>();
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 0; i < rsmd.getColumnCount(); i++) {
labels.add(rsmd.getColumnLabel(i + 1));
}
return labels;
}
public <T> List<T> transfterMapListToBeanList(Class<T> clazz,
List<Map<String, Object>> values) throws InstantiationException,
IllegalAccessException, InvocationTargetException {
List<T> result = new ArrayList<>();
T bean = null;
if (values.size() > 0) {
for (Map<String, Object> m : values) {
bean = clazz.newInstance();
for (Map.Entry<String, Object> entry : m.entrySet()) {
String propertyName = entry.getKey();
Object value = entry.getValue();
BeanUtils.setProperty(bean, propertyName, value);
}
result.add(bean);
}
}
return result;
}
// 查询单个值
public <T> T getOnlyValue(String sql, Object... args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
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 (T) resultSet.getObject(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.releaseDB(resultSet, preparedStatement, connection);
}
return null;
}
}