我在将数据从我的 GUI 传递到 MySQL 数据库时遇到问题。我正在从JTextField
s 和JDateChooser
, 收集信息到数据库中。除日期外,其他一切都有效。我尝试了多种在网上找到的方法,但都没有奏效。我还检查了该表,以确保在我的“患者信息”表中启用了“日期”数据类型。如果我删除 JDateChooser,我的查询就有效。否则,我将收到此错误消息:
Java.lang.NullPointerException
我在此消息中包含了我的源代码。
//Event handler adds records
//to the database
JButton subInfoBtn = new JButton("SUBMIT AND CONTINUE");
subInfoBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
//Invokes "javaconnect" class
//to link to MySQL database
conn = javaconnect.ConnecrDb();
//Invokes SQL Statement from "Connection" Object
pst = conn.createStatement();
//SQL query commands event handler to access
//"patientinfo" table, to insert data to columns.
pst.executeUpdate("INSERT into patientinfo(firstName, lastName, DOB, age, SSN, "
+ "address, phone,email, emergencycontact, emergencyphone) "
+ "VALUES" + "('"+firstTxt.getText() + "', '" + lastTxt.getText()
+ "', '" + DOBTxt.getDate() + "' ,'" + ageTxt.getText() + "', '"
+ ssnTxt.getText() + "', "+ " '" + addressTxt.getText() +"',
'"+phoneTxt.getText()+"' , '"+emailTxt.getText()+"' ,
'"+emergencyTxt.getText()+"' , '"+emergPhoneTxt.getText()+"' )");
//Closes Database Connection
conn.close();
JOptionPane.showMessageDialog(null, "Pantient Information Saved Succesfully");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
});
subInfoBtn.setFont(new Font("Tahoma", Font.BOLD, 14));
subInfoBtn.setBounds(305, 286, 220, 23);
contentPane.add(subInfoBtn);
//Also, I have tried to use SimpleDateFormat and
//((JTextField)jDateChooser1.getDateEditor().getUiComponent()).getText();
//Without any luck. I am also aware of using Prepare Statement to avoid SQL injections, but
//I would like to solve the JDateChooser bonding data dilema into MySQL database.