SQL INSERT system date to table

I created a table called "myTable" with the column CURRENT_DATE, and this data type is "DATE". When I update my table programmatically, I also want to place the timestamp or system date in the CURRENT_DATE field. Part of what I'm doing is shown below, but it does not work. I would think it would be easy, but ... Can you help?

//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 + " ')  ");      
+5
source share
5 answers

You really have to do it as a prepared expression using parameters, it simplifies the work and eliminates some very simple SQL injection threats.

Connection con = ...;
PreparedStatement statement = con.prepareStatement("INSERT INTO myTable (NAME, CURRENT_DATE) VALUES ( ?, ?)");
statement.setString(1, userName);
statement.setDate(2, currentDate );
statement.execute();

, . : http://www.jdbc-tutorial.com/jdbc-prepared-statements.htm

+8

, SQL-, . , , MS SQL Server getdate().

stmt.executeQuery("INSERT INTO myTable (NAME, CURRENT_DATE) VALUES (' " + userName + " ', getdate()");      

getdate() , :

stmt.executeQuery("INSERT INTO myTable (NAME) VALUES (' " + userName + " '" ')  ");      
+3

SQL. .

+2

:

stmt.executeQuery("INSERT INTO myTable (NAME, CURRENT_DATE) VALUES (' " + userName + " ', GETDATE())  "); 
0

.

Any other method may be caused by malicious (or erroneous) code.

How to get the actual value (and how to create the appropriate triggers) depends on the DBMS itself. DB2 provides current date, current timeand current timestampwhich you can use to insert server time into a database, which is likely to be more consistent than hundreds of different client times.

0
source

All Articles