@Test
public void testQuery() throws Exception{
Connection conn = null;
Statement st = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root","root");
//获取语句对象
st = (Statement) conn.createStatement();
//执行sql语句
String sql = "select * from depter";
//获取结果集
ResultSet rs = st.executeQuery(sql);
//把所有的结果集都显示出来
while (rs.next()){
Long id = rs.getLong("id");
Long name = rs.getLong("name");
Integer pwd = rs.getInt("pwd");
System.out.println(id+"-"+name+"-"+pwd);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//释放资源
try {
if(st!=null)st.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(conn!=null)conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
CRUD中关于查询所有结果集的代码
最新推荐文章于 2022-02-18 17:39:02 发布