0

I tried to test Derby sample source code. Unfortunately it failed: Cannot connect Derby database: connection refused

I was told that I haven't started a server. Official tutorial:

  1. Doesn't start any server.I have no feedback after C:\Apache\db-derby-10.4.1.3-bin\lib> java -jar derbyrun.jar server start just empty line shows and the derbyrun.jar ends.

    1. Doesn't show how to create server on the specified port

My question is: How to start a server on the specified port so the posted code works:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSetMetaData;


public class Restaurants
{
    private static String dbURL = "jdbc:derby://localhost:1526/myDB;create=true;user=me;password=mine";
    private static String tableName = "restaurants";
    // jdbc Connection
    private static Connection conn = null;
    private static Statement stmt = null;

    public static void main(String[] args)
    {
        createConnection();
        insertRestaurants(5, "LaVals", "Berkeley");
        selectRestaurants();
        shutdown();
    }

    private static void createConnection()
    {
        try
        {
            Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
            //Get a connection
            conn = DriverManager.getConnection(dbURL); 
        }
        catch (Exception except)
        {
            except.printStackTrace();
        }
    }

    private static void insertRestaurants(int id, String restName, String cityName)
    {
        try
        {
            stmt = conn.createStatement();
            stmt.execute("insert into " + tableName + " values (" +
                    id + ",'" + restName + "','" + cityName +"')");
            stmt.close();
        }
        catch (SQLException sqlExcept)
        {
            sqlExcept.printStackTrace();
        }
    }

    private static void selectRestaurants()
    {
        try
        {
            stmt = conn.createStatement();
            ResultSet results = stmt.executeQuery("select * from " + tableName);
            ResultSetMetaData rsmd = results.getMetaData();
            int numberCols = rsmd.getColumnCount();
            for (int i=1; i<=numberCols; i++)
            {
                //print Column Names
                System.out.print(rsmd.getColumnLabel(i)+"\t\t");  
            }

            System.out.println("\n-------------------------------------------------");

            while(results.next())
            {
                int id = results.getInt(1);
                String restName = results.getString(2);
                String cityName = results.getString(3);
                System.out.println(id + "\t\t" + restName + "\t\t" + cityName);
            }
            results.close();
            stmt.close();
        }
        catch (SQLException sqlExcept)
        {
            sqlExcept.printStackTrace();
        }
    }

    private static void shutdown()
    {
        try
        {
            if (stmt != null)
            {
                stmt.close();
            }
            if (conn != null)
            {
                DriverManager.getConnection(dbURL + ";shutdown=true");
                conn.close();
            }           
        }
        catch (SQLException sqlExcept)
        {

        }

    }
}
Community
  • 1
  • 1
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • Is the server actually running after you type C:\Apache\db-derby-10.4.1.3-bin\lib> java -jar derbyrun.jar server start ? Can you find the process? – ozborn Jan 04 '16 at 21:26
  • @ozborn No it doesn't run. The program runs for very short while. – Yoda Jan 04 '16 at 21:29
  • Is there an error log somewhere? You should see something like: Security manager installed using the Basic server security policy. Apache Derby Network Server - 10.4.1.3 - (648739) started and ready to accept connections on port 1527 at 2008-04-28 17:13:13.921 GMT – ozborn Jan 04 '16 at 22:10
  • @ozborn There is only single empty line then the command prompt shows again in the next line. – Yoda Jan 04 '16 at 22:16
  • Can you find derby.log (the error log) ? – ozborn Jan 04 '16 at 22:21
  • Not sure if you pasted your literal code or not, but you're asking to connect to the server running on 'localhost:1526', but the default port if you don't specify -p when starting the server is 1527. – Bryan Pendleton Jan 04 '16 at 23:13
  • @BryanPendleton Same thing was for `1527`. Thanks. – Yoda Jan 05 '16 at 09:24

1 Answers1

0

Setting port numbers

By default, Derby using the Network Server listens on TCP/IP port number 1527. If you want to use a different port number, you can specify it on the command line when starting the Network Server. For example:

java org.apache.derby.drda.NetworkServerControl start -p 1088

However, it is better to specify the port numbers by using any of the following methods

1. Change the startNetworkServer.bat or startNetworkServer.ksh scripts
2. Use the derby.drda.portNumber property in derby.properties

Please refer to:

https://db.apache.org/derby/docs/10.5/adminguide/tadminappssettingportnumbers.html

Amit Kaneria
  • 5,466
  • 2
  • 35
  • 38