Specifying attributes in a properties object

Instead of specifying attributes on the connection URL, you can specify attributes as properties in a Properties object that you pass as a second argument to the DriverManager.getConnection method.

For example, to set the user name and password:

Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

Properties p = new Properties();

p.setProperty("user", "sa");
p.setProperty("password", "manager");
p.setProperty("create", "true");

Connection conn = DriverManager.getConnection(
    "jdbc:derby:mynewDB", p);

If you are running on JDK 6 or higher, you do not normally need to invoke Class.forName(). In that environment, the EmbeddedDriver loads automatically. The only exception to this rule is when you need to shut down Derby in the middle of your application and then restart it. To restart Derby, create a new instance of the driver as follows:

Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Related tasks
Creating and accessing a database
Related reference
Using the databaseName attribute