java - com.mysql.jdbc.exception.jdbc4.MySQLNonTransientConnectionException: No operation allowed after connection closed -


how solve problem , wrong in code? know question has been asked before cant solve problem

private void cb_categoriespopupmenuwillbecomevisible(javax.swing.event.popupmenuevent evt) {                                                               cb_categories.removeallitems();     try {         string sql_c = "select * inventory.categories";         cc.pst = cc.c.preparestatement(sql_c);         cc.rs = cc.pst.executequery();         while (cc.rs.next()) {             string c_name = cc.rs.getstring("categoryname");             cb_categories.additem(c_name);          }     } catch (exception e) {         joptionpane.showmessagedialog(null, e);     } {         try {             cc.rs.close();             cc.pst.close();         } catch (exception e) {          }     } }           

your resultset , preparedstatement not declared in method scope, have assume you've declared them elsewhere.

that's big mistake.

you should declare statement , resultset in method scope.

you make attempt close resources, should wrap them in individual try/catch blocks. cannot risk 1 being closed , not other.

there other things i'd criticize code (e.g. select *, mingling ui , database code in single class), that's enough start.

start interface:

package persistence;  import java.util.list;  /**  * created michael  * creation date 8/20/2017.  * @link https://stackoverflow.com/questions/45787151/com-mysql-jdbc-exception-jdbc4-mysqlnontransientconnectionexception-no-operatio/45787321?noredirect=1#comment78532554_45787321  */ public interface categorydao {     list<string> findallcategories(); } 

then write concrete implementation:

package database;  import database.util.databaseutils; import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory;  import javax.sql.datasource; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.util.arraylist; import java.util.list;  /**  * created michael  * creation date 8/20/2017.  * @link https://stackoverflow.com/questions/45787151/com-mysql-jdbc-exception-jdbc4-mysqlnontransientconnectionexception-no-operatio/45787321?noredirect=1#comment78532554_45787321  */ public class categorydaoimpl implements categorydao {      private static final log logger = logfactory.getlog(categorydaoimpl.class);     private static string select_categories = "select categoryname inventory.categories ";      private datasource datasource;      public categorydaoimpl(datasource datasource) {         this.datasource = datasource;     }      @override     public list<string> findallcategories() {         list<string> categories = new arraylist<>();         preparedstatement ps = null;         resultset rs = null;         try {             ps = this.datasource.getconnection().preparestatement(select_categories);             rs = ps.executequery();             while (rs.next()) {                 categories.add(rs.getstring("categoryname"));             }         } catch (sqlexception e) {             logger.error(string.format("exception caught while selecting category names"), e);         } {             databaseutils.close(rs);             databaseutils.close(ps);         }         return categories;     } } 

this can test junit off side. running perfectly, give reference ui code. it'll keep ui , database code separate. can use dao in application without worrying swing or web ui.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -