2015-04-01 72 views
0

其實我有10-30假人從txtCC獲得的價值,但我只用了3個假人例如下面..如何從一個文本字段獲得多個值用分隔符和每個保存到數據庫

那麼如何獲取每個值並將其直接保存到我的數據庫而不使用虛擬?這是一個大問題,怎麼把」我的代碼太大使用這些假人編譯..

感謝您的幫助..

private void bSaveActionPerformed(java.awt.event.ActionEvent evt) 
{          
    // Save to database 

    String cc = txtCC.getText(); 
    String delimiter = ","; 
    String[] temp; 
    temp = cc.split(delimiter); 
    for(int i = 0; i < temp.length; i++) 

    if(i==0) { 
     txtC1.setText(temp[0]); 
     txtC2.setText("0"); 
     txtC3.setText("0"); } 
    else if (i==1) { 
     txtC1.setText(temp[0]); 
     txtC2.setText(temp[1]); 
     txtC3.setText("0"); } 
    else if (i==2) { 
     txtC1.setText(temp[0]); 
     txtC2.setText(temp[1]); 
     txtC3.setText(temp[2]); } 

    try { 
     String cc1 = txtC1.getText(); int CC1 = Integer.parseInt(cc1); 
     String cc2 = txtC2.getText(); int CC2 = Integer.parseInt(cc2); 
     String cc3 = txtC3.getText(); int CC3 = Integer.parseInt(cc3); 

     int opt = JOptionPane.showConfirmDialog(null,"Are you sure you want to save this record? "); 
     if (opt == 0){ 
      if(!txtC1.getText().equals("0")) { 
       stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); 
       String sql = "Select * from tbl_liqinfo"; 
       rs = stmt.executeQuery(sql); 
       rs.next(); 
       rs.moveToInsertRow(); 
       rs.updateInt("CC", CC1); 
       rs.insertRow(); 
       rs.close(); 
      } 

      if(!txtC2.getText().equals("0")) { 
       stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); 
       String sql = "Select * from tbl_liqinfo"; 
       rs = stmt.executeQuery(sql); 
       rs.next(); 
       rs.moveToInsertRow(); 
       rs.updateInt("CC", CC2); 
       rs.insertRow(); 
       rs.close(); 
      } 

      if(!txtC3.getText().equals("0")) { 
       stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); 
       String sql = "Select * from tbl_liqinfo"; 
       rs = stmt.executeQuery(sql); 
       rs.next(); 
       rs.moveToInsertRow(); 
       rs.updateInt("CC", CC3); 
       rs.insertRow(); 
       rs.close(); 
      } 

     } 
    } 
    catch (SQLException err){ 
     JOptionPane.showMessageDialog(FrmEmpLiquidation.this, err.getMessage()); 
    } 
} 
+0

這裏什麼是虛設? – 2015-04-01 10:23:17

+0

正確格式並再次粘貼代碼... – 2015-04-01 10:24:15

+0

使用String的拆分方法。 – Stultuske 2015-04-01 10:24:22

回答

0

而不是使用假人,創建簡單的小方法和利用它。這會減少你的代碼行數。也很容易理解。

私人無效bSaveActionPerformed(EVT java.awt.event.ActionEvent中){
//保存到數據庫

String cc = txtCC.getText(); 
    String delimiter = ","; 
    String[] temp; 
    temp = cc.split(delimiter); 
    for(int i = 0; i < temp.length; i++) 
     insertData(temp[i]); 

} 


public void insertData(final String data){ 
      txtC1.setText(data); 

     try { 
      String cc1 = txtC1.getText(); int CC1 = Integer.parseInt(cc1); 

      int opt = JOptionPane.showConfirmDialog(null,"Are you sure you want to save this record? "); 
      if (opt == 0){ 
       if(!txtC1.getText().equals("0")) { 
        stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); 
        String sql = "Select * from tbl_liqinfo"; 
        rs = stmt.executeQuery(sql); 
        rs.next(); 
        rs.moveToInsertRow(); 
        rs.updateInt("CC", CC1); 
        rs.insertRow(); 
        rs.close(); 
       } 

      } 
     } 
     catch (SQLException err){ 
      JOptionPane.showMessageDialog(FrmEmpLiquidation.this, err.getMessage()); 
     } 
} 
+0

在這裏謝謝..它幫助了很多..但另一個問題是,因爲我有一個消息框「你確定要保存這個記錄嗎?」它總是彈出多次,具體取決於文本框使用的值的數量。例如我一次放3個值然後消息框出現3次.. – 2015-04-04 08:23:49

相關問題