The rowset of jdk1.5 learning (I.)

Source: Internet
Author: User
Tags interface modify mysql

There is a rowset interface in the jdk1.4 javax.sql package, but there is no specific implementation of the class. After "Tiger" was born, the five Javax.sql.rowset in the package and the corresponding five implementation classes in the Com.sun.rowset package were introduced, so we used powerful rowset. The five rowset interfaces in jdk1.5 are Jdbcrowset,cachedrowset,webrowset,joinrowset and Filteredrowset, and they bring powerful features and easy operations to our database development.

My Java environment is: RedHat 9

JDK 1.5.1_01

Eclipse 3.1M4

MYSQL 4.1.10 (JDBC Driver:org.gjt.mm.mysql.Driver)

I first created two tables in the test database for this study, and here's the script:

CREATE TABLE table1 (id int not NULL, name varchar is not NULL);

CREATE TABLE table2 (ID int not NULL, info varchar () is not null);

Then I inserted some data for testing. The following content in my environment, you can only modify the appropriate place for your own test.

Rowset objects can be divided into two categories: connected and connectionless. Jdbcrowset is the only connected implementation, and the traditional resultset is the same, the implementation of the connection is based on the JDBC-driven connection, the database connection is throughout the operation of the database. The connectionless implementation is based on reader and writer Stream connection, the need to read data and write data to establish a connection, the entire operation is disconnected, the following four interface objects are connectionless implementation. Below I will introduce each of their functions for each interface.





Jdbcrowset Interface:

My understanding is that this interface basically has a similar function to resultset, except that its result set defaults to Resultset.type_scroll_insensitive and resultset.concur_updatable, That is, the default result set can be scrolled up and down and updatable.

Because itself the rowset interface is the ResultSet sub-interface, So 1.5 inside all rowset have ResultSet method, and Jdbcrowset is only in the default attributes and resultset difference, so it's the result set of operation and ResultSet are the same, I do not specifically introduced, you can refer to the API.

Let me introduce the Jdbcrowset creation method, which is based on the traditional JDBC connection to the database:

Class.forName ("Org.gjt.mm.mysql.Driver");

Connection conn=drivermanager.getconnection ("Jdbc:mydql://localhost:3306/test", "Root", "");

Statement stmt=conn.createstatement ();

ResultSet rs=stamt.executequery ("SELECT * from table1");

Jdbcrowset jrs=new Jdbcrowsetimpl (RS);

This creates an object (Jdbcrowsetimpl is the Com.sun.rowset package inside the implementation class, the text of the five interfaces in the package should have a implementation class), the object inside the data is the same as the data in Rs. Another way to create a method is to use the default construction method, and then the Set property gets the data, and the second method is recommended for personal use:

Jdbcrowset jrs=new Jdbcrowsetimpl ();

Jrs.seturl ("Jdbc:mydql://localhost:3306/test");

Jrs.setusername ("root");

Jrs.setpassword ("");

Jrs.setcommand ("SELECT * from table1");

Jrs.execute ();

The object created this way is the same result as the first method. Of course this method can connect to a data source, if we bind a data source in a context environment, the Jndi name is DataSource1, then the following code can get the object:

Jdbcrowset jrs=new Jdbcrowsetimpl ();

Jrs.setdatasourcename ("DataSource1")

Jrs.execute ();

After we get the object we can use the appropriate method to iterate, update, insert or delete the data.

I have 2 points to explain: first, the other four interface objects in addition to the Joinrowset creation method is basically the same, but the interface name and interface implementation class name is different, then I will no longer explain the method of creating objects. Second, although the Jdbcrowset default is scrollable and updatable, but this is also required database-driven support, I use the MySQL driver does not support the update result set, so you need to read the driver before the documentation.





CachedRowSet Interface:

It inherits from the rowset interface, and he is the parent of the other 3 implementations of the connectionless rowset, which means that the other 3 interfaces inherit it directly or indirectly. From the name we can know, it is the principle of reading data stored in the cache to do the appropriate operation.

To create an interface object in addition to the two creation methods above, there is also a way to pass a syncprovider in the construction method. I've said before that connectionless rowset are all based on streaming, so the syncprovider here is to provide specific reader and writer. The jdk1.5 document's sample coder has this implementation:

String provider= "Com.fred.providers.HighAvailabilityProvider"

CachedRowSet crs=new Cachedrowsetimpl (provider);

This allows us to set up specific reader and writer for rowset, but this is supported by a third party package. And the object we created with the parameterless constructor is using the default Syncprovider, which, of course, is enough for us. After you create an object, you can use the same method as the Jdbcrowset to modify the result set additions and deletions, but the only difference is that after updating the result set, you must call writer to write the data in the cache to the database, and the method is Crs.acceptchages ();

The most exciting feature that CachedRowSet offers is paging functionality. The problem with programmers before is how to deal with data paging without affecting performance, now with cachedrowset everything becomes so simple, see the following code:

CachedRowSet crs=new Caehedrowsetimpl ();

Crs.seturl ("Jdbc:mydql://localhost:3306/test");

Crs.setusername ("root");

Crs.setpassword ("");

Crs.setcommand ("SELECT * from table1");

Crs.setpagesize (5);

Crs.execute ();

while (Crs.nextpage ())

while (Crs.next ())

System.out.println (Crs.getint ("id" + "\t\t" +crs.getstring ("name"));

We set the number of rows per page before Crs.execute (), and reader reads the data with only the specified number of rows, so we avoid having to read all the data and then paging. Is it simple?



To be continued ....


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.