This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Inserting records into database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Inserting records into database" Watch "Inserting records into database" New topic
Author

Inserting records into database

Chak Terlapu
Ranch Hand

Joined: Oct 20, 2000
Posts: 32
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

Joined: Jul 26, 2001
Posts: 60
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
 
 
subject: Inserting records into database
 
Similar Threads
req.getParameterValues returning null
How to insert all the records except the failed one.
Handling SQL Exception
Insert into MS Access with PreparedStatement
Using JDBC how to put single quotes(')