Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

java - How to configure javadb in eclipse(part2)?

this is the continuation of my previous question How to configure javadb in eclipse?. I was able to made some changes on my code but now it is giving me this error -> ERROR: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect. Hope you guys can help me with this one.

Here is my code:

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

public class Test {

public static void main(String[] args) {
    try {
        Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    final String DB_URL = "jdbc:derby://localhost:1527/CoffeeDB;create=true";

    try{
        Connection conn = DriverManager.getConnection(DB_URL);
        Statement stmt = conn.createStatement();

        String sql = ("CREATE TABLE Coffee(Description CHAR(25),ProdNum CHAR(10) NOT NULL PRIMARY KEY,Price DOUBLE)");
        stmt.execute(sql);

        sql = "INSERT INTO Coffee VALUES('Bolivian Dark','14-001',8.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Bolivian Medium','14-002',8.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Brazilian Dark','15-001',7.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Brazilian Medium','15-002',7.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Brazilian Decaf','15-003',8.55)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Central American Dark','16-001',9.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Central American Medium','16-002',9.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Sumatra Dark','17-001',7.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Sumatra Decaf','17-002',8.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Sumatra Medium','17-003',7.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Sumatra Organic Dark','17-004',11.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Kona Medium','18-001',18.45)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Kona Dark','18-002',18.45)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('French Roast Dark','19-001',9.65)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Galapagos Medium','20-001',6.85)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Guatemalan Dark','21-001',9.95)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Guatemalan Decaf','21-002',10.45)";
        stmt.executeUpdate(sql);

        sql = "INSERT INTO Coffee VALUES('Guatemalan Medium','21-003',9.95)";
        stmt.executeUpdate(sql);

        String sqlStatement = "SELECT Description FROM Coffee";
        ResultSet result = stmt.executeQuery(sqlStatement);

        System.out.println("Coffees found in the Database");
        System.out.println("-----------------------------");

        while(result.next()){
            System.out.println(result.getString("Description"));
        }

        conn.close();
    }catch(Exception ex){
        System.out.println("ERROR: " + ex.getMessage());
    }

}

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had been trying with your current code and found that the exception happens when the server is not being started. The way to solve this is start the server in a simple way:

Go to the lib folder in the extracted folder of derby server download over command prompt. For me it was as follows:

C:>cd optdb-derby-10.11.1.1-liblib

C:optdb-derby-10.11.1.1-liblib>java -jar derbyrun.jar server start
Sat Oct 03 16:26:29 IST 2015 : Security manager installed using the Basic server security policy.
Sat Oct 03 16:26:29 IST 2015 : Apache Derby Network Server - 10.11.1.1 - (1616546) started and ready to accept connections on port 1527

There is a different way by which you can start the server in Eclipse. You would need to install plugin and then start the server. The following references should help you:

Derby Plugin How To
Getting Started with Derby Plugin for Eclipse


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...