Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    [JAVA]Strana SQLException

    Ciao a tutti , ho un'applicazione divisa in 2 pannelli , in un pannello a sinistra ho un albero dove al clik di un nodo viene lanciata una query che mostrerà poi i dati in una tabella nel pannello di destra .

    Allora , ho una prima classe che estende AbstractTableModel ,

    codice:
    class ResultSetTableModel extends AbstractTableModel {
        private ResultSet rs;
        private ResultSetMetaData rsdata;
        
        public ResultSetTableModel (ResultSet rset){
            rs = rset;
            try{
                rsdata = rs.getMetaData();
            }
            catch(SQLException sql){
                sql.printStackTrace();
            }
        }
        
        public String getColumnName(int c){
            try{
                return rsdata.getColumnName(c + 1);
            }
            catch(SQLException sql){
                sql.printStackTrace();
                return "";
            }
        }
        
        public int getColumnCount(){
            return 4;
        }
        
        public Object getValueAt(int r, int c){
            try{
                rs.absolute(r + 1);
                return rs.getObject(c + 1);
            }
            catch(SQLException sql){
                sql.printStackTrace();
                return null;
            }
        }
        
        public int getRowCount(){
            try{
                rs.last();
                return rs.getRow();
            }
            catch(SQLException sql){
                sql.printStackTrace();
                return 0;
            }
        }
    }
    e poi la classe che estende JPanel dove è presente la jtable

    codice:
    public class TablePanel extends JPanel {
        private ResultSetTableModel rstm;
        /** Creates new form TablePanel */
        public TablePanel(String s) {
            try{
                Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
                Connection c = DriverManager.getConnection("jdbc:derby:maildb");
                String sql = "select mail.m_from, mail.m_oggetto, mail.m_data, m_read, cartelle.* " +
                        "from cartelle inner join mail on cartelle.id_cartella = mail.id_cartella where " +
                        "cartelle.nome = '"+ s +"'";
                Statement stat = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
                ResultSet rs = stat.executeQuery(sql);
                rstm = new ResultSetTableModel(rs);
                rs.close();
                stat.close();
                c.close();
            }
            catch(ClassNotFoundException cnf){}
            catch(SQLException sql){
                while(sql != null){
                    sql.printStackTrace();
                    sql = sql.getNextException();
                }            
            }
            initComponents();
        }
    
        
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
        private void initComponents() {
            jScrollPane1 = new javax.swing.JScrollPane();
            table = new javax.swing.JTable();
    
            table.setModel(rstm);
            jScrollPane1.setViewportView(table);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
            );
        }// </editor-fold>                        
        
        
                            
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTable table;
        // End of variables declaration                   
    }
    Come db sto usando apache derby embedded . Al clik sul nodo però mi viene fuori un'eccezzione che dice :
    java.sql.SQLException: ResultSet not open. Operation 'last()' not permitted. Verify that autocommit is OFF.

    Ma non sembrerebbe essere un problema di legato al metodo last() , perchè se lo commento mi da lo stesso errore sul metodo getRow(), più tosto non capisco la voce "Verify that autocommit is OFF."
    Qualcuno sa aiutarmi ?
    Ciao e grazie
    Leonardo -

    Se vuoi vedere il sito ufficiale del paese di Barrali vai su www.barrali.net
    Guarda il video di Satch Boogie

  2. #2
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    906
    Problema di persistenza dei dati. Tu fai la query, crei il tuo ResultSetTableModel e metti il ResultSet come variabile, ma poi nel main chiudi il ResultSet 2 righe sotto. Nel momento in cui il ResultSet viene chiuso, perde tutti i dati perche' quest'oggetto lavoro sul database. quando dentro initComponent, vai ad applicare questo model alla JTable, chiama i vari metodi di ResultSetTableModel che usano il ResultSet (andato perso) e ti solleva eccezioni. Non salvare il ResultSet nel costruttore, ma piuttosto usa una struttura dati nella quale salvare le voci dei record (banalmente puo' essere una String[][]). Stesso discorso per i metadati.

  3. #3
    Ok grazie mille, stavo vedendo anche l'interfacia CachedRowSet , dovrebbe far si che il ResultSet vanga mantenuto in memoria , anche dopo la chiusura , giusto ?
    Potrebbe fare al caso mio ?
    Leonardo -

    Se vuoi vedere il sito ufficiale del paese di Barrali vai su www.barrali.net
    Guarda il video di Satch Boogie

  4. #4
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    906
    Non l'ho mai usata, ma leggendo velocemente le specifiche, immagino di si.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.