Derby在java应用程序中自动启动服务器并连接到数据库

时间:2015-05-02 19:18:27

标签: mysql database derby

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

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启动服务器然后运行应用程序,一切都很完美。任何帮助将不胜感激。

0 个答案:

没有答案