Derby在java应用程序中自动启动服务器并连接到数据库 [英] Derby automatically start server within java application and connect to database

查看:247
本文介绍了Derby在java应用程序中自动启动服务器并连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试自动连接到系统上的数据库。
数据库位于通过NetBeans创建的默认Derby文件夹中。
我想做的是启动服务器并连接到现有的数据库。

Trying to connect to a database on the system automatically. The database is in the default Derby folder, created via NetBeans. What I want to do is start the server and connect to the already existing database.

public void startServer() throws Exception {


 NetworkServerControl server = new NetworkServerControl();
    server.start(prntWrt);
}

@Override
public void start(Stage primaryStage) throws IOException, Exception {
    startServer();
    Pane root = (Pane) FXMLLoader.load(InteractiveFictionGame2.class.getResource("MainMenu.fxml"));

    Scene scene = new Scene(root);
    primaryStage.setTitle("MainMenu");
    primaryStage.setScene(scene);
    primaryStage.setFullScreen(true);
    primaryStage.show();
}

似乎服务器启动,但由于某种原因,到数据库,因为它认为它不存在。

It seems that the server does start but for some reason I can't connect to the database as it thinks it is non existant.

String host = "jdbc:derby://localhost:1527/InteractiveGameDatabase";
String unm = "Kylar";
String pswrd = "aswzxc";

public void loadImg()throws IOException {

public void loadImg() throws IOException {

    try {
        String SQL = "select vista from location where ycoordinate = ? and xcoordinate = ?";
        Stage stage = new Stage();

        con = DriverManager.getConnection(host, unm, pswrd);
        stmnt = con.prepareStatement(SQL);
        stmnt.setInt(1, ycoord);
        stmnt.setInt(2, xcoord);
        rs = stmnt.executeQuery();
        rs.next();
        fis = rs.getBinaryStream(1);
        BufferedImage imgt = null;
        try {
            imgt = javax.imageio.ImageIO.read(fis);
        } catch (IOException ex) {
            System.out.println("Image failed to load.");
        }
        Image newImg = SwingFXUtils.toFXImage(imgt, null);

        fadeInImage();
        img_1.setFitHeight(880);
        img_1.setImage(newImg);

        img_1.setPreserveRatio(true);
        img_1.setCache(true);

        CountDownLatch doneLatch = new CountDownLatch(1);
        animateUsingTimeline();


        stck1.getChildren().addAll();
        Scene scene = new Scene(stck1);
        stage.setTitle("Interactive Fiction Game");
        stage.setScene(scene);
        stage.setFullScreen(true);

        stage.show();

        rs.close();
        stmnt.close();
        con.close();

    } catch (SQLException e) {
        System.out.println(e.getMessage());

    }
}

被拒绝,因为未找到数据库InteractiveGameDatabase。如果我通过NetBeans IDE启动服务器,然后运行应用程序,一切都是完美的。

I get an error "The connection was refused because the database InteractiveGameDatabase was not found.". If I start the server through the NetBeans IDE and then run the application everything is perfect. Any help will be appreciated.

推荐答案

由于您指定:

String host = "jdbc:derby://localhost:1527/InteractiveGameDatabase";

作为连接URL,Derby网络服务器正在使用相对数据库名称InteractiveGameDatabase 。由于这是一个相对名称,而不是绝对名称,Derby网络服务器将在其主目录中查找数据库,这通常是当您启动Derby网络服务器时当前目录。

as your connection URL, the Derby Network Server is looking for the database using the relative database name "InteractiveGameDatabase". Since that is a relative name, not an absolute name, the Derby Network Server will look for the database in its home directory, which is typically whatever was the current directory when you started the Derby Network Server.

因此,可能这里发生的是,当您在NetBeans中运行Derby网络服务器时,根据NetBeans如何启动它,它以某个目录作为其主目录运行。

So probably what's going here is that when you run the Derby Network Server in NetBeans, it runs with a certain directory as its home directory, according to how NetBeans starts it up.

但是当你自己运行Derby网络服务器时,它运行在不同的目录作为它的主目录,因为你没有在NetBeans启动它的同一目录中精确启动它,因此在此新目录中找不到数据库InteractiveGameDatabase。

But when you run the Derby Network Server yourself, by hand, it runs in a different directory as its home directory, because you didn't precisely start it up in the same directory where NetBeans starts it up, and hence it can't find the database InteractiveGameDatabase in this new directory.

您可以:


  1. 始终使用NetBeans启动的Derby网络服务器

  2. 启动您自己的网络服务器,但安排在NetBeans启动Derby网络服务器的同一目录中

  3. 启动自己的网络服务器,但更改连接URL以指定运行NetBeans启动的Derby网络服务器的位置的完整绝对路径,以便网络服务器可以访问该目录你打开数据库。

有很多其他的可能性,但希望这些足以让你知道发生了什么。

There are many other possibilities, but hopefully these are enough to give you an idea about what's going on.

这篇关于Derby在java应用程序中自动启动服务器并连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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