Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Database Insert With java.util.Date  RSS feed

 
david k
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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??
 
Bill Siggelkow
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • 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
  • 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 ..

 
Kapil Keskar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!