| Author |
Message |
|
2004/08/16 00:00
|
|
|
jameshao
Joined: 2005/04/29
Messages: 1
Offline
|
|
|
|
Hi, This is a mysql-jdbc question. I am trying to issues a jdbc statement to add a row into the mysql table named ‘registration’. There are three columns in the table. The last one is a timestamp. The following is the jdbc statement in my java code: PreparedStatement pstmt = con.prepareStatement( "insert into registration(username, password, timestamp) values (?, ?, ?)"); pstmt.setString(1, username); pstmt.setString(2, password); pstmt.setString(3, “now()”); pstmt.executeUpdate(); With this statement, the timestamp in the table is always set to ‘0000000000’. I know the problem is related to how to format now() in a right way, I have tried “now()”, ‘now()’, and now(), and none of them work. Could any one advice how to make this right? Thanks in advance, James
|
|
|
|
 |
| |
|
2004/08/18 00:00
|
|
|
martyn
Joined: 2005/04/29
Messages: 217
Offline
|
|
|
|
Hi James, I presume you are using the "mysql-connector-java-3.0.14-production-bin.jar" of some sort. Now, I do not understand where you got the method "now()". Which class does it come from? More information is required. Thanks. Martyn
|
|
|
|
 |
| |
|
2004/08/31 00:00
|
|
|
Emma Hayes
Joined: 2005/04/29
Messages: 3
Offline
|
|
|
|
This should work nicely: PreparedStatement pstmt = con.prepareStatement( "insert into registration(username, password, timestamp) values (?, ?, now())"); pstmt.setString(1, username); pstmt.setString(2, password); pstmt.executeUpdate();
|
|
|
|
 |
| |
|
2004/08/31 00:00
|
|
|
Emma Hayes
Joined: 2005/04/29
Messages: 3
Offline
|
|
|
|
This should work nicely: PreparedStatement pstmt = con.prepareStatement( "insert into registration(username, password, timestamp) values (?, ?, now())"); pstmt.setString(1, username); pstmt.setString(2, password); pstmt.executeUpdate();
|
|
|
|
 |
| |
|
2004/08/31 00:00
|
|
|
Emma Hayes
Joined: 2005/04/29
Messages: 3
Offline
|
|
|
|
now() is a mysql-function, not a java-function. So you should change you code to: PreparedStatement pstmt = con.prepareStatement( "insert into registration(username, password, timestamp) values (?, ?, now())"); pstmt.setString(1, username); pstmt.setString(2, password); pstmt.executeUpdate();
|
|
|
|
 |
| |
|
2007/12/06 10:31
|
|
|
|
|
I'm not sure what context this ps is being used in, but I want something similar for the default value to be generated when a new row is inserted into the database.
The solution you have given will probably work but it goes against the MDA concept and isn't round trip reversible.
How do I add a generated timestamp value for this field that is fully reversible and specify it from the model rather than the code?
|
|
|
|
 |
| |