2017-08-28 79 views
0

我試圖從我的jFrame表單保存一些字段,但是當我點擊保存按鈕時,我得到空指針異常。以前我得到的錯誤,文本字段不能爲空,所以我取消選中不爲空,但它沒有工作,我決定不包括Voucher_no在MySQL插入語句,因爲它是一個自動增量和其文本字段不可編輯 我的代碼有問題嗎? 正在使用Mysql Workbench。未保存到db的jFrame字段

try { 
      Class.forName("com.mysql.jdbc.Driver").newInstance(); 
      Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/canning?zeroDateTimeBehavior=convertToNull", "root", ""); 
      con.setAutoCommit(false); 
      try { 

//   String kb = jTextField3.getText(); //voucher_no 
       String kc = (String) jComboBox3.getSelectedItem(); //factory 
       String kg = jTextField10.getText(); //fuel 
       Double ks = Double.valueOf(kg); 
       String ke = jTextField11.getText(); //electricity 
       Double kt = Double.valueOf(ke); 
       String kf = jTextField12.getText(); //manpower 
       Double ku = Double.valueOf(kf); 
       String kj = (String) jComboBox4.getSelectedItem(); //can_name 
       String kk = (String) jComboBox5.getSelectedItem(); //label name 
       String kq = jTextField23.getText();//no_of_cans 
       Double kv = Double.valueOf(kq); 
       String kr = jTextField24.getText();//no_of_labels 
       Double kw = Double.valueOf(kr); 

       String query1= "insert into production (date,factory,fuel,electricity,manpower,can_name,label_name,no_of_cans,no_of_labels)" 
         + "values (?,?,?,?,?,?,?,?,?)"; 

       try (PreparedStatement pst = con.prepareStatement(query1)){ 
//     pst.setString(10, kb); 
        pst.setString(1, ((JTextField) jDate1.getDateEditor().getUiComponent()).getText()); 
        pst.setString(2, kc); 
        pst.setDouble(3, ks); 
        pst.setDouble(4, kt); 
        pst.setDouble(5, ku); 
        pst.setString(6, kj); 
        pst.setString(7, kk); 
        pst.setDouble(8, kv); 
        pst.setDouble(9, kw); 

        pst.execute(); 
       } 


      try { 
         int rows = jTable2.getRowCount(); 

         for (int row = 0; row < rows; row++) { 
          String kd = (String) jTable2.getValueAt(row, 0); 
          Double kn = (Double) jTable2.getValueAt(row, 1); 
          try { 
//      Class.forName("com.mysql.jdbc.Driver").newInstance(); 
//    Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/canning?zeroDateTimeBehavior=convertToNull", "root", ""); 

           String query = "insert into production(product_name,product_quantity)" 
             + "values (?,?)"; 
           PreparedStatement update = con.prepareStatement(query); 
           update.setString(1, kd); 
           update.setDouble(2, kn); 

           update.addBatch(); 

           update.executeBatch(); 
           con.commit(); 

          } catch (SQLException e) { 
           e.printStackTrace(); 
//      JOptionPane.showMessageDialog(this, "Error in production Table"); 
          } 
         } 
        } catch (Exception e) { 
         JOptionPane.showMessageDialog(this, "Invalid Entry in fields"); 
        } 

      } catch (Exception e) { 
       e.printStackTrace(); 
//    JOptionPane.showMessageDialog(null, "Invalid in fields"); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
+0

你有沒有嘗試用調試器運行你的代碼,以便知道哪個對象爲空?此外,嘗試使用顯式變量名稱,在當前狀態下,您的代碼根本不可讀取。 – SebVb

+0

我使用了一個調試器,它表明問題出在我的for循環中,但我沒有看到任何錯誤,是嗎? – Stevoski

回答

0

的代碼做

      update.addBatch(); 

          update.executeBatch(); 
          con.commit(); 

executeBatchcommit必須外循環來完成。 誤解來自應該被命名爲「addToBatch」的「addBatch」。

+0

我試圖改變代碼,但現在我得到異常'product_name不能爲空'} update.executeBatch(); con.commit(); (異常e){ JOptionPane.showMessageDialog(null,「Can not save。」+ e); } – Stevoski

+0

順便說一句,我在這兩種情況下都有同樣的表'生產'!並且這兩個語句應該被隔離執行,或者也可以使用addBatch執行第一個語句。 –

+0

顯然kd爲空 –

0

試試這樣做。首先創建一個連接類如下:

import java.sql.Connection; 
import java.sql.DriverManager; 

public class db_connection 
{ 
    public Connection connection() 
    throws Exception 
    { 
    try 
    { 
     Connection conn = null; 

     String username = "root"; String password = ""; 
     String driver = "com.mysql.jdbc.Driver"; 
     String url = "jdbc:mysql://localhost:3306/database"; 
     try { 
     Class.forName(driver); 
     } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } 

     conn = DriverManager.getConnection(url, username, password); 

     conn.setAutoCommit(false); 
     return conn; 
    } catch (Exception e) { 
     e.printStackTrace(); 
     System.exit(0); 
    }return null; 
    } 
} 

