Search code examples
javasqldatabasederby

java derby Error connecting to server localhost 1527


I want to connect derby DB to Java application. I have downloaded all the jars, set a classpath as a java directory and downloaded Derby.What I get now is:

java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.

Can anyone help me:what does it actually mean, and what can I do now?I guess there might be a problem with Database_url variable, but I don't know how to set it propely.

private static final String protocol = "jdbc:derby:";

public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
    final String DATABASE_URL = "jdbc:derby://localhost:1527/myDB;create=true;user=user;password=pass";
    try {
        Class.forName("org.apache.derby.jdbc.ClientDriver");
        System.out.println("Loaded the client driver.");
    } catch (Exception err) {
        System.err.println("Unable to load the client driver.");
        err.printStackTrace(System.err);
        System.exit(0);
    }
    String dbName = "BookDatabase";
    Connection conn = null;
    try {
        System.out.println(
                "Connecting to and creating the database...");
        conn = DriverManager.getConnection(DATABASE_URL);
        System.out.println("Database created.");
        Statement s = (Statement) conn.createStatement();
        s.execute("CREATE TABLE names" +
                "(author varchar(50), author_id " +
                "int, url varchar(80))");
        System.out.println("Created 'names' table.");
        System.out.println("Inserting authors.");
        s.execute("INSERT INTO names " +
                "VALUES ('Adams, Douglas', 1," +
                "'http://www.douglasadams.com')");
        s.execute("INSERT INTO names " +
                "VALUES ('Simmons, Dan', 2, 'http://www.dansimmons.com')");
        s.execute("INSERT INTO names " +
                "VALUES ('Stephenson, Neal', 3, " +
                "'http://www.nealstephenson.com')");
        System.out.println("Authors inserted.");
        conn.close();
    } catch (SQLException err) {
        System.err.println("SQL error.");
        err.printStackTrace(System.err);
        System.exit(0);
    }

}

Solution

  • Given your error (Connection refused: connect) it seems that the database server has not been started. Apache Derby has two operation modes: client/server and embedded. Each mode uses different drivers (and have different JDBC URL syntax).

    The ClientDriver is used for client/server mode (which works as a traditional RDMS), so you need to start the Derby DB Server as a different Java process:

    java -cp derby.jar;derbynet.jar;derbyclient.jar;derbytools.jar org.apache.derby.drda.NetworkServerControl start 
    

    (and java ... NetworkServerControl stop in order to shutdown the database process).

    Another alternative is starting the DB in embedded mode (the database and the client both run in the same JVM, but no other process can connect to the database at the same time).

    The embedded mode uses org.apache.derby.jdbc.EmbeddedDriver and the datasource URL has the form jdbc:derby:firstdb;create=true. See this example, which is explained in Activity 3: Run a JDBC program using the embedded driver of the Getting Started with Derby guide. Note that in embedded mode, some additional steps must be executed in order to properly shutdown the database before exiting the JVM.