Results 1 to 10 of 10
  1. #1
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default What closes a ResultSet?

    I'm getting an exception in my code stating "operation not allowed after ResultSet closed, but the closing commands for my result sets are at the very end of my code, with only catch statement holding a messagebox type function coming after it.

    Makes me wonder if there's something else that would close a resultset?

    I can post my code, but bare in mind I'm just a beginner... I don't know much about optimization... :)

    Java Code:
        private void refreshStorage_btn1ActionPerformed(java.awt.event.ActionEvent evt) {
    
    
            DefaultTableModel bodyModel =(DefaultTableModel)bm1.getModel();
            DefaultTableModel neckModel =(DefaultTableModel)nm1.getModel();
            DefaultTableModel fretboardModel =(DefaultTableModel)fm1.getModel();
            DefaultTableModel markersType =(DefaultTableModel)mt1.getModel();
            DefaultTableModel bridgeType =(DefaultTableModel)bt1.getModel();
            DefaultTableModel saddleType =(DefaultTableModel)st1.getModel();
            DefaultTableModel pickupsType =(DefaultTableModel)pt1.getModel();
            DefaultTableModel tunersType =(DefaultTableModel)tt1.getModel();
    
            try{
    
                Class.forName("java.sql.Driver");
    
                Connection conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/guitar_database","root","");
    
                Statement st = conn.createStatement();
    
                String body = "Select * from body_model;";
                String neck = "Select * from neck_model;";
                String fretboard = "Select * from fretboard_model;";
                String markers = "Select * from markers_type;";
                String bridge = "Select * from bridge_type;";
                String saddle = "Select * from saddle_type;";
                String pickups = "Select * from pickups_type;";
                String tuners = "Select * from tuners_type;";
    
                ResultSet rsBody = st.executeQuery(body);
                ResultSet rsNeck = st.executeQuery(neck);
                ResultSet rsFretboard = st.executeQuery(fretboard);
                ResultSet rsMarkers = st.executeQuery(markers);
                ResultSet rsBridge = st.executeQuery(bridge);
                ResultSet rsSaddle = st.executeQuery(saddle);
                ResultSet rsPickups = st.executeQuery(pickups);
                ResultSet rsTuners = st.executeQuery(tuners);
    
                int rcBody = bodyModel.getRowCount();
                int rcNeck = neckModel.getRowCount();
                int rcFret = fretboardModel.getRowCount();
                int rcMarkers = markersType.getRowCount();
                int rcBridge = bridgeType.getRowCount();
                int rcSaddle = saddleType.getRowCount();
                int rcPickups = pickupsType.getRowCount();
                int rcTuners = tunersType.getRowCount();
    
                int i = 1;
    
                while( i <= rcBody){
                    bodyModel.removeRow(0);
                    i++;
                }
    
                i = 1;
    
                while( i <= rcNeck){
                    neckModel.removeRow(0);
                    i++;
                }
    
                i = 1;
    
                while( i <= rcFret){
                    fretboardModel.removeRow(0);
                    i++;
                }
    
                i = 1;
    
                while( i <= rcMarkers){
                    markersType.removeRow(0);
                    i++;
                }
    
                i = 1;
    
                while( i <= rcBridge){
                    bridgeType.removeRow(0);
                    i++;
                }
    
                i = 1;
    
                while( i <= rcSaddle){
                    saddleType.removeRow(0);
                    i++;
                }
    
                i = 1;
    
                while( i <= rcSaddle){
                    saddleType.removeRow(0);
                    i++;
                }
    
                i = 1;
    
                while( i <= rcPickups){
                    pickupsType.removeRow(0);
                    i++;
                }
    
                i = 1;
    
                while( i <= rcTuners){
                    tunersType.removeRow(0);
                    i++;
                }
    
    
    
    
                String d1;
                String d2;
    
                while(rsBody.next()){
    
                    d1 = rsBody.getString("Model");
                    d2 = rsBody.getString("Storage");
    
                    bodyModel.addRow(new Object[]{d1,d2});
                }
    
                while(rsNeck.next()){
    
                    d1 = rsNeck.getString("Model");
                    d2 = rsNeck.getString("Storage");
    
                    neckModel.addRow(new Object[]{d1,d2});
                }
    
                while(rsFretboard.next()){
    
                    d1 = rsFretboard.getString("Model");
                    d2 = rsFretboard.getString("Storage");
    
                    fretboardModel.addRow(new Object[]{d1,d1});
                }
    
                while(rsMarkers.next()){
    
                    d1 = rsMarkers.getString("Type");
                    d2 = rsMarkers.getString("Storage");
    
                    markersType.addRow(new Object[]{d1,d2});
                }
    
                while(rsBridge.next()){
    
                    d1 = rsBridge.getString("Type");
                    d2 = rsBridge.getString("Storage");
    
                    bridgeType.addRow(new Object[]{d1,d2});
                }
    
                while(rsSaddle.next()){
    
                    d1 = rsSaddle.getString("Type");
                    d2 = rsSaddle.getString("Storage");
    
                    saddleType.addRow(new Object[]{d1,d2});
                }
    
                while(rsPickups.next()){
    
                    d1 = rsPickups.getString("Type");
                    d2 = rsPickups.getString("Storage");
    
                    pickupsType.addRow(new Object[]{d1,d2});
                }
    
                while(rsTuners.next()){
    
                    d1 = rsTuners.getString("Type");
                    d2 = rsTuners.getString("Storage");
    
                    tunersType.addRow(new Object[]{d1,d2});
                }
    
                rsBody.close();
                rsNeck.close();
                rsFretboard.close();
                rsMarkers.close();
                rsBridge.close();
                rsSaddle.close();
                rsPickups.close();
                rsTuners.close();
                st.close();
                conn.close();
            }
    
                    catch(Exception e){
    
                    JOptionPane.showMessageDialog(this,e);
    
                    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: What closes a ResultSet?

    Read the API for the classes you are using. In particular the Statement class...and I quote

    By default, only one ResultSet object per Statement object can be open at the same time
    FWIW all those while statements can get ugly real quick - look into refactoring your code and boil them down to methods.

  3. #3
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: What closes a ResultSet?

    Ah well that explains it then. I only presumed I could open as many as I would need. Meh... Well not being able to open an individual result set for each query, what are my options?

    Could I just, use the same result set and direct a new query for it each time then?


    I'll have to see about optimizing the while's as you suggested... :)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: What closes a ResultSet?

    Not only should they be broken out, all of the above ought to be on some form of SwingWorker thread, to prevent the GUI from locking up while the stuff loads up.
    And each thing (ie SELECT) should be its own method, populating a model, even if (for the moment) that model is simply your Object[].
    The usual format would be to have these in a DAO (Data Access Object).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default More optimized?

    Hey folks thanks for the tips. I got it to run smooth, but I would like a comment on how this approach compares to the initial approach I had. Most of what I used here was first time, like use of arrays and case structure in general, as well as creating my first own fully custom methods. Here's my remade code:

    Java Code:
            DefaultTableModel bodyModel =(DefaultTableModel)bm1.getModel();
            DefaultTableModel neckModel =(DefaultTableModel)nm1.getModel();
            DefaultTableModel fretboardModel =(DefaultTableModel)fm1.getModel();
            DefaultTableModel markersType =(DefaultTableModel)mt1.getModel();
            DefaultTableModel bridgeType =(DefaultTableModel)bt1.getModel();
            DefaultTableModel saddleType =(DefaultTableModel)st1.getModel();
            DefaultTableModel pickupsType =(DefaultTableModel)pt1.getModel();
            DefaultTableModel tunersType =(DefaultTableModel)tt1.getModel();
    
            try{
    
                Class.forName("java.sql.Driver");
    
                Connection conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/guitar_database","root","");
    
                Statement st = conn.createStatement();
    
                String[]queries;
                queries = new String[8];
    
                queries[0] = "Select * from body_model;";
                queries[1] = "Select * from neck_model;";
                queries[2] = "Select * from fretboard_model;";
                queries[3] = "Select * from markers_type;";
                queries[4] = "Select * from bridge_type;";
                queries[5] = "Select * from saddle_type;";
                queries[6] = "Select * from pickups_type;";
                queries[7] = "Select * from tuners_type;";
    
                
                DefaultTableModel[]modelsArray;
                modelsArray = new DefaultTableModel[8];
                
                modelsArray[0] = bodyModel;
                modelsArray[1] = neckModel;
                modelsArray[2] = fretboardModel;
                modelsArray[3] = markersType;
                modelsArray[4] = bridgeType;
                modelsArray[5] = saddleType;
                modelsArray[6] = pickupsType;
                modelsArray[7] = tunersType;
    
    
                int i = 0;
                
                while(i < modelsArray.length){
                    int rowCount = modelsArray[i].getRowCount();
                    formatTables(rowCount, modelsArray[i]);
                    i++;
                }
    
    
                i = 0;
                int Case = 0;
    
                while(i < queries.length){
    
                    if(i == 3){
                        Case = 1;
                    }
                    
                ResultSet rs = st.executeQuery(queries[i]);
                populateTables(rs, modelsArray[i], Case);
                i++;
                }
    
                st.close();
                conn.close();
            }
    
                    catch(Exception e){
    
                    JOptionPane.showMessageDialog(this,e);
    
                    }


    and


    Java Code:
        public static DefaultTableModel formatTables(int rowCount, DefaultTableModel table) {
    
            int i = 0;
    
            while (i < rowCount){
                table.removeRow(0);
                i++;
            }
    
            return table;
            }

    and finally


    Java Code:
    public void populateTables(ResultSet rs, DefaultTableModel table, int Case ) {
                      
                String d1 = "";
                String d2 = "";
    
                try {
                    while(rs.next()){
    
                        switch(Case) {
                            case 0:
                                d1 = rs.getString("Model");
                                break;
                            case 1:
                                d1 = rs.getString("Type");
                                break;
                        }
    
                        d2 = rs.getString("Storage");
    
                        table.addRow(new Object[]{d1,d2});
                    }
    
                    
                    rs.close();
                    
                } 
                
                catch(Exception e) {
                    JOptionPane.showMessageDialog(this,e);
                }       
            }
    Last edited by MonkeyMan; 03-14-2012 at 12:18 AM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: What closes a ResultSet?

    You could skip the 'Case' part by using getXXX(int).
    eg:
    using the query
    SELECT column1, column2 FROM some_table
    'rs.getString(1)' will return the value for column1, whatever the actual name of the column is.
    Since you don't care about the column name, just change the queries and use this form of getXXX().
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: What closes a ResultSet?

    Thanks man, good pointer. Have some rep.

    I'm quite proud I managed to whip something like that together though, being such a beginner as I am (assuming it's better optimized than the initial approach). But if you folks got more tips to optimize further, keep em coming. I'm learning as we go :)

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: What closes a ResultSet?

    Java is object oriented, so I'd recommend writing in an object oriented manner. For instance, create classes that represents your data as opposed to passing string's to your table. This will help you follow a MVC type of architecture in which your data is separated away from the UI (use a DefaultTableModel class that is dependent upon instances of these classes to help even further). This might seem harder, but in the end it helps encapsulate the data and gives you a huge amount of flexibility when it comes to code maintenence and re-usability.

  9. #9
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: What closes a ResultSet?

    Aye that is precisely how I would do things, if I knew how to use such syntax. It seems I lack the experience with java to have seen enough & tried enough syntax like that. In time I hope, slow and steady. All I need is examples and at some point I can think objects :)

    On a different subject, tell me, can I loop through addEventListener?

    Java Code:
            while(listenerCounter < modelsArray.length){
            modelsArray[listenerCounter].addTableModelListener(new TableModelListener() {
                public void tableChanged(TableModelEvent e){ 
                    tableModelTableChanged(e, something to identify which listener this is);
                }
            });
            listenerCounter++;
        }
    Basically I'm trying to add event listeners to as many defaultTableModels as the modelsArray holds.
    Last edited by MonkeyMan; 03-14-2012 at 10:17 PM.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: What closes a ResultSet?

    You could pass in the counter, possibly (you'd need to assign it to a final int inside the loop otherwise the compiler will complain).
    Of course the event 'e' contains the table model affected, so it all depends on what tableModelTableChanged does.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. JFrame always closes despite of option chosen
    By ooJavaoo in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 05:28 PM
  2. Replies: 9
    Last Post: 05-05-2010, 05:33 PM
  3. Replies: 2
    Last Post: 03-23-2009, 10:02 AM
  4. [SOLVED] Application should wait until a Dialog closes
    By hannehomuth in forum Advanced Java
    Replies: 2
    Last Post: 07-24-2008, 03:51 PM
  5. Middle Click Closes Tab Eclipse Plugin 1.0.0
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 10-20-2007, 06:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •