我尝试了一个关于数据库的简单代码,但它给了我这个错误 - >错误:没有为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());
}
}
}
有人可以一步一步地告诉我如何配置这个吗?我试过寻找答案,但并不彻底。
答案 0 :(得分:0)
下载驱动程序Derby driver,如果我记得它是derby.jar(将其添加到类路径)
连接前注册驱动程序
Driver driver = (Driver) Class.forName("org.apache.derby.jdbc.EmbeddedDriver")
.newInstance();
DriverManager.registerDriver(driver);
你需要尝试catch或让你的metodo抛出Class.forName异常
有关详情,请查看此SQLException: No suitable driver found for jdbc:derby://localhost:1527
答案 1 :(得分:0)
如Petter Friberg所述,您缺少潜水员和驱动程序初始化语句。此外,您需要将Derby JAR添加到类路径中。
lib/derby.jar
和lib/derbyclient.jar
复制到Java Build Path。"CREATE TABLE Coffee(Description CHAR(25),Prod Num CHAR(10) NOT NULL PRIMARY KEY,Price DOUBLE)"
处存在语法错误。您需要删除Prod Num
列名称中的空格字符。您可以使用Prod_Num
或ProdNum
或\"Prod Num\"
。