No operations allowed after statement closed issue
- by Washu
I have the next methods in my singleton to execute the JDBC connections
public void openDB() throws ClassNotFoundException, IllegalAccessException,
InstantiationException, SQLException {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost/mbpe_peru";//mydb
conn = DriverManager.getConnection(url, "root", "admin");
st = conn.createStatement();
}
public void sendQuery(String query) throws SQLException {
st.executeUpdate(query);
}
public void closeDB() throws SQLException {
st.close();
conn.close();
}
And I'm having a problem in a void where i have to call this twice.
private void jButton1ActionPerformed(ActionEvent evt) {
Main.getInstance().openDB();
Main.getInstance().sendQuery("call insertEntry('"+EntryID()+"','"+SupplierID()+"');");
Main.getInstance().closeDB();
Main.getInstance().openDB();
for(int i=0;i<dataBox.length;i++){
Main.getInstance().sendQuery("call insertCount('"+EntryID()+"','"+SupplierID()+"','"+BoxID()+"');
Main.getInstance().closeDB();
}
}
I have already tried to keep the connection open and send the 2 querys and after that closed and it didnt work... The only way it worked was to not use the methods, declare the commands for the connection and use different variables for the connection and the statement. I thought that if i close the Connecion and the Statement I could use the variable once again since is a method but I'm not able to. Is there any way to solve this using my methods for the JDBC connection?