Visual Paradigm Logo
     Build Quality Applications Faster, Better and Cheaper
 
Home | Products | Download | Documentation | Support | Resources | Partners | Quick Tour | Shop | Company |  
 
   Visual Paradigm Discussion Forum
  Login   [ Register ] Email: Password: Log me on automatically I lost my password  
Recent Topics
Advanced Search
jdbc-mysql statement, timstamp now()  
Forum Index -> Java Questions
Author Message
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
 
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
 
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();
 
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();
 
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();
 
Anonymous

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?
 
 
   Forum Index -> Java Questions
Go to:   
Powered by JForum 2.1.4 © 2005 - Rafael Steil