Hello, I'm using NetBeans 6.7.1, and Derby 10.5.3.0. I'm trying to learn how to write Database applications, and I'm having trouble connecting to my Database. It loads the Derby Driver (EmbeddedDriver), but won't connect to the the Database named test. I looked at tests Properties in NetBeans, and it said that it was using the org.apache.jdbc.derby.ClientDriver. I did create the Database, and give it no username or password. Below is the code that I have. If anybody could help, that would be great =D

package jdb.test;
import java.sql.*;

public class DatabaseTest {

    public static void main(String[] args){
        try{
            // Load the EmbeddedDriver class
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
            System.out.println("Loaded Derby Driver");
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("Error loading Derby Driver.  Shutting down.");
            System.exit(-1);
        }

        // The database located on my computer
        String database = "jdbc:derby://localhost:1527/test";
        try{
            // Connect without a username and password
            Connection conn = DriverManager.getConnection(database);
            System.out.println("Connected to "+database);
        }catch(SQLException e){
            e.printStackTrace();
            System.out.println("Could not connect to "+database);
            System.exit(-1);
        }
        System.out.println("You have connect to the selected database.");
    }
}

This is the error that I get:

java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/test
        at java.sql.DriverManager.getConnection(DriverManager.java:602)
        at java.sql.DriverManager.getConnection(DriverManager.java:207)
        at jdb.test.DatabaseTest.main(DatabaseTest.java:22)

Your code is loading the EmbeddedDriver class, but using the ClientDriver network url for your connection string. The EmbeddedDriver uses the database name instead of the //servername url.

I tried using "jdbc:derby:test" instead of ""jdbc:derby://localhost:1527/test", but it gave me this error:

(this is the entire output from the execution)

Loaded Derby Driver
java.sql.SQLException: Database 'test' not found.
        at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.EmbedConnection.newSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.EmbedConnection.handleDBNotFound(Unknown Source)
        at org.apache.derby.impl.jdbc.EmbedConnection.<init>(Unknown Source)
        at org.apache.derby.impl.jdbc.EmbedConnection30.<init>(Unknown Source)
        at org.apache.derby.impl.jdbc.EmbedConnection40.<init>(Unknown Source)
        at org.apache.derby.jdbc.Driver40.getNewEmbedConnection(Unknown Source)
        at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source)
        at org.apache.derby.jdbc.AutoloadedDriver.connect(Unknown Source)
Could not connect to jdbc:derby:test
        at java.sql.DriverManager.getConnection(DriverManager.java:582)
        at java.sql.DriverManager.getConnection(DriverManager.java:207)
        at jdb.test.DatabaseTest.main(DatabaseTest.java:22)
Caused by: java.sql.SQLException: Database 'test' not found.
        at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
        ... 15 more

I tried setting the derby.system.home property to the location where I was storing the databases, but that didn't help either. It looks like I'm structuring everything correctly... I'm sorry, I think I'm missing something really obvious >.<

AHA! I used the create property to create a new Database, and then searched for it using the OS's Search Program. I found the newly created database in the NetBeans project folder. I can not successfully connect to the database. I don't know how to add that new Database to the list of databases that are editable directly through NetBeans.

This article has been dead for over six months. Start a new discussion instead.