本文利用的数据库如下

目录
如果想查询对应数据表的数据,我们就需要制造一个表的对象,里面包含了表中的内容,比如本文查询一下 Customers 数据表

我们就可以根据转换表,写出数据表对象
- import java.sql.Date;
-
- public class Customer {
- private int id;
- private String email;
- private String name;
- private Date birth;
-
- public Customer() {
- super();
- }
-
- public Customer(int id, String email, String name, Date birth) {
- super();
- this.id = id;
- this.email = email;
- this.name = name;
- this.birth = birth;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getEmail() {
- return email;
- }
-
- public void setEmail(String email) {
- this.email = email;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public Date getBirth() {
- return birth;
- }
-
- public void setBirth(Date birth) {
- this.birth = birth;
- }
-
- @Override
- public String toString() {
- return "Customer [id=" + id + ", email=" + email + ", name=" + name + ", birth=" + birth + "]";
- }
- }
要查询数据库,我们可以先梳理一下思路。首先我们要知道查询哪个表,查询语句是什么,查询多少数据。那么顺着这个思路,我们就可以把以上三点写为参数传入方法中
- import java.io.InputStream;
- import java.lang.reflect.Field;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Properties;
- import org.junit.Test;
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.PreparedStatement;
- import com.mysql.jdbc.ResultSetMetaData;
-
- public class JDBCSQLTest {
- @Test
- public void TestForList() throws Exception {
- //如果表名与sql中的关键字重合,我们需要添加着重号
- String sql = "select id,name,email from customers where id < ?";
- GetForListTest gfl = new GetForListTest();
- List<Customer> list = gfl.getForList(Customer.class,sql,12);
- list.forEach(System.out::println);
- }
- }
- class GetForListTest{
- public <T> List<T> getForList(Class<T> clazz,String sql,Object ...args) throws Exception {
- //读取文件中的信息
- InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
- //将用户名和密码封装在Properties中
- Properties pros = new Properties();
- pros.load(is);
- String user = pros.getProperty("user");
- String password = pros.getProperty("password");
- String url = pros.getProperty("url");
- String driverClass = pros.getProperty("driverClass");
- //加载驱动
- Class.forName(driverClass);
- //获取连接
- Connection conn = (Connection) DriverManager.getConnection(url, user, password);
- //预编译sql语句,返回PreparedStatement的实例
- PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
- //填充占位符
- for (int i = 0;i < args.length;i++) {
- ps.setObject(i + 1, args[i]);
- }
- ResultSet rs = ps.executeQuery();
- //获取结果集的元数据:ResultSetMetaData
- ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
- //通过ResultSetMetaData获取结果集中的列数
- int columnCount = rsmd.getColumnCount();
- //创造集合
- ArrayList<T> list = new ArrayList<T>();
- while(rs.next()) {
- T t = clazz.newInstance();
- //处理结果集一行数据中的每一个列:给t对象指定的属性赋值
- for (int i = 0;i < columnCount; i++) {
- //获取列值
- Object columnValue = rs.getObject(i + 1);
- //获取每个列的列名
- String columnLabel = rsmd.getColumnLabel(i + 1);
- //通过反射给t对象指定的columnName属性,赋值为columnValue
- Field field = clazz.getDeclaredField(columnLabel);
- field.setAccessible(true);
- field.set(t, columnValue);
- }
- list.add(t);
- }
- return list;
- }
- }
在查询数据表的时候,我们要获取数据表中的元数据,元数据可以理解为数据的整体(我是这样理解的,不知道对不对,欢迎大家纠正),之后我们利用遍历获取到一行中(一行就是一次 next 方法)所有列名与列值,再利用循环获取所有数据,存入集合当中
