JDK 1.5 LEARNING Rowset

Source: Internet
Author: User
Tags filter define functions connect sql mysql net string

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:o Rg.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 () not null); CREATE table table2 (ID int is 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 implementation without connectionis 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, which means that 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 has two methods, one is based on the traditional JDBC Connection database method: 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 is the same result as the first method. Of course this method can connect a data source, if we are in the context of a data source binding, Jndi name is DataSource1, then the following code can get objects: Jdbcrowset jrs=new Jdbcrowsetimpl (); Jrs.setdatasourcename ("DataSource1") Jrs.execute (); After we get the object, we can use the appropriate method to traverse, 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.   Creating 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) so we 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 (); CachedRowSet offers the most exciting featuresis the paging function. The problem that programmers have a headache before is how to deal with data paging without affecting performance, now with cachedrowset everything becomes so simple, please 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 ( "id" + "\t\t" +crs.getstring ("name"); we set the number of rows per page before Crs.execute (), so reader reads the data with only the specified number of rows. This avoids having to read all the data at once and then paging. Is it simple?

Joinrowset interface: This interface allows us to join the result set directly in a connectionless state. The following code provides the implementation of the Joinrowset: CachedRowSet crs1=new Caehedrowsetimpl (); Crs1.seturl ("jdbc:mydql://localhost:3306/test"); Crs1.setusername ("root"); Crs1.setpassword (""); Crs1.setcommand ("Select * from table1"); Crs1.execute (); CachedRowSet crs2=new Caehedrowsetimpl (); Crs2.seturl ("jdbc:mydql://localhost:3306/test"); Crs2.setUsername ("Root" ); Crs2.setpassword (""); Crs2.setcommand ("Select * from table2"); Crs2.execute (); Joinrowset jrs=new Joinrowsetimpl (); Jrs.addrowset (crs1, "id"); Jrs.addrowset (CRS2, "id"); while (Jrs.next ()) System.out.println (Jrs.getint ("id") + "\t\t" +jrs.getstring ("name") + "\t\t" +jrs.getstring ("info"); This code functions and executes a SELECT * FROM table1 INNER JOIN table2 The result set on the Table1.id=table2.id statement is the same. But I personally think it's better to use this join statement to get cachedrowset than to use joinrowset in such a complex way. The default join is inner JOIN, and the interface also supports cross Join,full join,left outer join and right outer join, and we modify the connection type through the Setjointype () method. Of course, this still requires the support of the database. And one notable thing is that in this case, the columns I connect to are called IDs in two, so we use ID when we fetch the data, what if the names of the two columns are different? The system will list a default name for this connection called "Merge."Dcol ". Filteredrowset interface:. NET Ado.net support to obtain result sets using certain conditions to filter to get different results, then now jdk1.5 can do, Filterrowset interface allows us to flexibly define the filter conditions to achieve different effects. The predicate interface in the Javax.sql.rowset package is this filter, we define the filter condition by implementing this interface, the following is the schematic code: public class Filter implements predicate {private int min;private int max;private string colname;public Filter (int min, int max, String colname) {this.min=min; this.max=ma X This.colname=colname;} public Boolean Evaluate (RowSet rs) {CachedRowSet crs= (cachedrowset) rs;if (Crs.getint (colname) >min) && ( Crs.getint (colname) <max)) return true;         else return false;}} Here we use this filter to filter out the data between Min and Max: Filteredrowset frs=new filteredrowset (); Frs.setcommand ("Select * from table1"); Frs.execute ();//Get all the data first; Frs.setfilter (new Filter (1,20, "id");//filter out data with ID value not between 1 and 20 because the method inside the Prdicate interface is flexible, So we have the flexibility to set the filter conditions so that we can get different results from just one statement.

Webrowset interface: XML because its platform independence is becoming more and more popular with developers, it is also a good choice for data persistence, Webrowset encapsulates the method of reading and writing XML, we can easily persist the data of the database to XML or write the data from XML to the database. The way to write to an XML file is Wrs.writexml (new FileOutputStream ("Data.xml")), and the result is that the data in memory is written to the Data.xml file in the current directory. Three types of data are recorded in this XML file: properties: Including Setxxx () method all the attributes, not set is the default properties metadata: including the database table related metadata, corresponding to the information in the ResultSetMetaData data: The result set of all the data from the XML file to load the rowset method is ReadXml (...); XML, as long as it is written in the canonical format, can be loaded in.

Conclusion: five rowset interface respectively there are some methods, due to space is limited, I only listed some of the typical methods, I hope this article for you to further learn jdk1.5 help!


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.