JDBC ResultSet scrollable result set

Source: Internet
Author: User

Scrollable result set

1)Com.microsoft.sqlserver.jdbc.SQLServerException: The requested operation is not supported for a forward-only result set.

When TYPE is set to:resultset.type_forward_only (default), the cursor cannot be moved arbitrarily, but only step forward, otherwise it will be reported sqlserverexception

Example:

statement = con.preparestatement (sql, resultset.type_forward_only, resultset.concur_xxx); rs = Statement.executequery (); Rs.absolute (2); System.out.println (rs.getstring ("Virusid") + "" +rs.getstring ("Featurecode") + "" +rs.getstring ("apppackage") + "" + Rs.getint ("Virustype") + "");

2) Com.microsoft.sqlserver.jdbc.SQLServerException: The result set does not go forward.

When TYPE is set to:resultset.type_scroll_insensitive or resultset.type_scroll_insensitive, the cursor can be moved, but the position moved is [1,count], Remember not to start from 0, or you will get an error.

Now that you can move, explain a few ways to move:

rs = Statement.executequery (); The cursor points to the position in front of the first line, where the data cannot be retrieved or an error occurs: The result set does not go forward

Rs.next (); The cursor moves down one position and returns true if there is a result set in place, otherwise false

Rs.previous (); The cursor moves up one position and returns true if there is a result set in place, otherwise false

Rs.first (); Where the cursor points to the first row

Rs.last (); Where the cursor points to the last row

Rs.beforefirst (); The cursor points to the position in front of the first row, where the data cannot be obtained

Rs.afterlast (); The cursor points to the position following the last line, which cannot fetch data

Rs.absolute (index); Cursor moves to index position, index is any number of [1,count], but cannot exceed, otherwise error

Rs.relative (index); The cursor moves the index position from the current position, that is, relative movement, index can be negative, but the result is also within [1,count]

Isafterlast (); Determines whether the cursor is after the last row.

Isbeforefirst ();//Determines whether the cursor precedes the first row.

Iffirst (); Determines whether the cursor points to the first row of the result set.

Islast (); Determines whether the cursor points to the last row of the result set.

GetRow ();//Gets the line number of the row to which the current cursor is pointing, starting with line number 1, or 0 if the result set has no rows.

Example:

statement = con.preparestatement (sql, resultset.type_scroll_insensitive, resultset.concur_xxx); rs = Statement.executequery (); Rs.absolute (1); System.out.println (rs.getstring ("Virusid") + "" +rs.getstring ("Featurecode") + "" +rs.getstring ("apppackage") + "" + Rs.getint ("Virustype") + "");

3)differences between resultset.type_scroll_insensitive and Resultset.type_scroll_insensitive

Many people say that the former is insensitive to data changes in the database and the latter is sensitive. But when I used the SQLServer2005 test, I really did not see the difference between the two, if someone saw can be posted code to learn from each other .....

In addition, the development process is seldom used, I think type_scroll_insensitive is enough.

Attention:

Pre-run Script:

IF not EXISTS (SELECT * from sysobjects WHERE id = object_id (' tbl_virusbaseinfo ') and type = ' U ') BEGIN CREATE TABLE Tbl_vi Rusbaseinfo (uidvirusid varchar) NOT NULL, Strfeaturecode varchar (null,strapppackage varchar) NULL, Ivirustype int null, CONSTRAINT pk_virusbaseinfo PRIMARY KEY (uidvirusid)) END Go
  public static connection  Getconnection () {connection con = null;try {string username =  "sa";  //   Username string password =  "123"; //  Password Class.forName (" Com.microsoft.sqlserver.jdbc.SQLServerDriver ");//  load the driver class Con = drivermanager.getconnection (" JDBC: Sqlserver://127.0.0.1:1433;databasename=testdb ", Username, password);//  Get the connection} catch  ( classnotfoundexception e)  {e.printstacktrace ();}  catch  (sqlexception e)  {e.printstacktrace ();} Return con;}  
@Testpublic void Resultsettest () {Connection con = getconnection (); String sql = "Select Uidvirusid,strfeaturecode,strapppackage,ivirustype from Tbl_virusbaseinfo"; PreparedStatement statement = null; ResultSet rs = null;try {statement = con.preparestatement (sql, resultset.type_scroll_sensitive, Resultset.concur_read_ only); rs = Statement.executequery (); Rs.absolute (3); System.out.println (rs.getstring ("Uidvirusid") + "" +rs.getstring ("Strfeaturecode")); catch (SQLException e) {e.printstacktrace ();} Finally{try {if (rs! = null) {Rs.close ();} if (statement! = NULL) {Statement.close ();} if (con! = null) {Con.close ();}} catch (SQLException e) {e.printstacktrace ();}}

Finally: Leave a question about how sensitivity and insensitivity are differentiated.

Next article: Updatable result sets





JDBC ResultSet scrollable 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.