Search code examples
javadatabasejdbcnetbeansderby

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.


Solution

  • 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"