データベースというクラスがあります。
public class Database {
public Connection connect = null;
public Statement st = null;
public PreparedStatement ps = null;
public ResultSet rs = null;
public boolean connectDB() throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager
.getConnection("jdbc:mysql://localhost/ots?"
+ "user=root&password=mongolia");
} catch (Exception e) {
System.out.println(e);
}
return true;
}
public void disconnectDB() {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (connect != null) {
connect.close();
}
} catch (Exception e) {
}
}
}
データベースクラスを拡張しているuserというクラス
public class User extends Database {
public ResultSet fetchTable(){
try{
connectDB();
st = connect.createStatement();
rs = st.executeQuery("SELECT * FROM user");
}catch(Exception e){
System.out.println(e);
}finally{
disconnectDB();
}
return rs;
}
}
//Inside JSP page
User user = new User();
ResultSet data = user.fetchTable();
//getting exception in the data.next() function
//java.sql.SQLException: Operation not allowed after ResultSet closed
while(data.next()){
out.println("<p>"+data.getInt(0)+"</p>");
}
//getting exception in the data.next() function
//java.sql.SQLException: Operation not allowed after ResultSet closed