Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
While inserting date this code is ok for me there is no error

Java
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy");
  String date1 = sdf.format(jDateChooser1.getDate());
  SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yy");

java.util.Date date = df.parse(date1);
String query="Insert into customermilk values('"+l[p].getText()+"','"+new java.sql.Date(date.getTime())+"','"+mt+"',"+q1+","+t12+")";
x=stmt.executeUpdate(query);


But retrieving date from table it generate exception

Java
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yy");
java.util.Date date = df.parse(date1);
query="select * from customermilk where name='"+cust[i]+"' AND date='"+new java.sql.Date(date.getTime())+"'";
ResultSet rs2=stmt.executeQuery(query);

It generate exception : Exception =java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.

What I have tried:

But retrieving date from table it generate exception

Java
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yy");
java.util.Date date = df.parse(date1);
query="select * from customermilk where name='"+cust[i]+"' AND date='"+new java.sql.Date(date.getTime())+"'";
ResultSet rs2=stmt.executeQuery(query);

It generate exception : Exception =java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.
Posted
Updated 25-Apr-17 21:49pm
v2
Comments
Richard MacCutchan 26-Apr-17 3:56am    
Stop using string concatenation for SQL, it leaves you open to SQL injection, and the corruption and destruction of your database. And you can simplify your code by using proper DateTime types in your database.

Also, why are you getting a date as a Date type, and converting it to a String, just so you can parse it back to a Date?
Member 10962951 26-Apr-17 7:12am    
can you give example of date obj 1. insert into 2. select from
these 2 queries
Richard MacCutchan 27-Apr-17 3:08am    
Check in the documentation for your database system, they are not all exactly the same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900