我有一个表单,它捕获用户在 JFormattedTextField 中输入的日期。然后需要使用 PreparedStatement 将日期存储在数据库 (postgresql) 中。我在 pStat.setDate(4, dob); 行收到错误消息。
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()));