I want to update a column in a table through servlet. First I execute the query and display the values from table,its working fine,it is inserting values also, but not updating.In update block it generate sql exception.Below is part of code not working
sql = "update book set balance = " + "'" + h + "'" + " where bookid = " + "'" + bid + "'" ;
pst = c.prepareStatement(sql); pst.executeUpdate(); c.commit(); c.setAutoCommit(true); where bid is a string.I tried to run the block without where clause but even then its not working.Please guide,where I'm wrong.
Assuming "c" is a Connection object, you should be able to do the following: c.setAutoCommit( false ); Statement stmt = c.createStatement(); stmt.setQueryTimeout( 0 ); stmt.executeUpdate( sql ); stmt.close(); c.commit(); c.close();
what is the error message? Does the SQL statement work in a SQL*Plus session(or equivalent)? If you want to continue utilizing PreparedStatements you should change your code to:
if balance is a number then your query statement in your original code should be changed to sql = "update book set balance = " + h + //remove single quotes " where bookid = " + "'" + bid + "'" ; hope this helps Jamie
If you intend to you preparedStatement, pattern suggested by Jamie is the one you need. In addition, if you want to include a quote within string of sql query, you may do this: query="update table1 set id='"+var1+"',name='"+varname+"'"; Hope this help.