Search code examples
javajdbcderby

Setting up a Java Database


I'm trying to set up a Java DB Database for a programming class. I'm following the steps in the Deitel and Deitel textbook:

I have JDK11.8.0_112 installed. I'm running Windows 10.

My install is at C:\Program Files\Java\jdk1.8.0_112 I have the JAVA_HOME var set as C:\Program Files\Java\jdk1.8.0_112

The textbook says to go the install location db\bin and edit the setEmbeddedCP.bat file from @rem set DERBY_INSTALL= to @SET DERBY_INSTALL=%JAVA_HOME%\db

Then open a command prompt and go to the directory of that file and run it, which I've done, this is the result:enter image description here

The book has some source code to create the tables. I copied and pasted into my own project, C:\Users\hulbe\OneDrive\Documents\NetBeansProjects\DisplayAuthors\build\classes\displayauthors.

The book then states to use the command line and change directories to the folder with chapters examples, which are now in my own project.

Next, type "%JAVA_HOME%\db\bin\ij" into the command line.

Then, connect 'jdbc:derby:newbooks;create=true;user=xxx;password=xxx'; I've got the DB and the tables to create (I think). But when I run the code I get "java.sql.SQLException: No suitable driver found for jdbc:derby:newbooks"

I'm really at my wits end here. I've spent two days now trying to set up clean installs and walking through exactly like how the books is set up.

Here's the code: `try ( Connection connection = DriverManager.getConnection( DATABASE_URL, "deitel", "deitel");

        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(SELECT_QUERY)) {
        
        ResultSetMetaData metaData = resultSet.getMetaData();
        int numberOfColumns = metaData.getColumnCount();
        
        System.out.printf("Authors Table of Books Database: %n%n");
        
        for (int i = 1; i  <= numberOfColumns; i++) {
            System.out.printf("%-8s\t", resultSet.getObject(i));
        }
    }
    catch (SQLException sqlException) {
        sqlException .printStackTrace();
    }`

I know this is long and I'm sure it falls under already been asked, but nothing I've seen is helping me figure this out.

Anybody know what I'm doing wrong?

EDIT: The problem was that I had to extract the derby.jar files and add them manually, thank you Marco Tizzano.

Now, however, I am getting this error: java.sql.SQLException: Database 'books' not found. at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source) at org.apache.derby.impl.jdbc.Util.generateCsSQLException(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.(Unknown Source) at org.apache.derby.jdbc.InternalDriver$1.run(Unknown Source) at org.apache.derby.jdbc.InternalDriver$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at org.apache.derby.jdbc.InternalDriver.getNewEmbedConnection(Unknown Source) at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source) at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source) at org.apache.derby.jdbc.AutoloadedDriver.connect(Unknown Source) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at displayauthors.DisplayAuthors.main(DisplayAuthors.java:24) Caused by: ERROR XJ004: Database 'books' not found.

EDIT 2: I think I was creating the DB in the wrong directory, I reran it in the correct directory, ProjectName->src->proejctname. The command prompt displayed all the right SQL stuff, table creation and column names and such and then ran the inserts. This also created a folder in that directory with the db name, but still the DB not found error.


Solution

  • If you are running your code in Netbeans, the issue is that the JVM cannot find the drivers for Apache Derby, which normally (from JDBC 4.0+) are automatically loaded from the classpath.
    Bear in mind that in order to successfully get the database connection, first of all the DB vendor driver files (in this case derby.jar, derbytools.jar and derbyoptionaltools.jar) have to be present in the CLASSPATH, and in fact that was exactly what the book guided you to do when creating the database.
    So, in your case, you have to add those libraries in the project dependencies.
    I have never used Netbeans, but this answer will help you: How do I set the classpath in NetBeans?

    I am reporting here the steps to do:

    1. Right-click your Project.
    2. Select Properties.
    3. On the left-hand side click Libraries.
    4. Under Compile tab - click Add Jar/Folder button.

    Or

    1. Expand your Project.
    2. Right-click Libraries.
    3. Select Add Jar/Folder.