Offline dataset (rowset) in JDBC)

Source: Internet
Author: User

If you program using JDBC APIs directly, you can use the resultset interface to read data, but you may find it inconvenient because resultset is an online dataset, when reading data, you cannot disconnect the database. You can close related objects only after the data is read.

In fact, Java also provides offline datasets, that is, the rowset interface and related subinterfaces. In addition, Sun provides a default implementation in JDK, and also provides its own rowset implementation in large database drivers such as oracle.

The following uses Sun's default implementation to describe how to use offline data. The database is SQL Server 2000 and the connected database is northwind.

Package EG; <br/> Import Java. SQL. connection; <br/> Import Java. SQL. drivermanager; <br/> Import Java. SQL. resultset; <br/> Import Java. SQL. sqlexception; <br/> Import Java. SQL. statement; <br/> Import javax. SQL. rowset; <br/> Import COM. sun. rowset. cachedrowsetimpl; <br/> public class rowsetdemo {<br/> Public static rowset query (connection con, string SQL) throws sqlexception {<br/> // use sun's default rowset implementation <br/> CAC Hedrowsetimpl rowset = new cachedrowsetimpl (); <br/> // the query has not changed. <br/> statement stat = con. createstatement (); <br/> resultset rs = stat.exe cutequery (SQL); <br/> // fill in the offline set <br/> rowset. populate (RS); <br/> // you can disable it. <br/> Rs. close (); <br/> stat. close (); <br/> return rowset; <br/>}< br/>/** <br/> * @ Param ARGs <br/> * @ throws classnotfoundexception <br/> * @ throws sqlexception <br/> */<br/> Public stat IC void main (string [] ARGs) throws classnotfoundexception, <br/> sqlexception {<br/> class. forname ("com. microsoft. sqlserver. JDBC. sqlserverdriver "); <br/> string conurl =" JDBC: sqlserver: // 127.0.0.1: 1433; database = northwind; user = sa; Password = zhangxs "; <br/> connection con = drivermanager. getconnection (conurl); <br/> rowset rs = query (con, "select * from customers order by customerid"); <br/> // close the connection.. <Br/> con. close (); <br/> // use the same as resultset <br/> while (RS. next () {<br/> system. out. print (RS. getstring (1) + ";"); <br/> system. out. println (RS. getstring ("companyName"); <br/>}< br/> // In fact, rowset can also complete the paging function. See the following method <br/> Public static rowset query (connection con, string SQL, int pagesize, <br/> int pagenumber) throws exception {<br/> cachedrowsetimpl rowset = new cachedrowsetimpl (); <br/> // result set that can be rolled <br/> statement stat = con. createstatement (resultset. type_scroll_sensitive, <br/> resultset. concur_read_only); <br/> resultset rs = stat.exe cutequery (SQL); <br/> // set the page size <br/> rowset. setpagesize (pagesize); <br/> // calculate the start cursor position <br/> int skip = (pagenumber-1) * pagesize + 1; <br/> // It can be filled <br/> rowset. populate (RS, skip); <br/> Rs. close (); <br/> stat. close (); <br/> return rowset; <br/>}< br/>
 

If you do not use the persistent layer, using offline datasets can solve the paging and database connection problems.

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.