Using Cursors

Another way to update or delete data is to use a cursor. While looping through the results of a query, you can modify, delete, or add records, but only if you set up the statement the right way.

By default, when you execute a query, it's run in a kind of “read-only” mode. You can step through the values using the next() method on the record set, but that's it.

If the code hands an extra pair of arguments to createConnection or prepareStatement, you can create a ResultSet that can move back and forth inside the query, as well as update and append to.

Unfortunately, as of the current release of the MySQL JDBC driver, this is a not-ready-for-prime-time feature. Luckily, most of the time you are better off doing an explicit insert or ...

Get MySQL™ and JSP™ Web Applications: Data-Driven Programming Using Tomcat and MySQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.