//java.sql.ResultSet 接口:结果集(客户端存表数据的对象)
//1.封装结果集
/*
提供一个游标,默认游标指向结果集第一行之前,调用一次next(),游标向下移动一行,提供一些get方法
封装数据的方法:
Object getObject(int columnIndex); 根据序号取值,索引从1开始
Object getObject(String ColumnName); 根据列名取值
将结果集中的数据封装到javaBean中
getClob 文本
*/
//2.可移动游标的方法
/*
boolean next() 将光标从当前位置向前移一行
Boolean previous() 将光标移动到次ResultSet对象的上一行
boolean absolute(int row) 参数是当前行的索引,从1开始根据行的索引定位移动的指定索引行
void afterLast() 将光标移动到末尾,正好位于最后一行之后
void beforeFirst() 将光标移动到开头,正好位于第一行之前
*/
//1.封装结果集
/*
提供一个游标,默认游标指向结果集第一行之前,调用一次next(),游标向下移动一行,提供一些get方法
封装数据的方法:
Object getObject(int columnIndex); 根据序号取值,索引从1开始
Object getObject(String ColumnName); 根据列名取值
将结果集中的数据封装到javaBean中
getClob 文本
*/
//2.可移动游标的方法
/*
boolean next() 将光标从当前位置向前移一行
Boolean previous() 将光标移动到次ResultSet对象的上一行
boolean absolute(int row) 参数是当前行的索引,从1开始根据行的索引定位移动的指定索引行
void afterLast() 将光标移动到末尾,正好位于最后一行之后
void beforeFirst() 将光标移动到开头,正好位于第一行之前
*/
TestCRUD.java
package cw_entity; import java.util.Date; public class User { private int id; private String name; private String password; private String email; private Date birthday; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", password=" + password + ", email=" + email + ", birthday=" + birthday + "]"; } }
User.java
package cw_jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import java.util.Properties; import org.junit.Test; import cw_entity.User; public class TestCRUD { @Test public void testSelect() throws Exception{ //1.加载驱动 Class.forName("com.mysql.jdbc.Driver"); //反射机制加载驱动类 // 2.获取连接Connection //主机:端口号/数据库名 //map集合 Properties info=new Properties(); info.setProperty("user", "root"); info.setProperty("password", "root"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/users",info); // 3.得到执行sql语句的对象Statement Statement stmt = conn.createStatement(); // 4.执行sql语句,并返回结果 ResultSet rs=stmt.executeQuery("select *from users"); List<User> list=new ArrayList<User>(); // 5.处理结果 while(rs.next()){ User user=new User(); user.setId(rs.getInt("id")); user.setName(rs.getString("name")); user.setPassword(rs.getString("password")); user.setEmail(rs.getString("email")); user.setBirthday(rs.getDate("birthday")); list.add(user); } // 6.关闭资源 rs.close(); stmt.close(); conn.close(); for(User users:list){ System.out.println(users); } } //可以利用光标的移动来做分页 @Test public void testSelect1() throws Exception{ //1.加载驱动 Class.forName("com.mysql.jdbc.Driver"); //反射机制加载驱动类 // 2.获取连接Connection //主机:端口号/数据库名 //map集合 Properties info=new Properties(); info.setProperty("user", "root"); info.setProperty("password", "root"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/users",info); // 3.得到执行sql语句的对象Statement Statement stmt = conn.createStatement(); // 4.执行sql语句,并返回结果 ResultSet rs=stmt.executeQuery("select *from users"); List<User> list=new ArrayList<User>(); // 5.处理结果 rs.afterLast(); //一共三行,移到最后一行之后 rs.previous(); //向上以一行即为最后一行 User user=new User(); user.setId(rs.getInt("id")); user.setName(rs.getString("name")); user.setPassword(rs.getString("password")); user.setEmail(rs.getString("email")); user.setBirthday(rs.getDate("birthday")); list.add(user); // 6.关闭资源 rs.close(); stmt.close(); conn.close(); System.out.println(user); } }