Lennart
Lennart

Reputation: 392

Java set up connection database

I want to connect to a database on my computer. I have already created a database, but i cannot connect. I always get a classnotfoundexception. I don't know how to fix it. My database is called begindb and I want to use org.apache.jdbc.ClientDriver as driver. This is my code from my program:

private final static String JDBC_URL="jdbc:derby://localhost/begindb";
private final static String JDBC_DRIVER="org.apache.derby.jdbc.ClientDriver";
private final static String USER_ID="test";
private final static String PASSW="test";
public static void main(String[] args) {
    try{
        Class.forName(JDBC_DRIVER);
        try(Connection conn = DriverManager.getConnection(JDBC_URL, USER_ID, PASSW)){
            System.out.println("good job!!");
        }
        catch(SQLException e){
            System.out.println("Error.");
        }
    }
    catch(ClassNotFoundException e)
        System.out.println(e.getMessage());
    }
}

So the first line in the try statement won't work. Because i am getting a classnotfoundexception.

Upvotes: 1

Views: 85

Answers (2)

user3684675
user3684675

Reputation: 381

You should have derbyclient.jar set in the classpath to recognize the driver class (org.apache.derby.jdbc.ClientDriver) you are using.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201429

From the documentation Step 4 -

To use the Derby Network Client JDBC driver, set your CLASSPATH to include the jar files listed below:

derbyclient.jar: contains the JDBC driver

derbytools.jar: optional, provides the ij tool

Add derbyclient.jar to your project classpath.

  1. Expand you Project.
  2. Right-click Libraries.
  3. Select Add Jar/Folder.
  4. Select "derbyclient.jar"

Upvotes: 2

Related Questions