我正在使用java.sql.Connection
和java.sql.Date
访问此处的日期是我的代码:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(jTextArea1.getText().equals(""))
JOptionPane.showMessageDialog(null, "Please Enter the Expence Description. ");
else if(jComboBox4.getSelectedIndex()<1)
JOptionPane.showMessageDialog(null, "Please Select the Type.");
else if(jDateChooser1.getDate().equals(""))
JOptionPane.showMessageDialog(null, "Please Enter the Date. ");
else if(jTextField3.getText().equals(""))
JOptionPane.showMessageDialog(null, "Please Enter the Amount.");
else{
try{
con= DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","1234");
ps=con.prepareStatement("insert into work values(?,?,?,?)");
ps.setString(1, jTextArea1.getText());
ps.setString(2, jCom.getDate());boBox4.getSelectedItem().toString());
ps.setDate(3, (Date) jDateChooser1.getDate());
但是它给出了错误
java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
任何人都可以为上述解决方案提供解决方案吗?
答案 0 :(得分:2)
ps.setDate(3,(Date)jDateChooser1.getDate());
不是在上面的代码中转换日期,而是将util Date转换为sql Date,如下所示:
java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
答案 1 :(得分:1)
in fact the format is used yyyy-MM-dd, just use a SimpleDateFormat to format the date value to the desired format when retriving a date value......
SimpleDateFormat dateFormat= new SimpleDateFormat("dd/MM/yyyy");
String strDate = dateFormat.format(rs.getDate("column name"));