How to configure javadb in eclipse?

怎甘沉沦 提交于 2019-12-10 22:43:45

问题


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.


回答1:


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\".



回答2:


  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



来源:https://stackoverflow.com/questions/32909305/how-to-configure-javadb-in-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!