java derby连接到端口1,527上的服务器localhost,消息连接被拒绝

时间:2017-02-04 12:34:23

标签: java database jdbc derby

我正在尝试将derby客户端数据库连接到我的java项目。起初一切都很好.3天后突然德比数据库无法连接。它显示此消息"错误连接到端口1,527上的服务器localhost,消息连接被拒绝:"

数据库连接代码:

public Connection getConnection() {
    try {

        String driver = "org.apache.derby.jdbc.ClientDriver";

        try {
            Class.forName(driver);
        } catch (java.lang.ClassNotFoundException e) {
            e.printStackTrace();
        }

        String dbURL2 = "jdbc:derby://localhost:1527/D:/Java Project/Alo Art Academy/AloArtDB;create=true";
        String user = "HabibDB";
        String password = "habib2017";
        Connection conn = DriverManager.getConnection(dbURL2, user, password);

        if (conn != null) {
            System.out.println("Connected to database #2");
        }
        return conn;

    } catch (SQLException ex) {
        ex.printStackTrace();
        return null;
    }
}

然后编辑C:\ Program Files \ Java \ jre1.8.0_77 \ lib \ security  java.policy文件并添加以下权限

    permission java.net.SocketPermission "localhost:1527", "listen";

但没有运气! :(

堆栈跟踪:

java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server localhost on port 1,527 with message Connection refused: connect.
    at org.apache.derby.client.am.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
    at org.apache.derby.jdbc.ClientDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at alo.art.academy.workstation.getConnection(workstation.java:46)
    at alo.art.academy.workstation.<init>(workstation.java:27)
    at alo.art.academy.AloArtAcademy$1.run(AloArtAcademy.java:26)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我有什么问题?

1 个答案:

答案 0 :(得分:0)

我认为您的网址存在问题,为什么要指定数据库的确切路径:

String dbURL2 = "jdbc:derby://localhost:1527/D:/Java Project/Alo Art Academy/AloArtDB;create=true";

连接URL语法如下:

jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*

因此请更改您的网址:

String dbURL2 = "jdbc:derby://localhost:1527/AloArtDB;create=true";

您可以了解更多herehere