Result有两个功能,作用都是获取查询结果
(一)next()
(1)将光标向下移动一行
(2)判断当前行是否有效
返回值:
true:有效行,当前行有数据
false:无效行,当前行没有数据
(二)xxx getXxx(参数)---获取数据
xxx:数据类型 如 int getInt(参数)
String getString(参数)
参数:int:列的编号从1开始
String:列名
//判断循环游标是否为最后一行
while(rs.next()){
rs.getXxx(参数);//获取数据
}
运用此性质遍历数据库数据表的全部数据
@Test
public void testResult() throws Exception {
//1.注册驱动
// Class.forName("com.mysql.jdbc.Driver");
//2.获取链接
String url="jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false";
String username = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url,username,password);
//3.定义sql
String sql = "select *from account";
//4.获取执行对象
Statement stmt =conn.createStatement();
// 5.执行sql
ResultSet rs = stmt.executeQuery(sql);
// 6. 处理结果,遍历rs(参数为int)
//6.1 光标向下移动一行,并且判断是否有数据
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
double money = rs.getDouble(3);
//遍历数据
System.out.println(id);
System.out.println(name);
System.out.println(money);
System.out.println("-------------------");
}
// 6. 处理结果,遍历rs(参数为String)
//6.1 光标向下移动一行,并且判断是否有数据
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
double money = rs.getDouble("money");
//遍历数据
System.out.println(id);
System.out.println(name);
System.out.println(money);
System.out.println("-------------------");
}
//7.释放资源
rs.close();
stmt.close();
conn.close();
}
案例--查询account内的数据,封装在Account对象中,并存储在ArrayList集合中
定义实体类Account
查询数据,封装到Account
将Account封装到Arraylist中
开始第一步--定义实体类Account
1.新建包存放类,包名为com.wkw.pojo

2.新建Java类,类名为Account


3.在类中定义列名-id account money

4.生成getter 和setter方法

tips--按住shift选择第一个再选择最后一个完成全选

在下方空白位置生成toString()方法

Account类生成完毕
开始第二步--查询数据,封装到Account
while(rs.next()){
Account account = new Account();
int id = rs.getInt("id");
String name = rs.getString("name");
double money = rs.getDouble("money");
//封装
account.setId(id);
account.setName(name);
account.setMoney(money);
}
开始第三步--将Account封装到Arraylist中
//创建集合
List<Account> list=new ArrayList<>();
while(rs.next()){
Account account = new Account();
int id = rs.getInt("id");
String name = rs.getString("name");
double money = rs.getDouble("money");
//赋值
account.setId(id);
account.setName(name);
account.setMoney(money);
//存入集合
list.add(account);
}
完成--完整代码如下
@Test
public void testResult2() throws Exception {
//1.注册驱动
// Class.forName("com.mysql.jdbc.Driver");
//2.获取链接
String url="jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false";
String username = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url,username,password);
//3.定义sql
String sql = "select *from account";
//4.获取执行对象
Statement stmt =conn.createStatement();
// 5.执行sql
ResultSet rs = stmt.executeQuery(sql);
//创建集合
List<Account> list=new ArrayList<>();
// 6. 处理结果,遍历rs
//6.1 光标向下移动一行,并且判断是否有数据
while(rs.next()){
Account account = new Account();
int id = rs.getInt("id");
String name = rs.getString("name");
double money = rs.getDouble("money");
//赋值
account.setId(id);
account.setName(name);
account.setMoney(money);
//存入集合
list.add(account);
}
System.out.println(list);
//7.释放资源
rs.close();
stmt.close();
conn.close();
}
}
输出结果为
[Account{id=1, name='张三', money=2000.0}, Account{id=2, name='李四', money=2000.0}]