Embedded server example

You can start the Network Server in another thread automatically when Derby starts by setting the derby.drda.startNetworkServer property, or you can start it by using a program.

See Setting Network Server properties for details on this property.

The following example shows how to start the Network Server by using a program:

import org.apache.derby.drda.NetworkServerControl;
import java.net.InetAddress;
NetworkServerControl server = new NetworkServerControl
	(InetAddress.getByName("localhost"),1527);
server.start(null);

The program that starts the Network Server can access the database by using either the embedded driver or the Network Client driver. The server framework's attempt to boot the local JDBC driver is ignored because it has already been booted within the application's JVM. The server framework simply accesses the instance of Derby that is already booted. There is no conflict between the application and the server framework.

The remote client can then connect through the Derby client driver:

String nsURL="jdbc:derby://localhost:1527/sample";  
java.util.Properties props = new java.util.Properties();
props.setProperty("user","usr");
props.setProperty("password","pwd");

/*
    If you are running on JDK 6 or higher, you do not
    need to invoke Class.forName(). In that environment, the
    ClientDriver loads automatically.
*/
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection conn = DriverManager.getConnection(nsURL, props);

/*interact with Derby*/
Statement s = conn.createStatement();

ResultSet rs = s.executeQuery(
"SELECT * FROM HotelBookings");
Related concepts
Connectivity configurations
Multiple-client features available in Derby
The Derby Network Server
Embedded servers
How to start an embedded server from an application