There is a RowSet interface in the JAVAX.sql package of JDK1.4, but there is no specific implementation class. After "Tiger" was born, the five sub-interfaces in the javax.sql.rowset package introduced five sub-interfaces in the javax.sun.rowset package, so that we used powerful RowSet. The five sub-interfaces in JDK1.5 are JDBcrowSet, CachedRowSet, WebrowSet, JoinRowSet, and FilteredRowSet, which brings power and convenient operation 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 for this study in the Test database Here is the script: Create Table Table1 (ID INT NOT NULL, NAME VARCHAR (20) NOT NULL); Create Table Table2 (ID Int Not Null, Info Varchar (20) Not Null; then I inserted some data for test. The following content is illustrated in my environment, and everyone can use their own test as long as you modify the corresponding place. The RowSet object can be divided into two categories: there is a connection and connectionless. JDBcrowSet is the only one with connections, and a traditional RESULTSET. The implementation of the connection is based on JDBC-driven connections. The database connection is the operation of the database throughout the database. The connectionless implementation is based on Reader and Writer streams. When you need to read data and write data, the connection is established, which is disconnected throughout the operation, and the back four interface objects are not connected. Realization. Here I will introduce their respective features for each interface. JDBCROWSET interface: My understanding is that this interface is basically similar to the resultSet, but its result set default is ResultSet.Type_Scroll_insensitive and resultset.concur_updatable, that is, the default result set is to scroll and updatable up and down. . Because the RowSet interface is the sub-interface of the ResultSet, all RowSets in 1.5 have a RESULTSET method, while JDBcrowSt is just different from the default attributes and resultTSet, so it is the same for the operational methods of the result set, and the result. I don't specifically introduce, you can refer to the API. Let me introduce the JDBcrowSet creation method. There are two ways, one is based on the traditional JDBC connection database: class.forname ("org.gjt.mm.mysql.driver); connection conn = drivermanager.getConnection ("JDBC: MyDQL: // Localhost: 3306 / Test", "root '," "); statement stmt = conn.createstatement (); resultset = stamt.executeQuery (" Select * from table1 "); JDBcrowset JRS = NEW JDBCROWSETIMPL (RS); this creates an object (JDBcrowSetiMPL is the implementation class in the com.sun.rowset package. The five interfaces in the text correspond to one implementation class in the package). The data in this object is and RS The data inside is the same.
There is also a method of creation is to use the default constructor, then set attribute to get data, personal recommendation use of the second method: JDBcrowset JRS = New JDBcrowsetImpl (); Jrs.SetURL ("JDBC: mydql: // localhost: 3306 / Test "); jrs.setUsername (" root "); jrs.setpassword (" ""); jrs.setcommand ("Select * from table1"); jrs.execute (); this created object is and the first method is The same result. Of course, this method can connect a data source. If we bind a data source in the 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 you get an object, we can use the appropriate way to traverse, update, insert or delete data. I have 2 points to explain: First, other four other interfaces are basically the same in addition to JoinRowSet creation, just the names of the interface and interface implementation classes, and I will no longer explain the way to create objects. . Second, although JDBCROWSET defaults to scroll and updatable, this is also a database-driven support. I use the MySQL driver that does not support update result sets, so you need to read the driver before you use the documentation. CachedRowSet interface: It inherits in the RowSet interface, and he is a unconnected Rowset's other three implementation of the parent interface, which means that the other three interfaces directly or indirectly. From the name we can know that its principle is that reading data is saved in the cache. Creating an interface object In addition to the above creation methods, there is a way to pass a SyncProvider in the constructor. I have said that there is no connected ROWSET written based on flow, then SyncProvider here is a specific Reader and Writer. Sample Coder jdk1.5 documents have this realization: String provider = "com.fred.providers.HighAvailabilityProvider" CachedRowSet crs = new CachedRowSetImpl (provider); so we set up a special Reader and Writer for the RowSet, but this is needed Third party's package support. The object we created with a non-arranging construction method is the use of the default syncprovider, of course, this is already enough for us. After creating an object, you can use the same method as the JDBcrowSet to perform the output of the result set, 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 provided by CachedRowSet is paging feature.
The problem of previous programmers is how to handle data pages without affecting performance. Now there is CachedRowset everything that makes it 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.next ()) while (crs.next ()) System.out.Println (Crs.Getint ("ID" "/ t / t" crs.getstring ("name")); We set the number of data lines per page before crs.execute (), then reader reads the data of the specified row when reader, so that we avoid reading all the data and then makes a paging operation. Is it Very simple? JoinRowSet interface: This interface can provide us directly to JoIn without connection. The following code provides JoinRowSet implementation: cachedrowset crs1 = new caehedrowsetiMPL (); crs1.seturl ("JDBC: mydql) : // LocalHost: 3306 / Test "); Crs1.SetuserName (" root "); crs1.setpassword (" ""); crs1.setcommand ("select * from table1"); crachedrowset CRS2 = New Caehedrowsetimpl (); Crs2.SETURL ("JDBC: MyDQL: // LocalHost: 3306 / Test"); crs2.setusername ("root"); crs2.setpassword (""); crs2.setcommand ("Select * from table2" ); crs2.execute (); joinortset jrs = new joinortsetimpl (); jrs.addrowset (CRS1, "ID"); jrs.addrowset (CRS2, "ID"); while (jrs.n EXT ()) System.Out.println (Jrs.Getint ("ID") "/ t / t" jrs.getstring ("name") "/ t / t" jrs.getstring ("info") The role of this code and execution select * from table1 inner Join table2 on table1.id = Table2.id statement gets the same. But I personally think that it is complicated with it, it is better to use this JOIN statement directly to get the cachedrowset. The default join is Inner Join, the interface also supports Cross Join, Full Join, Left Outer Join and Right Outer Join, we modify the connection type through the setJointYpe () method, of course, this still requires the support of the database. There is also a note that is worth noting. In this example, I am called the ID in the two tables, then we use the ID this name when we take the data, then if the names of the two columns are different? The system will take a default name for this connection called "mergecol". FilteredRowSet interface: .NET ADO.NET supports getting the result set uses a certain condition filtering to get different results, now JDK1.5 can also do it, and the FilterrowSet interface allows us to flexibly define the filter criteria to achieve different effects.
The Predicate interface in the javax.sql.rowset package is this filter, we define filter conditions by implementing this interface, below is a schematic code: public class filter imports predicate {private int min; private int max; public filter INT min, int max, string colname {this.min = min; this.max = max; this.colname = colname;} public boolean evaluate (rowset rs) {CachedRowSet CRS = (CachedRowSet) RS; IF ((CRS) GetinT (ColName)> Min) && (Crs.Getint (Colname) Conclusion: There are still some ways in the five ROWSET interfaces. Because of the limited space, I only list some of these typical methods, I hope this article will learn from the JDK1.5 for everyone!