我正在尝试编写这个练习 CRUD 应用程序。这是一个应用程序来存储与出生日期和东西的联系人。由于我使用命令行来接受输入并将其存储在 HSQLDB 数据库中,因此我不确定如何接受Date
类型参数。我尝试将它从 Sting 解析为 long,但它一直抛出一个SQLDataException
.
这里是我用于这个变量的代码行
//this in the class with main
private Date dob;
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
System.out.println("Enter date of birth of "+n+" in the format dd/mm/yyyy
String date = sc2.nextLine();
long d = Date.parse(date);
uv.setDob(d);
//class with all the business logic
public void addContact(String cb, String name, long dob, String email, String phno, String tag) {
sql = "insert into "+ cb + "(name,dob,email,phone,tag) values (?,?,?,?,?)";
try {
con = JDBCHelper.getConnection();
ps1 = con.prepareStatement(sql);
ps1.setString(1, name);
ps1.setLong(2, dob);
ps1.setString(3, email);
ps1.setString(4,phno);
ps1.setString(5, tag);
}
catch(SQLException e) {
e.printStackTrace();
}
}
它在输入日期时给我的例外是
java.sql.SQLSyntaxErrorException: incompatible data type in conversion
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.setParameter(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.setLongParameter(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.setLong(Unknown Source)
Caused by: org.hsqldb.HsqlException: incompatible data type in conversion
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.types.DateTimeType.convertJavaToSQL(Unknown Source)