Java PreparedStatement - a SQL UPDATE example

Java SQL FAQ: Can you provide a Java PreparedStatement example that shows how to use a SQL UPDATE?

Sure. I have quite a few examples on this website, just see the "Related" section for those. But for now, here's a short sample method that performs a JDBC SQL UPDATE using a Java PreparedStatement:

// a java preparedstatement example
public static void updateDescriptionAndAuthor (
  Connection conn,
  String description,
  int id,
  int seqNum,
  String author
throws SQLException
    // create our java preparedstatement using a sql update query
    PreparedStatement ps = conn.prepareStatement(
      "UPDATE Messages SET description = ?, author = ? WHERE id = ? AND seq_num = ?");

    // set the preparedstatement parameters

    // call executeUpdate to execute our sql update statement
  catch (SQLException se)
    // log the exception
    throw se;

I'll skip the description of this PreparedStatement UPDATE example for now, but if you have any questions or comments, just leave them below, and I'll be glad to respond to them.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.