ResultSet对象的next方法配合while()循环取出数据
package cn.itcast.jdbc;
import java.sql.*;
public class JdbcDemo7 {
public static void main(String[] args) {
Connection conn =null;
Statement stat =null;
ResultSet res=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/liuqi","root","123456");
stat = conn.createStatement();
String sql="select * from user";
res = stat.executeQuery(sql);
while(res.next()){
int id = res.getInt("id");
String username = res.getString("username");
String password1 = res.getString("password1");
System.out.println(id+"---"+username+"----"+password1);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(res!=null){
try {
res.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stat!=null){
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191118184920751.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTA5ODk5OA==,size_16,color_FFFFFF,t_70)