Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / May 2005

Tip: Looking for answers? Try searching our database.

Exception trying to set the method "setDate" of class PreparedStatement

Thread view: 
ojvm - 06 May 2005 23:01 GMT
hi, i'm trying to set the date to method setDate of class
PreparedStatement to save it in a database but it send me a error this
is the part of code where a i'm having troubles. thanks in advance.

PreparedStatement ps = cn.prepareStatement("INSERT INTO test (date)
VALUES(?)");

java.util.Calendar fecha = java.util.Calendar.getInstance();

ps.setDate(1,fecha.getTime());
ps.execute();
Joe Weinstein - 06 May 2005 23:43 GMT
> hi, i'm trying to set the date to method setDate of class
> PreparedStatement to save it in a database but it send me a error this
[quoted text clipped - 7 lines]
> ps.setDate(1,fecha.getTime());
> ps.execute();

Hi. The issue is that Calendar.getTime() returns a java.util.Date,
not a java.sql.Date. Create a java.sql.Timestamp or java.sql.Date
to pass to the setDate() call.

Joe Weinstein at BEA
Bjorn Abelli - 06 May 2005 23:47 GMT
"ojvm" wrote...

> hi, i'm trying to set the date to method setDate of class
> PreparedStatement to save it in a database but it send me a error this
[quoted text clipped - 6 lines]
>
>  ps.setDate(1,fecha.getTime());

You're trying to set a java.util.Date instead of a java.sql.Date.

As the handling of Times and Dates are different between Java and
standard-SQL, there's a number of other "Date-types" in the
java.sql-package, e.g:

 http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Date.html

One way of "converting" from your Calendar to a java.sql.Date, is to get the
milliseconds from your Calendar and using that constructor:

 PreparedStatement ps =
  cn.prepareStatement("INSERT INTO test (date) VALUES(?)");

 java.util.Calendar fecha = java.util.Calendar.getInstance();

 java.sql.Date d = new java.sql.Date(fecha.getTimeInMillis());

 ps.setDate(1, d);

// Bjorn A


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.