Andy
Andy

Reputation: 251

Can't connect to Derby in Eclipse

I am trying to develop a web app with eclipse that uses a derby database and runs on tomcat.

My problem is that I cannot start the derby server with eclipse (it works fine out of CMD) and I cannot get my servlet to establish a connection with the database, each time I try I get the error:

java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.at org.apache.derby.client.am.SQLExceptionFactory40.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(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.Jieren.servlets.Authenticator.testCredentials(Authenticator.java:84)
at com.Jieren.servlets.Authenticator.doPost(Authenticator.java:36)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

I do not have any xml files that do anything with the connection (I have seen web.xml and such that manage connections) but from what I have seen a connection should be possible via straight java code (which seemed easier to learn with as I am fairly new).

The code that I use to connect with is as follows.

Connection conn = null;
    PreparedStatement prestat = null;
    ResultSet pw = null;

    try {
        Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    try {
        conn = DriverManager.getConnection("jdbc:derby://localhost:1527/C:/apache-tomcat-7.0.19/Databases/Jieren;" +
                "user=Access;" +
                "password=Entry");

        prestat = conn.prepareStatement("SELECT password FROM logs WHERE username = ?");
        prestat.setString(1, username);
        pw = prestat.executeQuery();
        if (password.equals(pw.toString())) answer = 1;
        pw.close();
        pw = null;
        prestat.close();
        prestat = null;
        conn.close();
        conn = null;

    } catch (SQLException e) {
        e.printStackTrace();
    }
    finally{
        if (pw != null){
            try { pw.close();} catch (SQLException e){;}
            pw = null;
        }
        if (prestat != null){
            try { prestat.close();} catch (SQLException e){;}
            prestat = null;
        }
        if (conn != null){
            try {conn.close();} catch(SQLException e) {;}
        conn = null;
        }
    }

From what I have figured out from looking around, the code should work if everything else is configured correctly. connecting to the database via ij outside eclipse works, so I have a feeling that there is a setting or something that I need to write in eclipse to connect this.

Upvotes: 2

Views: 10542

Answers (1)

Bryan Pendleton
Bryan Pendleton

Reputation: 16389

The exception is telling you that your network server is not running. When your connection URL starts jdbc:derby://hostname, then you are telling Derby you wish to run in client-server mode, meaning that your client application will establish a TCP/IP connection to the Network Server. See this doc for how to setup and operate the Network Server: http://db.apache.org/derby/docs/10.8/adminguide/

Upvotes: 3

Related Questions