1

I am trying to create a new SQL database with this Java program

import java.sql.*; //Needed for JDBC classes

public class BuildPhonebookDB {
  public static void main(String[] args) throws Exception{
    //Create a named constant for the URL
    final String DB_URL = "jdbc:derby:Phonebook;create=true";

    try {
        //Create a connection to the database.
        Connection conn = DriverManager.getConnection(DB_URL);
        //Create a Statement object.
        Statement stmt = conn.createStatement();
        //Create the Entries table
        stmt.execute("CREATE TABLE Entries (" +
                "Name CHAR(20)"+
                "Number INTEGER)"
                );
        System.out.println("Database Connected");
        //Close the connection
        conn.close();
    }
    catch(Exception ex) {
        System.out.println(ex.getMessage());
    }
  }
}

When I try to run the program I get an error that:

No suitable driver found for jdbc:derby:Phonebook;create=true

I have looked at various other similar posts on Stack Overflow, such as this one, but none help. I have seen things about a driver jar, but I don't know what this is, if I need to edit this, could someone help me through it?

Thanks for any help

Community
  • 1
  • 1
Wowsk
  • 3,637
  • 2
  • 18
  • 29
  • 1
    Did you remember to add the Derby (or JavaDB) JDBC driver .jar file to your classpath? – Andreas Dec 09 '15 at 00:00
  • I don't know what you mean by this. I am using my textbook as a guide and it doesn't say anything about a .jar file. – Wowsk Dec 09 '15 at 00:01
  • https://db.apache.org/derby/quick_start.html. I guess the writer of your textbook expects you to use a search engine ... – Marged Dec 09 '15 at 00:02

2 Answers2

3

Did you see this guide and have you complited all step of this guide?

Apache Derby

Download Derby Download the binary Apache Derby distribution from the Derby web site at http://db.apache.org/derby/derby_downloads.html. These tutorial instructions use version 10.12.1.1 and assume you downloaded one of the binary distribution files listed in the table below:

Operating System Download File Windows db-derby-10.12.1.1-bin.zip UNIX, Linux, and Mac db-derby-10.12.1.1-bin.tar.gz If a more recent release is available, download that, then substitute that version number for 10.12.1.1 in the following instructions.

Install Derby Choose the directory into which you want to install the Derby software. You must have write permissions to this directory. The sample instructions below use C:\Apache for Windows and /opt/Apache for UNIX; be sure to use your actual location. Copy the software distribution to the location you choose, then extract it as shown below.

Windows (use your extraction tool e.g. WinZip -- these instructions use mks unzip):

mkdir C:\Apache copy db-derby-10.12.1.1-bin.zip
> C:\Apache cd C:\Apache unzip db-derby-10.12.1.1-bin.zip

UNIX:

mkdir /opt/Apache cp db-derby-10.12.1.1-bin.tar.gz /opt/Apache
> cd /opt/Apache tar xzvf db-derby-10.12.1.1-bin.tar.gz

In both cases, the software will now be extracted into a subdirectory named db-derby-10.12.1.1-bin.

Set DERBY_INSTALL Set the DERBY_INSTALL variable to the location where you installed Derby. Examples are shown below, but be sure to use the actual location on your system:

Windows: C:\> set DERBY_INSTALL=C:\Apache\db-derby-10.12.1.1-bin

UNIX Korn Shell:

 $ export
> DERBY_INSTALL=/opt/Apache/db-derby-10.12.1.1-bin

Configure Embedded Derby To use Derby in its embedded mode set your CLASSPATH to include the jar files listed below:

derby.jar: contains the Derby engine and the Derby Embedded JDBC driver derbytools.jar: optional, provides the ij tool that is used by a couple of sections in this tutorial You can set your CLASSPATH explicitly with the command shown below:

Windows:

C:\> set
> CLASSPATH=%DERBY_INSTALL%\lib\derby.jar;%DERBY_INSTALL%\lib\derbytools.jar

;.

UNIX:

$ export
> CLASSPATH=$DERBY_INSTALL/lib/derby.jar:$DERBY_INSTALL/lib/derbytools.jar:.

... Step 3: Embedded Derby

When an application accesses a Derby database using the Embedded Derby JDBC driver, the Derby engine does not run in a separate process, and there are no separate database processes to start up and shut down. Instead, the Derby database engine runs inside the same Java Virtual Machine (JVM) as the application. So, Derby becomes part of the application just like any other jar file that the application uses. Figure 1 depicts this embedded architecture.

Set the environment

To set up the environment, follow the "Configure Embedded Derby" instructions.

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
2

Use this before you get the connection from the driver: Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();

Laszlo Hirdi
  • 1,140
  • 12
  • 18
  • I have already tried this, but unfortunately I get a different error: _org.apache.derby.jdbc.ClientDriver_ – Wowsk Dec 09 '15 at 00:34