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

O

ojvm

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();
 
C

carlo.pellegrini

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

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

ps.setDate(1,new java.sql.Date(fecha.getTimeInMillis()));
ps.execute();
 
O

ojvm

tanks for the answer but i need pass a string as date, because the
method will recive a string with the value of some date. so i need
store the value in the format of dd/mm/yyyy.

tanks again.
 
C

carlo.pellegrini

So, if you need to store the date in the database as String there is:

java.util.Calendar fecha = java.util.Calendar.getInstance();
java.text.SimpleDateFormat df=new
java.text.SimpleDateFormat("dd/MM/yyyy");
ps.setString(1,df.format(fecha.getTime()));

And if you need to set a date in the db from a String:
String aDate="01/01/2005";
java.text.SimpleDateFormat df=new
java.text.SimpleDateFormat("dd/MM/yyyy");
ps.setDate(new java.sql.Date(df.parse(aDate).getTime()));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top