19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

«interface»<br />

java.sql.ResultSet<br />

«interface»<br />

javax.sql.RowSet<br />

«interface»<br />

javax.sql.rowset.JdbcRowSet<br />

«interface»<br />

javax.sql.rowset.CachedRowSet<br />

<strong>com</strong>.sun.rowset.JdbcRowSetImpl<br />

Figure 41.3<br />

<strong>com</strong>.sun.rowset.CachedRowSetImpl<br />

The JdbcRowSetImpl and CachedRowSetImpl are concrete implementations of<br />

RowSet.<br />

The RowSet interface contains the <strong>Java</strong>Beans properties with get and set<br />

methods. You can use the set methods <strong>to</strong> set a new url, username,<br />

password, and <strong>com</strong>mand for an SQL statement. Using a RowSet, Listing 37.1<br />

can be simplified, as shown in Listing 41.4.<br />

Listing 41.4 SimpleRowSet.java<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

import java.sql.SQLException;<br />

import javax.sql.RowSet;<br />

import <strong>com</strong>.sun.rowset.*;<br />

public class SimpleRowSet {<br />

public static void main(String[] args)<br />

throws SQLException, ClassNotFoundException {<br />

// Load the JDBC driver<br />

Class.forName("<strong>com</strong>.mysql.jdbc.Driver");<br />

System.out.println("Driver loaded");<br />

// Create a row set<br />

RowSet rowSet = new JdbcRowSetImpl();<br />

// Set RowSet properties<br />

rowSet.setUrl("jdbc:mysql://localhost/javabook");<br />

rowSet.setUsername("scott");<br />

rowSet.setPassword("tiger");<br />

17

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!