aspose file tools*
The moose likes JDBC and the fly likes Question for insertint to Date field. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Question for insertint to Date field." Watch "Question for insertint to Date field." New topic
Author

Question for insertint to Date field.

Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
I am confused what the proper way to insert into a Date field. Sybase is the database.
Sould I format the date as a string that Sybase will except as a Date, or is there a better approach.
If someone could provide a simple example of doing an insert into tablename ...... where one of the fields is of type Data, that would be very, very helpful.
Thanks in advance.
Rob
Prashanth menon
Ranch Hand

Joined: Feb 20, 2001
Posts: 65
Originally posted by Rob Levo:
I am confused what the proper way to insert into a Date field. Sybase is the database.
Sould I format the date as a string that Sybase will except as a Date, or is there a better approach.
If someone could provide a simple example of doing an insert into tablename ...... where one of the fields is of type Data, that would be very, very helpful.
Thanks in advance.
Rob

Hi rob,
I have no Idea if Sybase uses a particular format for date inserts. But you can always use prepared statements I suppose.
PreparedStatement pstmt =
conn.prepareStatement ("insert into EMP (EMPNO, ENAME) values (?, ?)");
pstmt.setInt (1, 1500); The first ? is for EMPNO
pstmt.setString (2, "LESLIE");// The second ? is for ENAME
If Date is one of the fields. we can use
pstmt.setDate(1,date);
date is a java.sql.Date object.
It works with oracle. I think its no different for Sybase
Hope this would help.
regards
Prash.
Michael Zalewski
Ranch Hand

Joined: Jun 10, 2002
Posts: 30
If you have myDate as type Date, just do
PreparedStatement psMyStatement = myConnection.prepareStatement( [my insert text]);
... Set columns 1 .. N-1
psMyStatement.setDate( N, new java.sql.Date( myDate));
... set columns N+1..
psMyStatement.executeUpdate();
You need two things.
1. Use a PreparedStatement instead of trying to build the INSERT statement as a String.
2. Bind your Date values using new java.sql.Date( java.util.Date). Or create the variables as java.sql.Date from the beginning.
Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
Sounds like the PreparedStatement is the way to go rather than trying to get the string format correct.
Thanks all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question for insertint to Date field.