try {
Class.forName("com.mysql.jdbc.Driver");
String ur1="jdbc:mysql://localhost:3306/lingshi";
String username="root";
String password="root";
Connection con=DriverManager.getConnection(ur1,username,password);
Statement stmt=con.createStatement();
String sql1="select * from guogan";
ResultSet rs1=stmt.executeQuery(sql1);
while(rs1.next()){
String id=rs1.getString(1);
System.out.print(id+"\t");
String name=rs1.getString("name");
System.out.print(name+"\t");
String price=rs1.getString(3);
System.out.print(price+"\t");
String weight=rs1.getString("weight");
System.out.print(weight+"\t");
String birthdate=rs1.getString("birthdate");
System.out.print(birthdate+"\t");
String enddate=rs1.getString("enddate");
System.out.print(enddate+"\t");
String PD=rs1.getString("PD");
System.out.print(PD+"\t");
rs1.close();
stmt.close();
con.close();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(SQLException e1){
e1.printStackTrace();
}
我的结果如下
1 芒果干 13.00 106g 2019.12.29 2020.12.29 12months java.sql.SQLException: Operation not allowed after ResultSet closed
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:963)