我创建了一个名为"myTable"的表,其中一列名为CURRENT_DATE,其数据类型为"DATE".当我以编程方式更新我的表时,我还想在CURRENT_DATE字段中放置时间戳或系统日期.我正在做的一部分如下所示,但它不起作用.我认为这很容易,但是......你能帮忙吗?
//System date is captured for placement in the CURRENT_DATE field in myTable
java.util.Date currentDate = new java.util.Date();
...
stmt.executeQuery("INSERT INTO myTable (NAME, CURRENT_DATE) VALUES (' " + userName + " ', ' " + currentDate + " ') ");
Run Code Online (Sandbox Code Playgroud)
你真的应该使用参数作为一个准备好的语句来做这件事,它使事情变得更容易,并削减了一些非常简单的SQL注入威胁.
Connection con = ...;
PreparedStatement statement = con.prepareStatement("INSERT INTO myTable (NAME, CURRENT_DATE) VALUES ( ?, ?)");
statement.setString(1, userName);
statement.setDate(2, currentDate );
statement.execute();
Run Code Online (Sandbox Code Playgroud)
有关如何正确使用预准备语句的大量信息.例如:http://www.jdbc-tutorial.com/jdbc-prepared-statements.htm
| 归档时间: |
|
| 查看次数: |
18466 次 |
| 最近记录: |