Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PreparedStatement and sysdate

 
Lukas Alamar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
i'm trying to make an insert into an oracle db table
and i need to use the db system date:
String query = insert into myTable (my_date) values (?);
PreparedStatement ps = con.prepareStatement(query);
//insert code here to bind the variable in the prep stat
ps.executeUpdate;
if i wasn't using a prep stat i'd simply write
"insert into myTable (my_date) values (sysdate)",
but i have (for some reasons) to use the prep stat...can i still use the db system date???
what i DO NOT want to do is this:
ps.setTimestamp(new Timestamp(System.currenttimeMillis()));

can you please enlighten me on this??
thanx
[ April 22, 2004: Message edited by: Lukas Alamar ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


if i wasn't using a prep stat i'd simply write
"insert into myTable (my_date) values (sysdate)",
but i have (for some reasons) to use the prep stat...can i still use the db system date???

Did you try it?
(Why not?)
If you like to use a '?', try:
pQuery = "INSERT INTO myTable (my_date) \
VALUES (SELECT sysdate FROM dual WHERE 1 = ?)";
but I can imagine you may use the simple above statement.
 
Jason Steele
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you do not need to use a prepared statement for an Oracle internal function like sysdate.

String query = insert into myTable (my_date) values (?);
PreparedStatement ps = con.prepareStatement(query);
//insert code here to bind the variable in the prep stat
ps.executeUpdate;

That preparedStatement is waiting for a parameter.
You could use:

But then you might as well just forget the prepared statement...unless this is something happening often.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34671
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you want to do it this way?
ps.setTimestamp(1, new Timestamp(System.currenttimeMillis()));
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic