Inserting records into database

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

[1252 byte] By [goBIGRED] at [2007-9-26 4:20:59]
# 1

There are 2 options that I can see here:

1. Send the current date/time from Java to the DB. This will set the host machine's date.

java.util.Date dt = new java.util.Date();

ps.setDate(5, new java.sql.Date(dt.getTime()));

2. If the DB is on some other machine running in a different timezone (which may not be the case most of the times), have the DB set the date field for you. Here is a sample for oracle.

String sql = "INSERT into hldy(HLDY_CODE, HLIDY_DESCR";

sql += ", HLIDY_DATE, USERID, TIME_STAMP) values (?, ?, ?, ?, sysdate)"; // May not work for all DBs?

ps = conn.prepareStatement(sql);

stmt = conn.createStatement();

I would suggest using the first approach - the DB-independent way.

Hope that helps.

Regards,

Vijay

vijayreddy at 2007-6-29 17:24:38 > top of Java-index,Archived Forums,Java Programming...