我有一个表单,它捕获用户在JFormattedTextField中输入的日期。然后,需要使用PreparedStatement将Date存储在数据库(postgresql)中。我在pStat.setDate(4,dob);行有错误消息。使用Java中的PreparedStatement在数据库中插入日期
Date dob = (Date)ftxtDOB.getValue();
String add = txtAddress.getText();
String country = txtCountry.getText();
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/postgres", "postgres","cisco");
pStat = conn.prepareStatement("INSERT INTO customer_info VALUES (?,?,?,?,?,?)");
pStat.setString(1, id);
pStat.setString(2, surname);
pStat.setString(3, fName);
pStat.setDate(4, dob);
}catch(Exception e){
}
编辑:我有编译器的这个错误信息。
no suitable method found for setDate(int,java.util.Date)
method java.sql.PreparedStatement.setDate(int,java.sql.Date,java.util.Calendar) is not applicable
(actual and formal argument lists differ in length)
method java.sql.PreparedStatement.setDate(int,java.sql.Date) is not applicable
(actual argument java.util.Date cannot be converted to java.sql.Date by method invocation conversion)
编辑:解决了,我用:
pStat.setDate(4, new java.sql.Date(dob.getTime()));
那可能是什么错误信息?你应该真的填写那个catch子句......至少把e.printStackTrace()放在那里。 – pcalcao 2012-02-27 17:11:27
而错误是?你看过错误信息吗?它应该可以帮助你,而不是一些可以忽略的乱码。切勿忽略异常。 – 2012-02-27 17:12:02