我编写了这段代码,将数据插入到tbl_Hotel_Reservation表中..但是它无效!问题是什么?!它可以在插入查询中的valuse部分吗? person和involved_by是int格式!
// DO RESERVATION
DateFormat parser = new SimpleDateFormat("M-dd-yyyy");
int hotel_id = 0;
String username = jTextField4.getText();
String city = jComboBox1.getSelectedItem().toString();
String hotel_name = jComboBox2.getSelectedItem().toString();
String start_temp = jDateChooser1.getDate().toString();
Date start = parser.parse(start_temp);
String end_temp = jDateChooser2.getDate().toString();
Date end = parser.parse(end_temp);
int person = Integer.parseInt(jTextField3.getText());
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/JEREN","root","");
Statement stat = con.createStatement();
String insert = "insert into tbl_Hotel_Reservation (start_date,end_date,number_of_person,arranged_by,involved_by) VALUES ('"+start+"','"+end+"',"+person+",'"+username+"',"+hotel_id+");";
stat.executeUpdate(insert);
stat.close();
con.close();
JOptionPane.showMessageDialog(null,"Reservation is done Successfully!");
}
catch (SQLException | HeadlessException e)
{
//e.printStackTrace();
JOptionPane.showMessageDialog(null,"Error occurred in reservation process");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Users.class.getName()).log(Level.SEVERE, null, ex);
}
答案 0 :(得分:0)
Date
对象没有格式。 Date
对象只是代表某个特定时刻。
您已有Date
。为什么要尝试获取toString()
值并将其解析回Date
?
String start_temp = jDateChooser1.getDate().toString();
Date start = parser.parse(start_temp);
这没有任何意义。
如果您尝试以表格中的文字格式输入Date
,只需格式化您拥有的Date
对象。
String start_temp = parser.format(jDateChooser1.getDate());
如果您尝试输入Date
作为SQL时间戳字段,请使用PreparedStatement
和setTime()
方法之一。{/ p>