2016-06-15 82 views
1

現在我的計數輸出爲0,對於組合框中的每個元素,即它類似於B10,B20,B30(B是默認值,下一項是來自數據庫的值, 0表示在這個連接字符串的計數)......我的計數不會增加通過結合組合框的值和串行計數創建字符串

我應該怎麼做,這樣我數增加時,我選擇的JComboBox 的值,然後按下按鈕IEI獲得B10,B11, B12,B20,B21,B22,B30,B31,B32

  public void actionPerformed(ActionEvent ae) { 
      String str = ae.getActionCommand(); 
      if (str.equals("GENERATE PART NO. :")) { 
      try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      con = DriverManager.getConnection("jdbc:mysql://localhost/d03", "root", ""); 
      st = con.createStatement(); 

      String s = "select value from user1 where Userdata='" + jc.getSelectedItem() + "'"; 

      rs = st.executeQuery(s); 
      t1.getText(); 
      if (rs.next()) { 
       int j = 0; 
       String add1 = rs.getString("value"); 
       t1.setEditable(false); 

       String str9 = new String(); 
       str9 = "B" + add1; //B is the default value, add1 is the value from database 

       String str10 = new String(); 
       str10 = str9 + j; 
       String query = "select MC from final"; 
       ResultSet rs1 = st.executeQuery(query); 
       while (rs1.next()) { 
        if (str10.equals(rs1)) { 
         j = j + 1; 
         j=new Integer(j+1); 
         t1.setText(str10); 
        } else { 
         t1.setText(str10); 
        } 
       } 
      } 
      try { 
       Class.forName("com.mysql.jdbc.Driver"); 
       con = DriverManager.getConnection("jdbc:mysql://localhost/d03", "root", ""); 

       String s1 = ("insert into final(MC)values(?)"); 
       PreparedStatement pstm = con.prepareStatement(s1); 
       pstm.setString(1, t1.getText()); 
       int rowi = pstm.executeUpdate(); 

       if (rowi > 0) { 
        JOptionPane.showMessageDialog(null, "DATA INSERTED"); 
       } 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
       JOptionPane.showMessageDialog(null, "ERROR CLOSE"); 
      } 

     } 

回答

0

的答案很簡單: 移動str10 = str9 + j;while身體。

你的代碼(片段)應該是這樣的:

/* if */while(rs.next()) { 
     int j = 0; 
     String add1 = rs.getString("value"); 
     t1.setEditable(false); 

    // String str9; //= new String(); redundant 
     String str9 = "B" + add1; //B is the default value add1 is the value from database 
     String str10 = str9; 
     String query = "select MC from final"; 
     ResultSet rs1 = st.executeQuery(query); 
     while (rs1.next()) { 
      if (str10.equals(rs1.getString("MC")) { 
       j++; //j = j + 1; 
       // j=new Integer(j+1); you simply increment j twice 
       str10 = str9 + j;// EDITED LINE! 
      } 
      t1.setText(str10); 
     } 
    } 
+1

它不工作....我想這已經 – user6443988

+0

那麼,什麼是錯的? –

+0

上一個值仍然顯示0(第一個計數),即B10(對於第一個元素的組合框)B20(對於第二個元素)等等。它應該增加,但不會增加 – user6443988