问题
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.
- Go to Apache Derby downloads and download the library. I tried with db-derby-10.11.1.1-lib.zip.
- Extract the archive and copy
lib/derby.jarandlib/derbyclient.jarto your Java Build Path. - 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 ofProd Num. You may useProd_NumorProdNumor\"Prod Num\".
回答2:
Download the driver Derby driver, if I remeber correcly it is the derby.jar (add it to class path)
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