2012-09-03 80 views
0

在我使用Java Swing的獨立應用程序中,我使用ResultSet遍歷Excel表單(後端數據)。但是在執行插入查詢之後,當我嘗試再次使用ResultSet遍歷記錄時,它顯示'ResultSet已關閉'。之後無法執行任何操作。
什麼是錯誤?我找不到它。請告訴我。我是一個新手..所以請簡單解釋一下。我很感激你的幫助。謝謝:)java中resultset被關閉錯誤

public sowui(String s) 
{ 


    try { 
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); 
     con=DriverManager.getConnection("jdbc:odbc:test"); 
     Statement stmt = con.createStatement(); 
     stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
     String sql ="SELECT * FROM [Sheet1$] "; 

     rs =stmt.executeQuery(sql); 
    } catch (SQLException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (InstantiationException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ClassNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    setTitle("MANAGEMENT OF STATEMENT OF WORKS"); 
    setLayout(null); 
    l1=new JLabel("CONTRACT REFERENCE NUMBER"); 
    t1=new JTextField(20); 


    l1.setBounds(50,100,250,50); 
    t1.setBounds(300,110,135,30); 
    t1.addKeyListener(new KeyAdapter() { 
     public void keyTyped(KeyEvent e) { 
      String input=t1.getText(); 
      Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^]"); 
      Matcher m = p.matcher(input); 
      if (m.find()) { 
       t1.setText(null); 
       JOptionPane.showMessageDialog(null, "Please enter only numbers"); 
      } 
     } });  
    l1m=new JLabel("*"); 
    l1m.setBounds(260,100,10,50); 
    l1m.setForeground(Color.RED); 

    l2=new JLabel("CR NUMBER"); 
    final String [] crn = {"N/A"}; 
    c6 =new JComboBox(crn); 
    c6.setEditable(true); 
    c6.addActionListener(new ActionListener() {    

     public void actionPerformed(ActionEvent e) {     
      if (e.getActionCommand().equals("comboBoxEdited")) {     
       System.out.println("Adding new player!");     
       c6.insertItemAt(c6.getSelectedItem(),0);    
      }    
     }   
    }); 

    l2.setBounds(50,150,250,50); 
    c6.setBounds(300,160,135,30); 

    l2m=new JLabel("*"); 
    l2m.setBounds(130,150,10,50); 
    l2m.setForeground(Color.RED); 

    l3=new JLabel("SOW NAME"); 
    t3=new JTextField(20); 
    t3.addKeyListener(new KeyAdapter() 
    { 
     public void keyTyped(KeyEvent e) { 
      String input=t3.getText(); 
      Pattern p = Pattern.compile("[0-9]"); 
      Matcher m = p.matcher(input); 
      if (m.find()) 
      { 
       t3.setText(null); 
       JOptionPane.showMessageDialog(null,"Enter only characters A-Z or a-z"); 

      } 
     } 
    }); 

    l3.setBounds(50,200,250,50); 
    t3.setBounds(300,210,135,30); 

    l3m=new JLabel("*"); 
    l3m.setBounds(120,200,10,50); 
    l3m.setForeground(Color.RED); 

    l4=new JLabel("PARTNER SERVICE MANAGER"); 
    t4=new JTextField(20); 
    t4.addKeyListener(new KeyAdapter() 
    { 
     public void keyTyped(KeyEvent e) { 
      String input=t4.getText(); 
      Pattern p = Pattern.compile("[0-9]"); 
      Matcher m = p.matcher(input); 
      if (m.find()) 
      { 
       t4.setText(null); 
       JOptionPane.showMessageDialog(null,"Enter only characters A-Z or a-z"); 

      } 
     } 
    }); 

    l4.setBounds(50,250,250,50); 
    t4.setBounds(300,260,135,30); 

    l4m=new JLabel("*"); 
    l4m.setBounds(230,250,10,50); 
    l4m.setForeground(Color.RED); 


    l5=new JLabel("LBG SERVICE MANAGER"); 
    t5=new JTextField(20); 
    t5.addKeyListener(new KeyAdapter() 
    { 
     public void keyTyped(KeyEvent e) { 
      String input=t5.getText(); 
      Pattern p = Pattern.compile("[0-9]"); 
      Matcher m = p.matcher(input); 
      if (m.find()) 
      { 
       t5.setText(null); 
       JOptionPane.showMessageDialog(null,"Enter only characters A-Z or a-z"); 

      } 
     } 
    }); 

    l5.setBounds(50,300,250,50); 
    t5.setBounds(300,310,135,30); 

    l5m=new JLabel("*"); 
    l5m.setBounds(200,300,10,50); 
    l5m.setForeground(Color.RED); 


    l6=new JLabel("TYPE OF CONTRACT"); 
    final String [] tct = {"SOW"}; 
    c4 =new JComboBox(tct); 
    c4.setEditable(true); 
    c4.addActionListener(new ActionListener() {    

     public void actionPerformed(ActionEvent e) {     
      if (e.getActionCommand().equals("comboBoxEdited")) {     
       System.out.println("Adding new player!");     
       c4.insertItemAt(c4.getSelectedItem(),0);    
      }    
     }   
    }); 

    l6.setBounds(50,350,250,50); 
    c4.setBounds(300,360,135,30); 

    l6m=new JLabel("*"); 
    l6m.setBounds(180,350,10,50); 
    l6m.setForeground(Color.RED); 

    l7=new JLabel("VALUE OF CONTRACT"); 
    t7=new JTextField(20); 
    t7.addKeyListener(new KeyAdapter() { 
     public void keyTyped(KeyEvent e) { 
      String input=t7.getText(); 
      Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^]"); 
      Matcher m = p.matcher(input); 
      if (m.find()) { 
       t7.setText(null); 
       JOptionPane.showMessageDialog(null, "Please enter only numbers"); 
      } 
     } });     
    l7.setBounds(50,400,250,50); 
    t7.setBounds(300,410,135,30); 

    l7m=new JLabel("*"); 
    l7m.setBounds(190,400,10,50); 
    l7m.setForeground(Color.RED); 

    l8=new JLabel("REMARKS"); 
    t8=new TextArea(15,40); 
    l8.setBounds(50,450,250,50); 
    t8.setBounds(300,460,200,80); 

    l9=new JLabel("WORK STATUS"); 
    final String [] workstatus = {"Live","Pipeline","Working at Risk"}; 
    c1 =new JComboBox(workstatus); 
    c1.setEditable(true); 
    c1.addActionListener(new ActionListener() {    

     public void actionPerformed(ActionEvent e) {     
      if (e.getActionCommand().equals("comboBoxEdited")) {     
       System.out.println("Adding new player!");     
       c1.insertItemAt(c1.getSelectedItem(),0);    
      }    
     }   
    }); 

    l9.setBounds(500,100,250,50); 
    c1.setBounds(750,110,135,30); 

    l9m=new JLabel("*"); 
    l9m.setBounds(600,100,10,50); 
    l9m.setForeground(Color.RED); 

    l10=new JLabel("CONTRACT APPROVAL"); 
    final String [] cal = {"Fully signed off","WithPlatform","with partner for signoff"}; 
    c2 =new JComboBox(cal); 
    c2.setEditable(true); 
    c2.addActionListener(new ActionListener() {    

     public void actionPerformed(ActionEvent e) {     
      if (e.getActionCommand().equals("comboBoxEdited")) {     
       System.out.println("Adding new player!");     
       c2.insertItemAt(c2.getSelectedItem(),0);    
      }    
     }   
    }); 
    l10.setBounds(500,150,250,50); 
    c2.setBounds(750,160,135,30); 

    l10m=new JLabel("*"); 
    l10m.setBounds(640,150,10,50); 
    l10m.setForeground(Color.RED); 


    l11=new JLabel("PLATFORM"); 
    final String [] PF = {"ADM-WPM","ADM-ALIS","ADM-SWeC","ADM-PAS","ADM-BSM","ADM-RAS","ADM-LP&I"}; 
    c5 =new JComboBox(PF); 
    c5.setEditable(true); 
    c5.addActionListener(new ActionListener() {    

     public void actionPerformed(ActionEvent e) {     
      if (e.getActionCommand().equals("comboBoxEdited")) {     
       System.out.println("Adding new player!");     
       c5.insertItemAt(c5.getSelectedItem(),0);    
      }    
     }   
    }); 
    l11.setBounds(500,200,250,50); 
    c5.setBounds(750,210,135,30); 

    l11m=new JLabel("*"); 
    l11m.setBounds(570,200,10,50); 
    l11m.setForeground(Color.RED); 


    l12=new JLabel("COMMERCIAL MODEL"); 
    final String [] cml = {"T&M","FP","PSA"}; 
    c3 =new JComboBox(cml); 
    c3.setEditable(true); 
    c3.addActionListener(new ActionListener() {    

     public void actionPerformed(ActionEvent e) {     
      if (e.getActionCommand().equals("comboBoxEdited")) {     
       System.out.println("Adding new player!");     
       c3.insertItemAt(c3.getSelectedItem(),0);    
      }    
     }   
    }); 
    l12.setBounds(500,250,250,50); 
    c3.setBounds(750,260,135,30); 

    l12m=new JLabel("*"); 
    l12m.setBounds(630,250,10,50); 
    l12m.setForeground(Color.RED); 

    l13=new JLabel("START DATE"); 
    t13=new JTextField(20); 
    l13.setBounds(500,300,250,50); 
    t13.setBounds(750,310,135,30); 
    t13.setEditable(false); 

    l13m=new JLabel("*"); 
    l13m.setBounds(580,300,10,50); 
    l13m.setForeground(Color.RED); 



    l14=new JLabel("END DATE"); 
    t14=new JTextField(20); 
    l14.setBounds(500,350,250,50); 
    t14.setBounds(750,360,135,30); 
    t14.setEditable(false); 

    l14m=new JLabel("*"); 
    l14m.setBounds(570,350,10,50); 
    l14m.setForeground(Color.RED); 


    l15=new JLabel("CUMULATIVE VALUE OF CONTRACT"); 
    t15=new JTextField(20); 
    t15.addKeyListener(new KeyAdapter() { 
     public void keyTyped(KeyEvent e) { 
      String input=t15.getText(); 
      Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^]"); 
      Matcher m = p.matcher(input); 
      if (m.find()) { 
       t15.setText(null); 
       JOptionPane.showMessageDialog(null, "Please enter only numbers"); 
      } 
     }});  

    l15.setBounds(500,400,250,50); 
    t15.setBounds(750,410,135,30); 

    l15m=new JLabel("*"); 
    l15m.setBounds(710,400,10,50); 
    l15m.setForeground(Color.RED); 

    l16=new JLabel("SOW TRACKER"); 
    l16.setBounds(400,50,250,50); 
    l16.setFont(new Font("TIMES NEW ROMAN", Font.BOLD, 28)); 


    firstb=new JButton("FIRST"); 
    firstb.setBounds(200,600,100,40); 

    prev=new JButton("PREVIOUS"); 
    prev.setBounds(400,600,100,40); 

    nextb=new JButton("NEXT"); 
    nextb.setBounds(600,600,100,40); 

    lastb=new JButton("LAST"); 
    lastb.setBounds(800,600,100,40); 

    newb=new JButton("NEW"); 
    newb.setBounds(200,650,100,40); 

    save=new JButton("SAVE"); 
    save.setBounds(400,650,100,40); 

    exitb = new JButton("EXIT"); 
    exitb.setBounds(600, 650,100,40); 

    searchb=new JButton("SEARCH"); 
    searchb.setBounds(800,650,100,40); 

    b9=new JButton(".."); 
    b9.setBounds(900,315,20,20); 

    b10=new JButton(".."); 
    b10.setBounds(900,365,20,20); 



    add(l1); 
    add(t1); 
    add(l1m); 

    add(l2); 
    add(c6); 
    add(l2m); 

    add(l3); 
    add(t3); 
    add(l3m); 

    add(l4); 
    add(t4); 
    add(l4m); 

    add(l5); 
    add(t5); 
    add(l5m); 

    add(l6); 
    add(c4); 
    add(l6m); 

    add(l7); 
    add(t7); 
    add(l7m); 

    add(l8); 
    add(t8); 

    add(l9); 
    add(c1); 
    add(l9m); 


    add(l10); 
    add(c2); 
    add(l10m); 

    add(l11); 
    add(c5); 
    add(l11m); 

    add(l12); 
    add(c3); 
    add(l12m); 

    add(l13); 
    add(t13); 
    add(l13m); 

    add(l14); 
    add(t14); 
    add(l14m); 

    add(l15); 
    add(t15); 
    add(l15m); 

    add(l16); 


    add(firstb); 
    firstb.addActionListener(this); 

    add(prev); 
    prev.addActionListener(this); 

    add(nextb); 
    nextb.addActionListener(this); 

    add(lastb); 
    lastb.addActionListener(this); 

    add(newb); 
    newb.addActionListener(this); 

    add(save); 
    save.addActionListener(this); 

    add(exitb); 
    exitb.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e) 
     { 
      System.exit(0); 
     } 
    }); 

    add(searchb); 
    searchb.addActionListener(this); 

    add(b9); 
    b9.addActionListener(this); 

    add(b10); 
    b10.addActionListener(this); 









    setSize(1000,1000); 
    setVisible(true); 
    setResizable(false); 


} 