然後在您的保存按鈕做這樣的事情,

private void ButtonRegGroup_SubmitActionPerformed(java.awt.event.ActionEvent evt) {              
// TODO add your handling code here: 
    if ((this.jTextFieldGrp_ReqAmnt.getText().equals("")) 
      || (this.jTextFieldGrp_name.getText().equals("")) 
      || (this.jTextFieldGrp_Id.getText().equals("")) 
      || (this.jTextFieldGrp_prj_name.getText().equals(""))) { 
     JOptionPane.showMessageDialog(this, "Some fields are empty", this.title, this.error); 
    } else { 
     try { 
      this.con = ((Connection) new db_connection().connection()); 
      this.pst = ((PreparedStatement) this.con.prepareStatement("insert into registered_projects(project_type,name,project_name,project_id,constituency,ward,requested_amount)values(?,?,?,?,?,?,?)")); 

      GroupRegDetails(); 
      this.pst.setString(1, this.proj_type); 
      this.pst.setString(2, this.group_name); 
      this.pst.setString(3, this.proj_name); 
      this.pst.setInt(4, this.proj_id); 
      this.pst.setString(5, this.constituency); 
      this.pst.setString(6, this.ward); 
      this.pst.setInt(7, this.requested_amount); 
      int row = this.pst.executeUpdate(); 
      con.commit(); 
      if (row == 0) { 
       this.con.rollback(); 
       JOptionPane.showInternalMessageDialog(this, "Didn't Register project", this.title, this.error); 
      } 
      JOptionPane.showMessageDialog(this, "Project Successfully Registered", this.title, this.infor); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
public void GroupRegDetails() { 
    this.group_name = this.jTextFieldGrp_name.getText(); 
    this.proj_id = Integer.parseInt(this.jTextFieldGrp_Id.getText()); 
    this.proj_name = this.jTextFieldGrp_prj_name.getText(); 
    this.constituency = this.jComboBoxGrp_Comb_const.getSelectedItem().toString(); 
    this.ward = this.jComboBoxGrp_comb_Ward.getSelectedItem().toString(); 
    this.requested_amount = Integer.parseInt(this.jTextFieldGrp_ReqAmnt.getText()); 
    this.proj_type = "Group"; 
} 

最後好的做法是把在一個JPanel和JPanel的JFrame上的字段。希望這可以幫助。

+0

您是使用'this.group_name = this.jTextFieldGrp_name.getText()'定義變量的嗎?' – Stevoski

+0

是例如jTextFieldGrp_name是字段變量名稱。我有未初始化的變量String group_name;並且我從字段中給它賦值,如this.group_name = this.jTextFieldGrp_name.getText();.希望被清除 – Simiyu

+0

是啊我得到你,如果你想在MySQL數據庫中保存多行數據從一個jtable到一行,即我有一個生產和raw_material jtable,其中一個生產包含許多原材料。做這個。我希望他們分享同一個productionId。 – Stevoski