File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes How to update ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "How to update ?" Watch "How to update ?" New topic

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);
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 );
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
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.
I agree. Here's the link:
subject: How to update ?
It's not a secret anymore!