2016-08-08 7 views
1

4つのComboBoxがある小切手印刷アプリケーションを作成しようとしています。それらのすべてがoracleデータベースからデータを取得しています。 ComboBox1はすべての銀行の名前を取得し、ComboBox2は選択した銀行のすべてのアカウントを取得します。 ComboBox3は範囲のすべてのfrom_cheque番号を取得し、最後の範囲は範囲のすべてのto_cheque番号を取得します。
問題は、アカウント番号を選択したときです。 "java.sql.SQLException:Closed Resultset:next"という例外が発生し、複数の行がデータベースに格納されている間にComboBox3に1つの行しか持ち込まれず、最後のComboBoxに結果が表示されません。コードは以下のとおりです。java.sql.SQLException:Closed Resultset:ComboBoxから項目を選択するとき

package frame; 

import java.beans.Statement; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import javax.swing.*; 
import java.io.*; 
import java.sql.SQLException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* 
* @author talha 
*/ 
public class print_cheque extends javax.swing.JFrame { 

    Connection con = null; 
    java.sql.Statement st = null; 
    java.sql.Statement st1 = null; 
    java.sql.Statement st2 = null; 
    java.sql.Statement st3 = null; 

    /** 
    * Creates new form print_cheque 
    */ 
    public print_cheque() { 
     initComponents(); 

     //Statement st = null; 
     String query = "Select bnk_nm from d_bank_mst"; 

     try { 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      con = DriverManager.getConnection("jdbc:oracle:thin:@191.1.1.220:1521:abc10g", "payablek", "payablek"); 
      System.out.println("connection successful"); 

      st = con.createStatement(); 
      ResultSet rs = st.executeQuery(query); 
      int itemCount = jComboBox1.getItemCount(); 
      jComboBox1.addItem("--Select Company--"); 
      for (int i = 0; i < itemCount; i++) { 
       jComboBox1.removeItemAt(0); 
      } 

      while (rs.next()) { 
       String bank = rs.getString(1); 
       jComboBox1.addItem(bank); 
      } 

      //JOptionPane.showMessageDialog(this, i); 
      //con.close(); 
      st.close(); 
      rs.close(); 

     } catch (Exception e) { 
      JOptionPane.showMessageDialog(this, e); 
     } 

    } 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jComboBox1 = new javax.swing.JComboBox<>(); 
     jLabel1 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 
     jComboBox2 = new javax.swing.JComboBox<>(); 
     jLabel3 = new javax.swing.JLabel(); 
     jLabel4 = new javax.swing.JLabel(); 
     jRadioButton1 = new javax.swing.JRadioButton(); 
     jComboBox4 = new javax.swing.JComboBox<>(); 
     jComboBox6 = new javax.swing.JComboBox<>(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jComboBox1.setToolTipText(""); 
     jComboBox1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jComboBox1ActionPerformed(evt); 
      } 
     }); 

     jLabel1.setText("Bank Name"); 

     jLabel2.setText("Account No"); 

     jComboBox2.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jComboBox2ActionPerformed(evt); 
      } 
     }); 

     jLabel3.setText("From Cheque No"); 

     jLabel4.setText("To Cheque No"); 

     jRadioButton1.setText("Cash"); 
     jRadioButton1.addActionListener(new java.awt.event.ActionListener() { 


    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
     //JOptionPane.showConfirmDialog(null, "ello");  // TODO add your handling code here: 
     JComboBox cb = (JComboBox) evt.getSource(); 
     int itemCount = jComboBox2.getItemCount(); 
     String banknm = (String) cb.getSelectedItem();// = "'%Sindh Bank Ltd%'"; 
     JOptionPane.showMessageDialog(null, banknm); 
     jComboBox2.addItem("--Select Account--"); 
     try { 
      st1 = con.createStatement(); 
     } catch (Exception ex) { 
      Logger.getLogger(print_cheque.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     JOptionPane.showMessageDialog(null, 1); 
     ResultSet rs2 = null; 

     String query2 = "Select distinct c.ac_no " 
       + "from d_chq_mst c, d_bank_mst d " 
       + "where d.bnk_nm like '%" + banknm + "%'" 
       + "and c.bnk_cd= d.bnk_cd ";//" + banknm + " 
     try { 
      JOptionPane.showMessageDialog(null, 2); 
      //int itemCount = jComboBox2.getItemCount(); 
      for (int i = 0; i < itemCount; i++) { 
       jComboBox2.removeItemAt(1); 
      } 
      rs2 = st1.executeQuery(query2); 
      JOptionPane.showMessageDialog(null, 3); 
      while (rs2.next()) { 
       try { 
        //JOptionPane.showMessageDialog(null, 4); 
        jComboBox2.addItem(rs2.getString(1)); 
       } catch (SQLException ex) { 
        Logger.getLogger(print_cheque.class.getName()).log(Level.SEVERE, null, ex); 
       } 
      } 
      st1.close(); 
      rs2.close(); 
     } catch (SQLException ex) { 
      Logger.getLogger(print_cheque.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    }           

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {            
     // TODO add your handling code here: 
    }            

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
     //int t = 0; 
     JComboBox cb1 = (JComboBox) evt.getSource(); 
     jComboBox4.addItem("--Select From Cheque Number--"); 
     String acnt = (String) cb1.getSelectedItem(); 
     /*if((t == 0)&&!(jComboBox2.equals(null))){ 
      cb1.setSelectedItem(null); 
     }*/ 

     JOptionPane.showMessageDialog(null, acnt); 

     try { 
      st2 = con.createStatement(); 
     } catch (Exception ex) { 
      //Logger.getLogger(print_cheque.class.getName()).log(Level.SEVERE, null, ex); 
      ex.printStackTrace(); 
     } 
     ResultSet rs3 = null; 
     String query3 = "select distinct d.chq_from " 
       + "from d_chq_mst d, d_chq_dtl e " 
       + " where e.ac_no= '" + acnt + "'" 
       + " and d.ac_no = e.ac_no "; 
     try { 
      rs3 = st2.executeQuery(query3); 
      int itemCount2 = jComboBox4.getItemCount(); 
      /*for (int i = 0; i < itemCount2; i++) { 
       //jComboBox4.removeItemAt(0); 
       JOptionPane.showMessageDialog(this, i); 
      }*/ 
      while (rs3.next()) { 
       try { 
        JOptionPane.showMessageDialog(null, "item Count**" + itemCount2); 
        jComboBox4.addItem(rs3.getString(1)); 
       } catch (SQLException ex) { 
        //Logger.getLogger(print_cheque.class.getName()).log(Level.SEVERE, null, ex); 
        ex.printStackTrace(); 
       } 

       st2.close(); 
       rs3.close(); 

      } 
     } catch (Exception ex) { 
      //Logger.getLogger(print_cheque.class.getName()).log(Level.SEVERE, null, ex); 
      ex.printStackTrace(); 
     } 
     //t++; 

    }           

    private void jComboBox4ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
     jComboBox6.addItem("--Select To Cheque Number--"); 
     JComboBox cb2 = (JComboBox) evt.getSource(); 

     String account = (String) cb2.getSelectedItem(); 

     //JOptionPane.showMessageDialog(null, acnt); 
     try { 
      st3 = con.createStatement(); 
     } catch (Exception ex) { 
      //Logger.getLogger(print_cheque.class.getName()).log(Level.SEVERE, null, ex); 
      ex.printStackTrace(); 
     } 
     ResultSet rs4 = null; 
     String query3 = "select distinct d.chq_to " 
       + "from d_chq_mst d, d_chq_dtl e " 
       + "where d.ac_no= '" + account + "'" 
       + "and d.ac_no= '" + account + "'"; 

     //+ "where d.ac_no = '" + acnt + "'"; 
     try { 
      rs4 = st3.executeQuery(query3); 
      int itemCount3 = jComboBox6.getItemCount(); 
      /*for (int i = 0; i < itemCount3; i++) { 
       jComboBox6.removeItemAt(0); 
      }*/ 
      while (rs4.next()) { 
       try { 
        JOptionPane.showMessageDialog(null, rs4.getString(1)); 
        jComboBox6.addItem(rs4.getString(1)); 
       } catch (SQLException ex) { 
        Logger.getLogger(print_cheque.class.getName()).log(Level.SEVERE, null, ex); 
        ex.printStackTrace(); 
       } 

       st3.close(); 
       rs4.close(); 

      } 

     } catch (Exception ex) { 
      //Logger.getLogger(print_cheque.class.getName()).log(Level.SEVERE, null, ex); 
      ex.printStackTrace(); 
     } 
    }           

    private void jComboBox6ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
    } 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(print_cheque.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(print_cheque.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(print_cheque.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(print_cheque.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new print_cheque().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify      
    private javax.swing.JComboBox<String> jComboBox1; 
    private javax.swing.JComboBox<String> jComboBox2; 
    private javax.swing.JComboBox<String> jComboBox4; 
    private javax.swing.JComboBox<String> jComboBox6; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JLabel jLabel4; 
    private javax.swing.JRadioButton jRadioButton1; 
    // End of variables declaration     
} 

ここにスタックトレースがあります。

実行:接続に成功します。java.sql.SQLException:定休結果セット: 次 oracle.jdbc.driver.InsensitiveScrollableResultSet.next(InsensitiveScrollableResultSet.java:558)で frame.print_cheque.jComboBox2ActionPerformed(print_chequeで。 java:262) at frame.print_cheque.access $ 100(print_cheque.java:23) frame.print_cheque $ 2.actionPerformed(print_cheque.java:103)at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1260) で、 にあるjavax.swing.JComboBox.setSelectedItem(JComboBox.java:588)javax.swing.JComboBox.setSelectedIndex(JCom javax.swing.plaf.basic.BasicComboBoxUI $ Actions.actionPerformed(BasicComboBoxUI.java:1528) で javax.swing.plaf.basic.BasicComboBoxUI.selectNextPossibleValue(BasicComboBoxUI.java:1124) でboBox.java:624) のjavaxでjavax.swing.JComboBox.processKeyBindingでjavax.swing.JComponent.processKeyBinding(JComponent.java:2879)(JComboBox.java:1425)でjavax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1662) で.swing.JComponent.processKeyBindings(JComponent.java:2940)at にあるjavax.swing.JComponent.processKeyEvent(JComponent.java:2842)javax.swing.JComboBox.processKeyEvent(JComboBox.java:1417)at java.awt .Component.processEvent(Component.java:6293)at java.awt.Container.processEvent(Container.java:2229)at java.awt.Component.dispatchEventImpl(Component.java:4872)at java.awt.Container.dispatchEventImpl(Container.java:2287)at java ジャワで.awt.Component.dispatchEvent java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762)で java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1887)で(Component.java:4698) .awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027) を に設定します。java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899) をに設定します。java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727) (java.awt.Component.dispatchEventImpl(Component.java:4742)at )java.awt.Container.dispatchEventImpl(Container.java:2287)at java .awt.Window.dispatchEventImpl(Window.java:2719)at java.awt.Component.dispatchEvent(Component.java:4698)at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)at java.awt .EventQueue.access $ 300(EventQueue.java:103)at java.awt.EventQueue $ 3.run(EventQueue.java:706)at java.awt.EventQueue $ 3.run(EventQueue。Javaの:704) すべてjava.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.javaで すべてjava.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76) で java.security.AccessController.doPrivileged(ネイティブメソッド)で: 8735) at java.awt.EventQueue $ 4.run(EventQueue.java:720)at java.awt.EventQueue $ 4.run(EventQueue.java:718)at java.security.AccessController.doPrivileged(ネイティブメソッド)at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) でのjava.awt.EventQueue.dispatchEventで すべてjava.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76) (EventQueue.java:717) java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) でjava.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)で java.awt.EventDispatchThread.runで java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) (EventDispatchThread.java:91)

