java derby connecting to server localhost on port 1,527 with message Connection refused

I am trying to connect derby client database to my java project. At first everything was fine.After 3 days later Suddenly the derby db could not be connected.It's showing this message "Error connecting to server localhost on port 1,527 with message Connection refused:"

Database connection code:

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;
    }
}

Then edited C:\Program Files\Java\jre1.8.0_77\lib\security the java.policy file and add the following permission

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

but no luck! :(

stacktrace:

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)

What is wrong with me?

Upvotes: 2

Views: 4724

Answers (1)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59988

I think you have a problem in your URL why you specify the exact path of your Database :

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

The connection URL syntax is as follows:

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

So change your URL like this :

String dbURL2 = "jdbc:derby://localhost:1527/AloArtDB;create=true";
  • Make sure that the post is open
  • Make sure to download and put your jdbc derby driver in class path.

You can learn more here and here

Upvotes: 1

Related Questions