ResultSet
封装DQL查询语句的结果
Boolean next():(1)、将光标从当前位置向前移动一行 (2)、判断当前行是否为有效行
getxxx(参数):获取数据
xxx:数据类型 int getint/String getString()
参数:
int:列的编号,从1开始
String:列的名称
查询account账户表数据,封装为Account对象中,并且存储到ArrayList集合中。
package com.avb.jdbc; import com.avb.pojo.Account; import org.junit.jupiter.api.Test; import java.sql.*; import java.util.ArrayList; import java.util.List; public class jdbcdemo { @Test public static void main(String[] args) throws Exception { //注册驱动 Class.forName("com.mysql.jdbc.Driver"); //获取连接 String url = "jdbc:mysql://127.0.0.1:3306/db1"; String username = "root"; String password = "root"; Connection conn = DriverManager.getConnection(url,username,password); //定义sql String sql = "select * from account;"; //获取执行sql的对象Statement Statement stmt = conn.createStatement(); //执行sql ResultSet rs = stmt.executeQuery(sql); //循环遍历 //创建集合 List<Account> list = new ArrayList<>(); while(rs.next()){ Account account = new Account(); //获取数值 int id = rs.getInt(1); String name = rs.getString(2); int money = rs.getInt(3); account.setId(id); account.setMoney(money); account.setName(name); //存入集合 list.add(account); } System.out.println(list); //释放资源 stmt.close(); conn.close(); rs.close(); } }