我有java swing日期选择器,当我将日期传递给derby表虽然准备插入语句下面我得到错误消息拒绝日期日期类型,在我的表中我把日期类型是日期。 在代码下面,数据库pst.setString不接受第二个字段(2, dateChooser.getText());:
public void DoSave() {
try{
String host1 = "jdbc:derby://localhost:1527//accountsdb";
String uName1="accounts";
String uPass1="accounts";
con1=DriverManager.getConnection(host1,uName1 ,uPass1);
String sql1="INSERT into journal
(journal_no,journal_date,journal_submain_no,journal_dr
, journal_cr,journal_desc,journal_user,journal_docno)
values ( ?,?,?,?,?,?,?,?)";
PreparedStatement pst=con1.prepareStatement(sql1);
pst.setString(1,jTjournal_docno.getText().trim() );
pst.setString(2,dateChooser.getText());
pst.setString(3,jTjournal_submain_no.getText().trim() );
pst.setString(4,jTjournal_amount.getText() ); //Dr
pst.setString(5,jTjournal_amount.getText() );//Cr
pst.setString(6,jTjournal_desc.getText().trim() );
pst.setString(7,Frmlogin.myname );
pst.setString(8,jTjournal_docno.getText().trim() );
pst.execute();
rs.close();
doClear();
JOptionPane.showMessageDialog(null, "Saved ");
new JFband().setVisible(true);
this.dispose();
}
catch (SQLException ex) {
System.out.println(ex.getMessage()) ;
}
}
我不知道如何使用上面的预付费声明通过日期选择器发送日期到表格请帮助
答案 0 :(得分:0)
最后我有一个解决方案似乎是我不熟悉的日期选择器 用我的代码,现在我将jcalendar-1.3.3 jar库添加到我的项目中 并用JDateChooser替换了我的旧日期,现在代码工作正常,我可以 根据以下代码将包含日期的记录插入到我的表中:
public void DoSave() {
try{
String host1 = "jdbc:derby://localhost:1527//accountsdb";
String uName1="accounts";
String uPass1="accounts";
con1=DriverManager.getConnection(host1,uName1 ,uPass1);
String sql1="INSERT into journal (journal_no,journal_date,journal_submain_no,journal_dr,journal_cr,journal_desc,journal_user,journal_docno) values (?,?,?,?,?,?,?,?)";
PreparedStatement pst=con1.prepareStatement(sql1);
pst.setInt(1,Integer.parseInt(jLvoucher_no.getText().trim()) );
pst.setString(2,((JTextField)dateChooser.getDateEditor().getUiComponent()).getText());
pst.setInt(3,Integer.parseInt(jTjournal_submain_no.getText().trim()) );
pst.setInt(4,Integer.parseInt(jTjournal_amount.getText().trim()) ); //Dr
pst.setInt(5,Integer.parseInt(jTjournal_amount.getText().trim()) );//Cr
pst.setString(6,jTjournal_desc.getText().trim() );
pst.setString(7,Frmlogin.myname );
pst.setString(8,jTjournal_docno.getText().trim() );
pst.execute();
JOptionPane.showMessageDialog(null, "Saved");
}
catch (SQLException ex) {
System.out.println(ex.getMessage()) ;
}
}