JDK 1.5 learning Rowset (II.)

Source: Internet
Author: User
Tags filter define interface join min net

Continue .....



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");

The effect of this code is the same as the result set from the execution of the SELECT * FROM table1 inner JOIN table2 on Table1.id=table2.id statement. 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 "Mergedcol".





Filteredrowset Interface:

. NET Ado.net support to get the result set using certain conditions to filter to get different results, and 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, and we define the filter conditions by implementing this interface, and here's 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=max; 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 the data with ID value not between 1 and 20;

Because the method of implementing the Prdicate interface is flexible, we can set the filter condition very flexibly, and we can get different results from only one statement.





Webrowset Interface:

Because its platform independence is becoming more and more popular with developers, XML is also a good choice for data persistence, Webrowset encapsulates a method of reading and writing XML, and we can easily persist database data to XML or write 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: Includes all attributes of the setxxx () method, which is not set to the default property

Metadata: Including related metadata of database tables, corresponding to the information in ResultSetMetaData

Data: The entire number of result sets

The way to read data from an XML file to rowset is to 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.