Problem changing from a String to a Date.

Hey everyone,

I am working on my very first real life program, for my portfolio.

I am making a booking sheet.

I have the booking sheet working very well, I canstore/search/update the database ok. but my database is MS Access.

I have my first column set as a string, which just shows the date but as a String.

I used SimpleDateFormat("EEEE dd MMMM yyyy") to format how I wanted the Date to look.

as I said this works fine.

now my problem is, I want to add a usefull feature, which would be a statistics area, so I thought I would need to convert the String to a real Date obeject. so that I can order the date and count any integers I store.

I have tried to do this in my database class where I store and update my database.

PreparedStatement ps =null;

SimpleDateFormat sdf =new SimpleDateFormat("EEEE dd MMMM yyyy");//added

Date d = (Date)sdf.parse(date);//added

System.out.println(d);//added

try

{

con = DriverManager.getConnection(dbURL,"","");

s = con.createStatement();

s.execute("SELECT * FROM "+tableName+" WHERE column_1 = '"+date+"'");//might need to change date to d

ResultSet rs = s.getResultSet();// get any ResultSet that came from our query

boolean found = rs.next();

if (rs !=null)

{

if(!found)

{

String insert ="INSERT INTO " +tableName+" VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

ps = con.prepareStatement(insert);

ps.setDate(1, d);//fails here

....

I get an eception- but the exception I get is: java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date

How can I get round this to make my String a Date object so that I can store it to my database?

davy

p.s. I changed my database field from String toLong Date in Access

Message was edited by:

davyk

[2625 byte] By [davyka] at [2007-11-27 3:53:31]
# 1
are you using java.sql.Date or java.util.Date ?*Edit*(In case it's not obvious you are using java.util.Date when you should be using java.sql.Date)do this : ps.setDate(1, (java.sql.Date) d);
Aknibbsa at 2007-7-12 8:57:35 > top of Java-index,Java Essentials,Java Programming...
# 2
I had in my imports:import java.sql.*;import java.text.*;import java.sql.Date; //swapped with one below this//import java.util.Date.*;import java.text.DateFormat.*;and I also tried your suggestion, still same problem...davy
davyka at 2007-7-12 8:57:35 > top of Java-index,Java Essentials,Java Programming...
# 3
put your code to the way is was when you started and only put in the (java.sql.Date) in the call to setDatetry that.
Aknibbsa at 2007-7-12 8:57:35 > top of Java-index,Java Essentials,Java Programming...
# 4
same error i am afraiddavy
davyka at 2007-7-12 8:57:35 > top of Java-index,Java Essentials,Java Programming...
# 5
post the exact error - with the full stack trace
Aknibbsa at 2007-7-12 8:57:35 > top of Java-index,Java Essentials,Java Programming...
# 6

here is the full stack trace

java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date

at Database.data(Database.java:41)

at TestBookingSheet.save(TestBookingSheet.java:400)

at TestBookingSheet.actionPerformed(TestBookingSheet.java:422)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)

at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

davy

davyka at 2007-7-12 8:57:35 > top of Java-index,Java Essentials,Java Programming...
# 7
To go from java.util.Date to java.sql.Date:java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
DrLaszloJamfa at 2007-7-12 8:57:35 > top of Java-index,Java Essentials,Java Programming...
# 8

thanks guys,

it worked first time, now I have other errors now...

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.

but I will look into this, cause its from another class, one that looks into the database and gets the info

davy

davyka at 2007-7-12 8:57:35 > top of Java-index,Java Essentials,Java Programming...
# 9
You can try thispublic static Date getDateWithoutTime(String pDate) throws ParseException {SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");Date date = formatter.parse(pDate);return date;}
rajnikanthgoldstonea at 2007-7-12 8:57:35 > top of Java-index,Java Essentials,Java Programming...