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]

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