Jdbc-mysql statement, timstamp now()

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

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

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();

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();

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();

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?