【问题标题】:Derby automatically start server within java application and connect to databaseDerby 在 Java 应用程序中自动启动服务器并连接到数据库
【发布时间】:2015-05-02 19:18:27
【问题描述】:

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

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

似乎服务器确实启动了,但由于某种原因我无法连接到数据库,因为它认为它不存在。

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

public void loadImg() 抛出 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 启动服务器,然后运行应用程序,一切就完美了。任何帮助将不胜感激。

【问题讨论】:

    标签: mysql database derby


    【解决方案1】:

    既然你指定了:

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

    作为您的连接 URL,Derby Network Server 正在使用相对数据库名称“InteractiveGameDatabase”查找数据库。由于这是一个相对名称,而不是绝对名称,因此 Derby Network Server 将在其主目录中查找数据库,这通常是您启动 Derby Network Server 时的当前目录。

    所以这里的情况可能是,当您在 NetBeans 中运行 Derby 网络服务器时,它会根据 NetBeans 的启动方式将某个目录作为其主目录运行。

    但是当您手动运行 Derby Network Server 时,它会在与其主目录不同的目录中运行,因为您并没有在 NetBeans 启动它的同一个目录中精确地启动它,因此它可以'在这个新目录中找不到数据库 InteractiveGameDatabase。

    你可以:

    1. 始终使用由 NetBeans 启动的 Derby 网络服务器
    2. 启动您自己的网络服务器,但安排在 NetBeans 启动 Derby 网络服务器的同一目录中进行此操作
    3. 启动您自己的网络服务器,但更改您的连接 URL 以指定运行 NetBeans 启动的 Derby 网络服务器的位置的完整绝对路径,以便您的网络服务器在您打开数据库时可以访问该目录.

    还有许多其他可能性,但希望这些足以让您了解正在发生的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      相关资源
      最近更新 更多