• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using prepared statements

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I'm creating a web application which displays record stored in an mysql database. I have created a PreparedStatement method as follows:

public static void doPreparedStatements(Connection conn, ResultSet rsClients, int rowInt){

String sql = "UPDATE clients SET firstName = ? WHERE clientID=" +rowInt;
PreparedStatement ps = null;
System.out.println("sql = " +sql);

try {
// Updating the database.
ps = conn.prepareStatement(sql);
ps.setString(1, rsClients.getString("firstName"));
ps.executeUpdate();
} catch (SQLException sqle) {
System.err.println("Error! Opening connection has failed: " + sqle);
sqle.printStackTrace();
}
}

I'm very new to jdbc and sql and I cannot see what is wrong with the sql statement above - it does not error and it does not update the mysql database.

Can anyone see anything wrong with sql.

Any help will be greatly appreciated.

Cheers Joe.
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, I'd write it like this:



Second, there of course needs to be a row that matches in your database.

Third, if you've set autocommit off (somewhere else), which you generally should do, you will need to commit your changes.
[ May 01, 2006: Message edited by: stu derby ]
 
stu derby
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh.. Now that I look at it more closely, I see that you're passing in a ResultSet.

ResultSets are always positioned BEFORE the first row, you have to call ResultSet.next() to advance the next row.

Also, while I don't think this is a problem you have, keep in mind that re-executing the same statement will close a previous ResultSet.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic