这是我上一个问题How to configure javadb in eclipse?的延续。我能够对我的代码进行一些更改,但现在它给了我这个错误 - > 错误:java.net.ConnectException:在端口1527上连接到服务器localhost时出错,并显示消息Connection refused:connect 。希望你们能帮助我解决这个问题。
这是我的代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Test {
public static void main(String[] args) {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
final String DB_URL = "jdbc:derby://localhost:1527/CoffeeDB;create=true";
try{
Connection conn = DriverManager.getConnection(DB_URL);
Statement stmt = conn.createStatement();
String sql = ("CREATE TABLE Coffee(Description CHAR(25),ProdNum 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 :(得分:1)
我一直在尝试使用您当前的代码,发现在服务器未启动时会发生异常。解决这个问题的方法是以一种简单的方式启动服务器:
转到解压缩的derby server download over命令提示符文件夹中的lib文件夹。对我来说如下:
C:\>cd opt\db-derby-10.11.1.1-lib\lib
C:\opt\db-derby-10.11.1.1-lib\lib>java -jar derbyrun.jar server start
Sat Oct 03 16:26:29 IST 2015 : Security manager installed using the Basic server security policy.
Sat Oct 03 16:26:29 IST 2015 : Apache Derby Network Server - 10.11.1.1 - (1616546) started and ready to accept connections on port 1527
您可以通过不同的方式在Eclipse中启动服务器。您需要安装插件然后启动服务器。以下参考资料可以帮助您:
Derby Plugin How To
Getting Started with Derby Plugin for Eclipse