public class JDBCDemo04 {
@Test
public void queryAll() throws SQLException {
Connection con = JDBCUtil.getConnect();
String sql = "select * from users";
PreparedStatement pstmt = con.prepareStatement(sql);
List<User> list = new ArrayList<>();
ResultSet rs = pstmt.executeQuery();
while (rs.next()){
String uid = rs.getString("uid");
String uname = rs.getString("uname");
String upass = rs.getString("upass");
User user = new User(uid,uname,upass);
list.add(user);
}
if (list.size()==0){
System.out.println("没有查询到结果");
}else {
for (User u : list) {
System.out.println(u);
}
}
JDBCUtil.release(con,pstmt,rs);
}
}
使用PreparedStatement查询数据库表中所有元素