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