+0

try-with-resourcesステートメントを使用して検討することもでき'while(rs3.next()){'とループ内の各ステートメントから何が戻ってくるかを見てください。これは問題を指摘するかもしれません。 – jr593

+0

これは関連していない可能性がありますが、近い順序が間違っています。最初に結果セットを閉じ、次にステートメントを終了し、最後に接続が必要ない場合は最後に終了します。言い換えれば、クローズシーケンスは逆順のシーケンスを作成する必要があります –

+0

質問にスタックトレースを追加してください –

答えて

3

であなたのバグはここにある:

while (rs3.next()) { 
    ... 
    st2.close(); 
    rs3.close(); 
} 

あなたのResultSet NB

whileループではなく、ループの外閉じる:あなたの近くのシーケンスは、あなたがもはや必要でない場合は、最初の結果セット、文、最終的に接続を閉じることになっている間違っています。言い換えれば、クローズシーケンスは逆生成シーケンスでなければなりません。

あなたのコードは、むしろそのような何かでなければなりません

try { 
    ... 
} catch (Exception ex) { 
    //Logger.getLogger(print_cheque.class.getName()).log(Level.SEVERE, null, ex); 
    ex.printStackTrace(); 
} finally { 
    // Close first the result set 
    if (rs4 != null) { 
     rs4.close(); 
    } 
    // The close your statement 
    if (st3 != null) { 
     st3.close(); 
    } 
} 

は、あなたは、Java 7以降を使用すると仮定すると、あなたは私がデバッグモードで実行し、ブレークポイントを設定することをお勧め

関連する問題