This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question for insertint to Date field.

 
Rob Levo
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like the PreparedStatement is the way to go rather than trying to get the string format correct.
Thanks all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic