• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Database Insert With java.util.Date

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having a problem with a database insert. Here is the code
for the function:
public void executeEFormInsertTest(String d, String dd,
String ddd, String user, String day1, String code1, String hours1) {
try {
conn = DriverManager.getConnection(DB_DSN,DB_USER,DB_PWD);
pstmt = conn.prepareStatement(insertEFormTest);

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
java.util.Date date1 = sdf.parse(d);
java.util.Date date2 = sdf.parse(dd);
java.util.Date date3 = sdf.parse(ddd);

pstmt.clearParameters();
pstmt.setDate(1, date1);
pstmt.setDate(2, date2);
pstmt.setDate(3, date3);
pstmt.setString(4, user);
pstmt.setString(5, day1);
pstmt.setString(6, code1);
pstmt.setString(7, hours1);
pstmt.executeUpdate();
//pstmt.close();
}
catch(SQLException se) {
System.out.println(se.toString());
}
finally {
if(pstmt !=null) {
try {
pstmt.close();
conn.close();
} catch(Exception e) {
System.out.println("Error while closing db connection");
}
}
}
} // executeEFormInsertTest()
The compiler complains that the [ pstmt.setDate(1,date1); etc..]
calls are not able to be resolved. This is strange to me?? The dates are passed to the function as String objects and I convert them to a java.util.Date using the SimpleDateFormat class.. Any suggestions??
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You probably need to use a java.sql.Date (it is a subclass of java.util.Date
 
david k
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much..
I will look at using it. It seems that part of my data
requirements have changed, of the seven arguments passed
to my function the first two are dates which I was able to
pass to the database as a String ..

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use sdf.format(dt1)
& as per my knowledge it doesn't work with preparedstatement.but you can use with normal statement.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic