我试图从我的jFrame表单中保存一些字段但是当我单击保存按钮时,我得到空指针异常。以前我得到的错误是文本字段不能为空所以我取消选中不是null但它不起作用,我决定不在MySQL插入语句中包含Voucher_no,因为它是一个自动增量,其文本字段不可编辑 我的代码有问题吗? 我正在使用Mysql Workbench。
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 :(得分:0)
代码正在执行
update.addBatch();
update.executeBatch();
con.commit();
executeBatch
和commit
必须在循环外完成。
误解源于&#34; addBatch&#34;应该已经命名为&#34; addToBatch。&#34;
答案 1 :(得分: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上的Jpanel上。希望这会有所帮助。