aspose file tools*
The moose likes JDBC and the fly likes How to update ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to update ?" Watch "How to update ?" New topic
Author

How to update ?

Ghazala Islam
Ranch Hand

Joined: Nov 22, 2000
Posts: 73
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.
John Sinues
Ranch Hand

Joined: Feb 21, 2000
Posts: 52
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();
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

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
SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to update ?