Datasheet

The showZipCodes method actually opens the connection and performs the query. The driver used is
org.apache.derby.jdbc.EmbeddedDriver. Derby also includes a ClientDriver for connecting to
Derby in network mode, where Derby runs a network server providing for a client/server approach to
using Derby:
public void showZipCodes()
{
try {
String driver = “org.apache.derby.jdbc.EmbeddedDriver”;
Class.forName(driver).newInstance();
Connection conn = null;
conn = DriverManager.getConnection(“jdbc:derby:DerbyTestDB”);
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery(“SELECT city, state, zipcode
FROM zipcodes”);
while(rs.next()) {
System.out.println(“City : “ + rs.getString(1));
System.out.println(“State : “ + rs.getString(2));
System.out.println(“Zipcode: “ + rs.getString(3));
System.out.println();
}
rs.close();
s.close();
conn.close();
} catch(Exception e) {
System.out.println(“Exception: “ + e);
e.printStackTrace();
}
}
}
Here’s the output from the previous code:
c:\>java DerbyTestDBClient
City : Fairfax
State : VA
Zipcode: 22030
City : Annandale
State : VA
Zipcode: 22003
City : Beverly Hills
State : CA
Zipcode: 90210
Derby is a thriving project and continues to implement more features. You can keep an eye on its devel-
opment at
http://db.apache.org/derby.
6
Part I: Thinking Like a Java Developer
05_777106 ch01.qxp 11/28/06 10:43 PM Page 6