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.