File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to update ?

 
Ghazala Islam
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic