DataSource access example

The following example uses org.apache.derby.jdbc.ClientDataSource to access the Network Server:

public static javax.sql.DataSource getDS(String database, String user, String password) throws SQLException { org.apache.derby.jdbc.ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource(); // DatabaseName can include URL Attributes ds.setDatabaseName(database); if (user != null) ds.setUser(user); if (password != null) ds.setPassword(password); // The host on which Network Server is running ds.setServerName("localhost"); // port on which Network Server is listening ds.setPortNumber(1527); return ds; }

The program then can connect: javax.sql.DataSource ds = getDS("mydb;create=true", null, null); // Note: user and password are required on connection Connection conn = ds.getConnection("usr2", "pass2");