2

I've tried a simple code about database but it gave me this error -> ERROR: No suitable driver found for jdbc:derby:CoffeeDB;create=true

package Practice;
import java.sql.*;

public class Practice {
public static void main(String[]args)throws Exception{
    final String DB_URL = "jdbc:derby:CoffeeDB;create=true";
    try{
        Connection conn = DriverManager.getConnection(DB_URL);
        Statement stmt = conn.createStatement();

        String sql = ("CREATE TABLE Coffee(Description CHAR(25),Prod Num CHAR(10) NOT NULL PRIMARY KEY,Price DOUBLE)");
        stmt.execute(sql);

        sql = "INSERT INTO Coffee VALUES('Bolivian Dark','14-001',8.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Bolivian Medium','14-002',8.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Brazilian Dark','15-001',7.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Brazilian Medium','15-002',7.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Brazilian Decaf','15-003',8.55)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Central American Dark','16-001',9.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Central American Medium','16-002',9.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Sumatra Dark','17-001',7.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Sumatra Decaf','17-002',8.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Sumatra Medium','17-003',7.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Sumatra Organic Dark','17-004',11.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Kona Medium','18-001',18.45)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Kona Dark','18-002',18.45)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('French Roast Dark','19-001',9.65)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Galapagos Medium','20-001',6.85)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Guatemalan Dark','21-001',9.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Guatemalan Decaf','21-002',10.45)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Guatemalan Medium','21-003',9.95)";
        stmt.executeUpdate(sql);

        String sqlStatement = "SELECT Description FROM Coffee";
        ResultSet result = stmt.executeQuery(sqlStatement);

        System.out.println("Coffees found in the Database");
        System.out.println("-----------------------------");

        while(result.next()){
            System.out.println(result.getString("Description"));
        }

        conn.close();
    }catch(Exception ex){
        System.out.println("ERROR: " + ex.getMessage());
    }
}
}

Can someone give me a step by step process on how to configure this? I've tried searching for answers but it is not thorough.

Pearl
  • 123
  • 1
  • 7

2 Answers2

0
  1. Download the driver Derby driver, if I remeber correcly it is the derby.jar (add it to class path)

  2. Before connecting register the driver

    Driver driver =  (Driver) Class.forName("org.apache.derby.jdbc.EmbeddedDriver")
    .newInstance();
    
    DriverManager.registerDriver(driver);
    

You need to do try catch or have your metodo throw the Class.forName exception

For more info check also this SQLException: No suitable driver found for jdbc:derby://localhost:1527

Community
  • 1
  • 1
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • 1
    Since your URL is jdbc:derby:CoffeeDB, you're set up with the Derby Embedded configuration, so you don't need derbyclient.jar, just derby.jar. Furthermore, you don't want to try to load the ClientDriver, you want to load the EmbeddedDriver. Use the EmbeddedDriver for Embedded configuration Connection URL strings; use the ClientDriver for Client-Server configuration Connection URL strings (jdbc:derby://host-name:port/...) – Bryan Pendleton Oct 03 '15 at 01:24
  • Thanks, edited my answer and upvoted your comment (long time a go that I used derby) – Petter Friberg Oct 03 '15 at 13:19
0

As mentioned by Petter Friberg, you are missing the diver and the driver initialization statement. Moreover, you need to add Derby JAR to your classpath.

  1. Go to Apache Derby downloads and download the library. I tried with db-derby-10.11.1.1-lib.zip.
  2. Extract the archive and copy lib/derby.jar and lib/derbyclient.jar to your Java Build Path.
  3. There is a syntax error at statement "CREATE TABLE Coffee(Description CHAR(25),Prod Num CHAR(10) NOT NULL PRIMARY KEY,Price DOUBLE)". You would need to remove the space character in the column name of Prod Num. You may use Prod_Num or ProdNum or \"Prod Num\".
Community
  • 1
  • 1
James Jithin
  • 10,183
  • 5
  • 36
  • 51
  • Since your URL is jdbc:derby:CoffeeDB, you're set up with the Derby Embedded configuration, so you don't need derbyclient.jar, just derby.jar. – Bryan Pendleton Oct 03 '15 at 01:23
  • i thought the derby.jar and derbyclient.jar is already included in the jdk? – Pearl Oct 03 '15 at 03:07
  • @James Jithin - i am getting this error -> ERROR: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect. – Pearl Oct 03 '15 at 03:42
  • @James Jithin - Windows – Pearl Oct 03 '15 at 10:10
  • @James Jithin - i have made some changes on my code..check out this post http://stackoverflow.com/questions/32919136/how-to-configure-javadb-in-eclipsepart2 .. – Pearl Oct 03 '15 at 10:18
  • @Pearl, let us continue discussion there. – James Jithin Oct 03 '15 at 10:26