如何在eclipse(part2)中配置javadb? [英] How to configure javadb in eclipse(part2)?

查看:236
本文介绍了如何在eclipse(part2)中配置javadb?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我上一个问题的延续如何在eclipse中配置javadb?。我能够对我的代码进行一些更改,但现在它给我这个错误 - > 错误:java.net.ConnectException:错误连接到服务器本地主机在端口1527与消息连接拒绝:连接

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.

这是我的代码:

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

}

}


推荐答案

我一直在尝试与您当前的代码,发现异常发生时,服务器不启动。解决方法是以一种简单的方式启动服务器:

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:

通过命令提示符访问derby服务器下载的解压缩文件夹中的lib文件夹。对我来说如下:

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

C:\>cd opt\db-derby-10.11.1.1-lib\lib

C:\opt\db-derby-10.11.1.1-lib\lib>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

有一种不同的方法可以在Eclipse中启动服务器。您需要安装插件,然后启动服务器。以下参考资料应该可以帮助您:

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插件如何
0047
Derby Plugin for Eclipse入门

这篇关于如何在eclipse(part2)中配置javadb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