Component | Jar files |
Embedded database engine and JDBC driver | derby.jar |
Network client JDBC driver | derbyclient.jar |
Network server | derbynet.jar, derbyrun.jar |
Command line tools | derbytools.jar |
Localization messages | derbyLocale_xx_YY.jar |
Type of driver | JDBC Driver Class Name |
Embedded driver | org.apache.derby.jdbc.EmbeddedDriver |
Network client driver | org.apache.derby.jdbc.ClientDriver |
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");Or:DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());However, since Java 6.0 or later, loading JDBC driver as such becomes optional. The driver manager can load appropriate driver based on the database connection URL.
jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*jdbc:derby:codejava/webdb;create=true
jdbc:derby:E:/projects/codejava/webdb;create=true
jdbc:derby:memory:codejava/webdb;create=true
jdbc:derby:classpath:webdb
Where the absolute directory E:/projects/codejava is added to the classpath.
jdbc:derby:jar:webdb
jdbc:derby:jar:(E:/projects/db.jar)webdb
jdbc:derby:;shutdown=true
jdbc:derby://server[:port]/databaseName[;attribute=value]*The default port is 1527 if omitted. For example, to connect the user tom with password secret to the database webdb on the server dbserver, use the following URL:jdbc:derby://dbserver/webdb;user=tom;password=secret
String dbURL = "jdbc:derby:codejava/webdb;create=true"; Connection conn = DriverManager.getConnection(dbURL);
String dbURL = "jdbc:derby://localhost/webdb;create=true"; String user = "tom"; String password = "secret"; Connection conn = DriverManager.getConnection(dbURL, user, password);
String dbURL = "jdbc:derby://localhost/webdb";
Properties properties = new Properties();
properties.put("create", "true");
properties.put("user", "tom");
properties.put("password", "secret");
Connection conn = DriverManager.getConnection(dbURL, properties); And following is a full example program:package net.codejava.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* This program demonstrates how to connect to Apache Derby (Java DB) database
* for the embedded driver and network client driver.
* @author www.codejava.net
*
*/
public class JdbcDerbyConnection {
public static void main(String[] args) {
try {
// connect method #1 - embedded driver
String dbURL1 = "jdbc:derby:codejava/webdb1;create=true";
Connection conn1 = DriverManager.getConnection(dbURL1);
if (conn1 != null) {
System.out.println("Connected to database #1");
}
// connect method #2 - network client driver
String dbURL2 = "jdbc:derby://localhost/webdb2;create=true";
String user = "tom";
String password = "secret";
Connection conn2 = DriverManager.getConnection(dbURL2, user, password);
if (conn2 != null) {
System.out.println("Connected to database #2");
}
// connect method #3 - network client driver
String dbURL3 = "jdbc:derby://localhost/webdb3";
Properties properties = new Properties();
properties.put("create", "true");
properties.put("user", "tom");
properties.put("password", "secret");
Connection conn3 = DriverManager.getConnection(dbURL3, properties);
if (conn3 != null) {
System.out.println("Connected to database #3");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}That's Java code example to connect to Apache Derby database.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.