美好的一天
我试图将日期插入数据库并使用相同的解决方案here
我现在面对的是,在我使用此解决方案后,它为我显示
Java.nullPointerException
我也试过((JTextField) jDateChooser1.getDateEditor().getUiComponent()).getText()
并没有与我合作,并说不能将日期转换为字符串。
这是我的代码:
import java.util.Date;
import java.sql.*;
String Sql="insert into rect (recName,phoneNo,position,DOB,gender,Mstatus,nationality,quali,major,Uni,email,EngCom,AraCom,status,Stage,IntDate,source,interviewer,RecEval,comments,Attach) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hr","root","");
Date date = jDateChooser1.getDate();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String strDate = formatter.getDateInstance().format(date);
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
Date datex = jDateChooser2.getDate();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String intrDate = format.getDateInstance().format(datex);
java.sql.Date sqlDatex = new java.sql.Date(datex.getTime());
ps= con.prepareStatement(Sql);
ps.setString(1, jTextField18.getText());
ps.setString(2, jTextField1.getText());
ps.setString(3, jTextField2.getText());
ps.setDate(4,sqlDate);
if(jRadioButton1.isSelected()== true){
ps.setString(5, jRadioButton1.getText());
}
else {
ps.setString(5, jRadioButton2.getText());
}
ps.setString(6, (String) jComboBox1.getSelectedItem());
ps.setString(7, jTextField3.getText());
ps.setString(8, jTextField4.getText());
ps.setString(9, jTextField5.getText());
ps.setString(10, jTextField6.getText());
ps.setString(11, jTextField8.getText());
ps.setString(12, jTextField9.getText());
ps.setString(13, jTextField10.getText());
ps.setString(14, jTextField11.getText());
ps.setString(15, jTextField12.getText());
ps.setDate(16, sqlDatex);
ps.setString(17, jTextField14.getText());
ps.setString(18, jTextField15.getText());
ps.setString(19, jTextArea1.getText());
ps.setString(20, jTextArea2.getText());
ps.setString(21, jTextField19.getText());
int i =ps.executeUpdate();
if (i>0)
{
JOptionPane.showMessageDialog(null, "Data is Saved");
}
else {
JOptionPane.showMessageDialog(null, "Data is not Saved");
}
}
catch (Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
JOptionPane.showMessageDialog(null, e);
}
答案 0 :(得分:1)
正如@MadProgrammer所说,并this发布:
// Gives you the date
Date date = jDateChooser1.getDate();
// Transform to string
String strDate = DateFormat.getDateInstance().format(date);