Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting records into database

 
Chak Terlapu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to database connectivity using servlets. I have a form which on clicked should insert data into database. I am able to get the data from the fields as follows:
String HLDY_CODE = req.getParameter("HOLIDAY_CODE").trim();
String HLDY_DESCR = req.getParameter("HOLIDAY_DESCR").trim();
String HLDY_DATE = req.getParameter("HOLIDAY_DATE").trim();
String USERID = req.getParameter("LST_CHANGE_USERID").trim();
I am trying to insert these into the database. I tried the following way but its not working.
String sql = "INSERT into hldy(HLDY_CODE, HLIDY_DESCR";
sql += ", HLIDY_DATE, USERID, TIME_STAMP) values (?, ?, ?, ?, ?)";
ps = conn.prepareStatement(sql);
stmt = conn.createStatement();
//inserting records
ps.setString(1, HOLIDAY_CODE);
ps.setString(2, HOLIDAY_DESCR);
ps.setString(3, HOLIDAY_DATE);
ps.setString(4, LAST_CHANGE_USERID);
I also need to insert the time stamp when the record is being inserted. The fields of "hldy" table are HLDY_CODE, HLDY_DESCR, HLDY_DATE, USERID, TIME_STAMP. How can I find the time the record is being inserted?? Can someone point me in the right direction please.
Thanks
Chak
 
Manjunath Reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this...and im assuming you are successfull in getting a connection to the database through your driver manager.
String sql = "INSERT into hldy(HLDY_CODE, HLIDY_DESCR";
sql += ", HLIDY_DATE, USERID, TIME_STAMP) values (?, ?, ?, ?, ?)";
ps = conn.prepareStatement(sql);
//stmt = conn.createStatement();(what was this doing here ???)

//depends on limitations of database timestamp storage types
Timestamp tsDateTime = new Timestamp(new Date().getTime());
//inserting records
ps.setString(1, HOLIDAY_CODE);
ps.setString(2, HOLIDAY_DESCR);
ps.setString(3, HOLIDAY_DATE);
ps.setString(4, LAST_CHANGE_USERID);
ps.setString(5, tsDateTime );
int iInsertCount = pStmt.executeUpdate();
cheers,
mpr
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic