I wrote a java code for inserting the current date but when I try to run it exception occurs:
public void Session_Table_Update (String Update_User) throws SQLException{
String SQL_Statement = null;
error_Message = null;
if (ds == null) throw new SQLException( error_Database = "No data source");
Connection conn = ds.getConnection();
if (conn == null) throw new SQLException( error_Database = "No connection");
try {
conn.setAutoCommit(false);
boolean committed = false;
try {
SQL_Statement = "INSERT INTO USERS (LAST_LOGIN) VALUES (?,?,?) WHERE USERZ ="+ Update_User;
PreparedStatement insertQuery = conn.prepareStatement(SQL_Statement);
insertQuery.setString(3, "2.2.2011");
insertQuery.executeUpdate();
conn.commit();
committed = true;
} finally {
if (!committed) conn.rollback();
}
}
finally {
conn.close();
}
return;
}
Can you help me to fix the problem?
You have three parameters (
?characters) in your query, so you need to set that many parameters. You’re only setting one of these (the third one), so you need to haveOr another
set*method depending on your data type. See http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.htmlAs BalusC says, to set a date it would be better to use the
setDate()method on yourPreparedStatement. However, as you’re using Oracle, you may also consider using theTO_DATEOracle function (see http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions183.htm), which converts a string to an Oracle date. e.g.The format string can be changed to suit your needs