ResultSet结果集
-Statement执行SQL语句时返回ResultSet结果集
-ResultSet提供的检索不同类型字段的方法,常用的有:
getString():获得在数据库里是varchar,char等数据类型的对象
getFloat():获得在数据库里是Floatr数据类型的对象
getDate():获得在数据库里是Date数据类型的对象
getBoolean():获得在数据库里是Boolean数据类型的对象
-依序(先打开的后关闭)关闭使用对象及连接
Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc?&useSSL=false&serverTimezone=UTC" ,"root","123456"); String sql="select id,username,pwd from t_user where id>?"; ps=conn.prepareStatement(sql); ps.setObject(1, 7);//取出id>7的记录 rs=ps.executeQuery(); while(rs.next()) { System.out.println(rs.getInt(1)+"---"+rs.getString(2)+"---"+rs.getInt(3));//参数表示第几列 } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { try { if(rs!=null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } try { if(ps!=null) ps.close(); } catch (SQLException e) { e.printStackTrace(); } try { if(conn!=null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } }