Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to update ?  RSS feed

 
Ranch Hand
Posts: 73
  • 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.
 
Ranch Hand
Posts: 52
  • 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();
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • 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
 
Ranch Hand
Posts: 155
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!