This section describes how to create connections to Java DB (Derby) Network Server.
After I have my Java DB (Derby) started in Network Server mode, I am ready to try
to create a connect object to access an existing database on the server.
Here is my sample program showing you how to create a connection to access the TestDB:
/**
* DerbyConnection.java
* Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
*/
import java.sql.*;
public class DerbyConnection {
public static void main(String [] args) {
Connection con = null;
try {
// Connect with a url string
con = DriverManager.getConnection(
"jdbc:derby://localhost/TestDB");
System.out.println("Derby connection ok.");
con.close();
} catch (Exception e) {
System.err.println("Exception: "+e.getMessage());
}
}
}
The output confirms that the DriverManager class loaded the driver class and created a connection
to the Java DB (Derby) server correctly:
C:\>javac DerbyConnection.java
C:\>java -cp .;\local\javadb\lib\derbyclient.jar DerbyConnection
Derby connection ok.