Generally for beginners making a project find it difficult to understand how to start the Java DB network server or shut it down right from their java code.
There are two ways to do this as described in Derby documentation (http://db.apache.org/derby/docs/dev/adminguide/tadminconfig814963.html):
- You can use the NetworkServerControl API to start the Network Server from a separate thread within a Java application:.
- Using a derby.properties file with some special strings inside it.
For the beginners I suggest to go with the first method of using the NetworkServerControl API and I am going to explain the same for you. For second method you can look in documentation on the link given above.
NetworkServerControl class is included in the JavaDB(Apache Derby) package. It is located in lib\derbynet.jar\org\apache\derby\drda\NetworkServerControl.class
So derbynet.jar must be in your classpath or included in your project libraries. It contains four constructors that let you start derby server in a more flexible way. Here is a small guide in documentation of Apache derby on these four constructors (http://db.apache.org/derby/docs/10.7/adminguide/tadminconfig813694.html)
You can find the NetworkServerControl API information on http://download.oracle.com/javadb/10.4.2.1/javadoc/jdbc3/org/apache/derby/drda/NetworkServerControl.html
You can use any of the below to start the server on the local host machine and if you want to start the server on a different host then you can insert the InetAddress.
NetworkServerControl serverControl=new NetworkServerControl(); serverControl.start(new PrintWriter(System.out,true));
NetworkServerControl serverControl=new NetworkServerControl(InetAddress.getByName("localhost"),1527); serverControl.start(new PrintWriter(System.out,true));