Mysql Derby在java应用程序中自动启动服务器并连接到数据库
正在尝试自动连接到系统上的数据库。 数据库位于通过NetBeans创建的默认Derby文件夹中。 我要做的是启动服务器并连接到已经存在的数据库Mysql Derby在java应用程序中自动启动服务器并连接到数据库,mysql,database,derby,Mysql,Database,Derby,正在尝试自动连接到系统上的数据库。 数据库位于通过NetBeans创建的默认Derby文件夹中。 我要做的是启动服务器并连接到已经存在的数据库 public void startServer() throws Exception { NetworkServerControl server = new NetworkServerControl(); server.start(prntWrt); } @Override public void start(Stage primaryS
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启动服务器,然后运行应用程序,一切都很完美。如果您提供任何帮助,我们将不胜感激。因为您指定:
String host = "jdbc:derby://localhost:1527/InteractiveGameDatabase";
作为您的连接URL,Derby网络服务器正在使用相对数据库名称“InteractiveGameDatabase”查找数据库。由于这是一个相对名称,而不是绝对名称,因此Derby网络服务器将在其主目录中查找数据库,该目录通常是启动Derby网络服务器时的当前目录
因此,这里可能发生的情况是,当您在NetBeans中运行Derby网络服务器时,根据NetBeans启动它的方式,它以某个目录作为其主目录运行
但是,当您自己手动运行Derby网络服务器时,它会在与其主目录不同的目录中运行,因为您没有准确地在NetBeans启动它的同一目录中启动它,因此它无法在这个新目录中找到数据库InteractiveGameDatabase
你可以: