JDBC ResultSet Updatable result set

Source: Internet
Author: User

Then prev: scrollable result set;

1) Com.microsoft.sqlserver.jdbc.SQLServerException: The result set cannot be updated.

When concurrency is set to:resultset.concur_read_only (default), the result set cannot update the data, otherwise it will be reported sqlserverexception

Example:

statement = con.preparestatement (sql, resultset.type_scroll_sensitive, resultset.concur_read_only); rs = Statement.executequery (); Rs.absolute (3);//result set scrollable rs.updatestring ("Strfeaturecode", "hwp_332"); Rs.updaterow (); Update result set, error SQLServerExceptionSystem.out.println (rs.getstring ("Virusid") + "" +rs.getstring ("Featurecode") + "" + Rs.getstring ("apppackage") + "" "+rs.getint (" Virustype ") +" ");

2) Com.microsoft.sqlserver.jdbc.SQLServerException: This cursor type/concurrency combination is not supported.

When concurrency is set to:resultset.concur_updatable, the type must be type_scroll_sensitive, and the result set can update the data. But updates are not absolute. The following conditions need to be noted:

1) Some database drivers do not support this operation, such as Oracle, then this is only jdbc2.0 wishful thinking, although the interface is defined, but the interface is not fully implemented. Therefore, it is also best not to use result sets to update data for compatibility.

2) Online said if "select * from Tbl_virusbaseinfo", then will not be able to use the result set update, but in fact, I use the SQLServer2005 database for test discovery, not only can update the value of the property, and the value of the property is case-insensitive, In other words, you can update the same effect as follows:

Rs.updatestring ("Strfeaturecode", "Hello_kitty"); Rs.updaterow (); System.out.println (rs.getstring ("Uidvirusid") + "" +rs.getstring ("Strfeaturecode"));


3) If the SQL statement when querying the result set is an associative query, then it is not possible to update the data source through the result set, which makes sense as if we cannot modify the source table's data through a complex view.

Example:

statement = con.preparestatement (sql, resultset.type_scroll_sensitive, resultset.concur_updatable); rs = Statement.executequery (); Rs.absolute (3); The result set can be scrolled rs.updatestring ("Strfeaturecode", "hwp_332"); Rs.updaterow (); Update result set SYSTEM.OUT.PRINTLN (rs.getstring ("Virusid") + "+rs.getstring (" Featurecode ") +" "+rs.getstring (" Apppackage ") + "" +rs.getint ("Virustype") + "");

3) About updating result set-related operations

A: Update only, at which point the resultset cursor points to the current row and does not move, but his data is already up-to-date in the database and Java

rs = Statement.executequery (); Rs.absolute (3); Rs.updatestring ("Strfeaturecode", "hwp_332"); Rs.updaterow (); Rs.getstring ("Strfeaturecode") is hwp_332

B: New data, first we want to point the cursor to the row where the data can be inserted, and then into the data, pointing to the insert operation. It is important to note that some people say that the first action is to move the cursor to the back of the last row, not really, if so why should the API Movetoinsertrow this method? So it's still different from the afterlast. In addition, when we insert the data, the cursor has moved to the position behind the last line, so it is necessary to movetocurrentrow to get the result set just now. Also, the result set gets failed, but the entire operation is not rolled back, the data has been completed and data consistency is ensured.

Rs.movetoinsertrow (); Rs.updatestring ("Uidvirusid", "ID_6FFFF7"); Rs.updatestring ("Strfeaturecode", "Heddddllo_ Kittyd "); Rs.insertrow (); Rs.movetocurrentrow ();

C: Delete the data, first use the cursor to locate the location of the deleted row, and then perform the delete operation, after the deletion of the cursor or point to delete the row, that is, the current row, if you also go to get data, then the error, because the deleted data can not be obtained, although the error, but the entire operation will not be rolled back

Rs.absolute (3); Rs.deleterow ();

For a specific script, see previous article: scrollable result set.

JDBC ResultSet Updatable result set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.