未保存到db的jFrame字段

问题描述:

我试图从我的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

的代码做

      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为空 –

试试这样做。首先创建一个连接类如下:

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