public void actionPerformed(ActionEvent ae) 
{ 

    if(ae.getSource()==save) 
    { 


     if(t1.getText().length()==0) { try{ 

      JOptionPane.showMessageDialog(null, "Enter CONTRACT REFERENCE NUMBER"); }catch(Exception e){ 

       JOptionPane.showMessageDialog(null, "JTextField is " + t1.getText()); }} 


     /*else if(t2.getText().length()==0) { try{ 

JOptionPane.showMessageDialog(null, "Enter CR NUMBER"); }catch(Exception e){ 

     JOptionPane.showMessageDialog(null, "JTextField is " + t2.getText()); }}*/ 

     else if(t3.getText().length()==0) { try{ 

      JOptionPane.showMessageDialog(null, "enter text in JTextField-SOW NAME"); }catch(Exception e){ 

       JOptionPane.showMessageDialog(null, "JTextField is " + t3.getText()); }} 

     else if(t4.getText().length()==0) { try{ 

      JOptionPane.showMessageDialog(null, "enter text in JTextField-PARTNER SERVICE MANAGER"); }catch(Exception e){ 

       JOptionPane.showMessageDialog(null, "JTextField is " + t4.getText()); }} 

     else if(t5.getText().length()==0) { try{ 

      JOptionPane.showMessageDialog(null, "enter text in JTextField-LBG SERVICE MANAGER"); }catch(Exception e){ 

       JOptionPane.showMessageDialog(null, "JTextField is " + t5.getText()); }} 

     else if(t7.getText().length()==0) { try{ 

      JOptionPane.showMessageDialog(null, "Enter VALUE OF CONTRACT"); }catch(Exception e){ 

       JOptionPane.showMessageDialog(null, "JTextField is " + t7.getText()); }} 

     /*else if(t11.getText().length()==0) { try{ 

JOptionPane.showMessageDialog(null, "enter text in JTextField-PLATFORM"); }catch(Exception e){ 

     JOptionPane.showMessageDialog(null, "JTextField is " + t11.getText()); }} 

else if(t12.getText().length()==0) { try{ 

JOptionPane.showMessageDialog(null, "enter text in JTextField-COMMERCIAL MODEL"); }catch(Exception e){ 

     JOptionPane.showMessageDialog(null, "JTextField is " + t12.getText()); }}*/ 

     else if(t13.getText().length()==0) { try{ 

      JOptionPane.showMessageDialog(null, "Enter START DATE"); }catch(Exception e){ 

       JOptionPane.showMessageDialog(null, "JTextField is " + t13.getText()); }} 

     else if(t14.getText().length()==0) { try{ 

      JOptionPane.showMessageDialog(null, "Enter END DATE"); }catch(Exception e){ 

       JOptionPane.showMessageDialog(null, "JTextField is " + t14.getText()); }} 

     else if(t15.getText().length()==0) { try{ 

      JOptionPane.showMessageDialog(null, "Enter-CUMULATIVE VALUE OF CONTRACT"); }catch(Exception e){ 

       JOptionPane.showMessageDialog(null, "JTextField is " + t15.getText()); }} 


     String str1=t13.getText(); 
     String str2=t14.getText(); 
     SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy"); 

     Date date1 = null; 
     try { 
      date1 = (Date) df.parse(str1); 
     } catch (ParseException e) { 

     } 

     Date date2 = null; 
     try { 
      date2 = (Date) df.parse(str2); 
     } catch (ParseException e) { 

     } 

     if (date1.after(date2)) try{ 
      JOptionPane.showMessageDialog(null, "Start Date must be earlier than the end date"); 


     }catch(Exception e){} 

     if(ae.getSource()==save) 
     { 
      try 
      { 

       String value1=t1.getText(); 

       String value2=(String)c6.getSelectedItem(); 

       String value3=t3.getText(); 

       String value4=t4.getText(); 

       String value5=t5.getText(); 

       String value6 = (String) c4.getSelectedItem(); 

       String value7=t7.getText(); 

       String value8=t8.getText(); 

       String value9 = (String) c1.getSelectedItem(); 

       String value10 = (String) c2.getSelectedItem(); 

       String value11 = (String) c5.getSelectedItem(); 

       String value12 = (String) c3.getSelectedItem(); 

       String value13=t13.getText(); 

       String value14=t14.getText(); 

       String value15=t15.getText(); 


       Statement st=con.createStatement(); 
       System.out.println(value1); 
       // String q = "insert into [sheet1$] (CRN,CR_NUM,SOW_N,PSM,LBG,T_CONTRACT,V_CONTRACT,REMARKS,STATUS,APPROVAL,PLATFORM,MODEL,START_D,END_D,CUM_VALUE) values ('12345','"+value2+"','"+value3+"','"+value4+"','"+value5+"','"+value6+"','"+value7+"','"+value8+"','"+value9+"','"+value10+"','"+value11+"','"+value12+"','"+value13+"','"+value14+"','"+value15+"')"; 
       String q = "insert into [sheet1$] values ('"+value1+"','"+value2+"','"+value3+"','"+value4+"','"+value5+"','"+value6+"','"+value7+"','"+value8+"','"+value9+"','"+value10+"','"+value11+"','"+value12+"','"+value13+"','"+value14+"','"+value15+"')"; 
       int ds = st.executeUpdate(q); 

       System.out.println("Inserted Sucessfully...!!!"); 
       System.out.println(ds); 




      }catch(Exception e) 
      { 
       System.out.println(e.getMessage()); 
      } 

      finally{ 
       try { 
        con.close(); 
       } catch (SQLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 

    } 
    else if(ae.getSource()==newb) 
    { 
     try{ 

      t1.setText(null); 
      c6.addItem(null); 
      t3.setText(null); 
      t4.setText(null); 
      t5.setText(null); 
      c4.addItem(null); 
      t7.setText(null); 
      t8.setText(null); 
      c1.addItem(null); 
      c2.addItem(null); 
      c5.addItem(null); 
      c3.addItem(null); 
      t13.setText(null); 
      t14.setText(null); 
      t15.setText(null); 

     }catch(Exception e){} 

    } 
    else if(ae.getSource()==firstb) 
    { 
     try { 
      rs.first(); 
      int id_col = rs.getInt("CRN"); 

      String id = Integer.toString(id_col); 
      //int id_col1 = rs.getInt("CR_NUM"); 
      //String cr= Integer.toString(id_col1); 
      String cr =rs.getString("CR_NUM"); 
      String sow = rs.getString("SOW_N"); 
      String psm = rs.getString("PSM"); 
      String lbg = rs.getString("LBG"); 
      String toc = rs.getString("T_CONTRACT"); 
      String rm = rs.getString("REMARKS"); 

      String wks = rs.getString("WORK_ST"); 

      String cap = rs.getString("APPROVAL"); 
      String pfm = rs.getString("PLATFORM"); 
      String cmm = rs.getString("MODEL"); 

      String std = rs.getString("START_D"); 
      String etd= rs.getString("END_D"); 

      int id_col2 = rs.getInt("V_CONTRACT"); 
      String voc= Integer.toString(id_col2); 


      int id_col3 = rs.getInt("CUM_VALUE"); 
      String cvc = Integer.toString(id_col3); 
      t1.setText(id); 
      //c6.addItem(cr); 
      t3.setText(sow); 
      t4.setText(psm); 
      t5.setText(lbg); 
      //c4.addItem(toc); 
      t7.setText(voc); 
      t8.setText(rm); 
      c1.setSelectedItem(wks); 
      c2.setSelectedItem(cap); 
      c5.setSelectedItem(pfm); 
      c3.setSelectedItem(cmm); 
      c4.setSelectedItem(toc); 
      c6.setSelectedItem(cr); 



      //c1.addItem(wks); 
      //c2.addItem(cap); 
      //c5.addItem(pfm); 
      //c3.addItem(cmm); 
      t13.setText(std); 
      t14.setText(etd); 
      t15.setText(cvc); 

      System.out.println(wks); 


     } 
     catch (SQLException err) { 
      JOptionPane.showMessageDialog(sowui.this, err.getMessage()); 
     } 
     try{ 
     stmt.close(); 
     }catch(SQLException a) {} 
    } 

    else if(ae.getSource()==nextb) 
    { 
     try { 
      if (rs.next()) { 
       int id_col = rs.getInt("CRN"); 

       String id = Integer.toString(id_col); 
       //int id_col1 = rs.getInt("CR_NUM"); 
       //String cr= Integer.toString(id_col1); 
       String cr =rs.getString("CR_NUM"); 
       String sow = rs.getString("SOW_N"); 
       String psm = rs.getString("PSM"); 
       String lbg = rs.getString("LBG"); 
       String toc = rs.getString("T_CONTRACT"); 
       String rm = rs.getString("REMARKS"); 

       String wks = rs.getString("WORK_ST"); 

       String cap = rs.getString("APPROVAL"); 
       String pfm = rs.getString("PLATFORM"); 
       String cmm = rs.getString("MODEL"); 

       String std = rs.getString("START_D"); 
       String etd= rs.getString("END_D"); 

       int id_col2 = rs.getInt("V_CONTRACT"); 
       String voc= Integer.toString(id_col2); 


       int id_col3 = rs.getInt("CUM_VALUE"); 
       String cvc = Integer.toString(id_col3); 
       t1.setText(id); 
       //c6.addItem(cr); 
       t3.setText(sow); 
       t4.setText(psm); 
       t5.setText(lbg); 
       //c4.addItem(toc); 
       t7.setText(voc); 
       t8.setText(rm); 
       //c1.addItem(wks); 
       //c2.addItem(cap); 
       //c5.addItem(pfm); 
       //c3.addItem(cmm); 
       c1.setSelectedItem(wks); 
       c2.setSelectedItem(cap); 
       c5.setSelectedItem(pfm); 
       c3.setSelectedItem(cmm); 
       c4.setSelectedItem(toc); 
       c6.setSelectedItem(cr); 
       t13.setText(std); 
       t14.setText(etd); 
       t15.setText(cvc); 

      } 

      else { 
       rs.previous(); 
       JOptionPane.showMessageDialog(sowui.this, "End of File"); 
      } 
     } 
     catch (SQLException err) { 
      JOptionPane.showMessageDialog(sowui.this, err.getMessage()); 
     }try{ 
     stmt.close(); 
     }catch(SQLException a) {} 
    } 
    else if(ae.getSource()==prev) 
    { 
     try { 
      if (rs.previous()) { 
       int id_col = rs.getInt("CRN"); 

       String id = Integer.toString(id_col); 
       //int id_col1 = rs.getInt("CR_NUM"); 
       //String cr= Integer.toString(id_col1); 
       String cr =rs.getString("CR_NUM"); 
       String sow = rs.getString("SOW_N"); 
       String psm = rs.getString("PSM"); 
       String lbg = rs.getString("LBG"); 
       String toc = rs.getString("T_CONTRACT"); 
       String rm = rs.getString("REMARKS"); 

       String wks = rs.getString("WORK_ST"); 

       String cap = rs.getString("APPROVAL"); 
       String pfm = rs.getString("PLATFORM"); 
       String cmm = rs.getString("MODEL"); 

       String std = rs.getString("START_D"); 
       String etd= rs.getString("END_D"); 

       int id_col2 = rs.getInt("V_CONTRACT"); 
       String voc= Integer.toString(id_col2); 


       int id_col3 = rs.getInt("CUM_VALUE"); 
       String cvc = Integer.toString(id_col3); 
       t1.setText(id); 
       //c6.addItem(cr); 
       t3.setText(sow); 
       t4.setText(psm); 
       t5.setText(lbg); 
       //c4.addItem(toc); 
       t7.setText(voc); 
       t8.setText(rm); 
       //c1.addItem(wks); 
       //c2.addItem(cap); 
       //c5.addItem(pfm); 
       //c3.addItem(cmm); 
       c1.setSelectedItem(wks); 
       c2.setSelectedItem(cap); 
       c5.setSelectedItem(pfm); 
       c3.setSelectedItem(cmm); 
       c4.setSelectedItem(toc); 
       c6.setSelectedItem(cr); 
       t13.setText(std); 
       t14.setText(etd); 
       t15.setText(cvc); 

      } 

      else { 
       rs.next(); 
       JOptionPane.showMessageDialog(sowui.this, "Top of File"); 
      } 
     } 
     catch (SQLException err) { 
      JOptionPane.showMessageDialog(sowui.this, err.getMessage()); 
     }try{ 
     stmt.close(); 
     }catch(SQLException a) {} 
    } 
    if(ae.getSource()==lastb) 
    { 
     try { 
      rs.last(); 
      int id_col = rs.getInt("CRN"); 

      String id = Integer.toString(id_col); 
      //int id_col1 = rs.getInt("CR_NUM"); 
      //String cr= Integer.toString(id_col1); 
      String cr =rs.getString("CR_NUM"); 
      String sow = rs.getString("SOW_N"); 
      String psm = rs.getString("PSM"); 
      String lbg = rs.getString("LBG"); 
      String toc = rs.getString("T_CONTRACT"); 
      String rm = rs.getString("REMARKS"); 

      String wks = rs.getString("WORK_ST"); 

      String cap = rs.getString("APPROVAL"); 
      String pfm = rs.getString("PLATFORM"); 
      String cmm = rs.getString("MODEL"); 

      String std = rs.getString("START_D"); 
      String etd= rs.getString("END_D"); 

      int id_col2 = rs.getInt("V_CONTRACT"); 
      String voc= Integer.toString(id_col2); 


      int id_col3 = rs.getInt("CUM_VALUE"); 
      String cvc = Integer.toString(id_col3); 
      t1.setText(id); 
      //c6.addItem(cr); 
      t3.setText(sow); 
      t4.setText(psm); 
      t5.setText(lbg); 
      //c4.addItem(toc); 
      t7.setText(voc); 
      t8.setText(rm); 
      //c1.addItem(wks); 
      //c2.addItem(cap); 
      //c5.addItem(pfm); 
      //c3.addItem(cmm); 
      c1.setSelectedItem(wks); 
      c2.setSelectedItem(cap); 
      c5.setSelectedItem(pfm); 
      c3.setSelectedItem(cmm); 
      c4.setSelectedItem(toc); 
      c6.setSelectedItem(cr); 
      t13.setText(std); 
      t14.setText(etd); 
      t15.setText(cvc); 



     } 
     catch (SQLException err) { 
      JOptionPane.showMessageDialog(sowui.this, err.getMessage()); 
     }try{ 
     stmt.close(); 
     }catch(SQLException a) {} 
    } 
+4

我認爲你對自己和我們這個巨大的上帝階級來說太難了。首先,考慮重構這個怪物,至少從你的GUI代碼中分離出你的數據庫代碼。這會讓你更容易調試,並且讓我們理解你是否遇到問題。此外,您的問題與Swing無關,並且都與數據庫編碼有關,因此您的Swing代碼只會分散注意力並混淆我們。 –

+0

代碼嚇到我了!請更新它使其可讀。 –

+0

只是發佈實際發生錯誤的代碼。 –

回答

2

Resultset綁定到您的語句和您的連接。當您仍然連接到打開的數據庫時,應該讀取ResultSet的內容,然後關閉結果集和語句,然後關閉連接。

請注意,您不必將結果讀取到UI - 您也可以將它們讀入一些內部集合中,並在需要時更新UI。只要將它們從結果集中移出,但仍然有對所提到的對象的引用。

+0

我該怎麼做?順便說一下編碼。我不確定實際發生錯誤的位置。所以我貼這樣。再次抱歉。 – user1616277

+0

@ Zaki ..如果插入查詢後我沒有關閉連接,文件沒有得到更新。即插入不會發生。但是,如果我給con.close()插入發生但不能遍歷。 – user1616277

+0

@ user1616277只是遍歷你的結果集並插入到一個列表,一個hashmap或類似的地方,並返回其他方法來使用 – eis

3

下一頁什麼已經在評論集,有關代碼一些其他的話你貼:

  • 擺脫setLayout(null)通話和所有相關電話(setBounds等)的。改爲Learn to use Layout Managers
  • Swing組件應在Event Dispatch Thread上創建/訪問/修改。此線程負責UI,不應該用於長時間運行的任務(如處理ResultSet實例)。見Concurrency in Swing教程更多信息
  • 驗證一個JTextComponent的輸入(在你的代碼中的變量稱爲t1t3)不應該使用KeyListenerUse a DocumentFilter instead,或者在文本字段爲consider using a JFormattedTextField的情況下。例如,在文本字段中輸入的拖放將完全繞過您的KeyListener
  • 請注意,您在偵聽器中收到的大部分事件都有一個getSource()方法,該方法允許檢索發生事件的源組件。這可以避免很多代碼重複,因爲您有多個完全相同的偵聽器。它們不同的唯一部分是源組件,它可以從事件中檢索。另一種選擇是從匿名類切換到內部類,並在構造函數中傳遞所需的信息。
  • 只需在try{} catch (Exception e)的範圍內隨機選擇一段代碼,然後不做任何錯誤處理就會在將來尋求奇怪的錯誤。要麼你正確地處理了Exception,要麼你進一步拋棄它。
  • catch塊更糟糕。現在你默默地忽略了這個錯誤,代碼進一步發生,沒有任何反應。誰知道你的程序將以什麼狀態結束