【发布时间】:2013-02-15 19:53:59
【问题描述】:
我正在寻找一种方法来获取今天的日期并传递给 sql 表并保存在那里。调用保存的日期并使用 JODA TIME API 执行一些任务。更改后的 Joda 时间日期到 sql 表并保存在那里并继续处理..
我试过这样,
//prints todays date
java.sql.Date sqlDate = new java.sql.Date(new Date().getTime());
//passes wrong date to the table like 1970-07-01 instead of 2013-03-01
String insert = "INSERT INTO TEST_TABLE VALUES(1,"+sqlDate+")";
pStmt = conn.prepareStatement(insert);
pStmt.executeUpdate();
//converting to joda time
LocalDate ld = new LocalDate(sqlDate);
//some calculations, and how to convert back to sql date?
我在这里要做的是,一个包含 3 列(id、startdate、finishdate)的表。 id 将由用户输入,开始日期应自动输入今天的日期。在使用 joda 时间和完成日期进行一些计算后,将设置为完成日期。
代码
String insert = "INSERT INTO TEST_TABLE VALUES(2,'"+timestamp+"')";
错误
Data type mismatch in criteria expression
//I have created table using MS access
//the format of the date column is Date/Time.
【问题讨论】:
-
可以请看底部的更新问题,我遇到了错误,谢谢。
-
请正确使用准备好的语句(通过使用
?代替您希望参数去的位置,然后使用适当的setX()函数),您的PS 在技术上仍然容易受到SQL 的影响注射。 -
我认为你应该接受这个答案,因为它帮助你解决了你的基本问题