问题
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 {
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());
}
}
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.
回答1:
Since you specified:
String host = "jdbc:derby://localhost:1527/InteractiveGameDatabase";
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.
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.
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.
You could:
- Always use the NetBeans-started Derby Network Server
- Start your own Network Server, but arrange to do so in the same directory where NetBeans starts the Derby Network Server
- Start your own Network Server, but change your connection URL to specify the full absolute path to the location where the NetBeans-started Derby Network Server was run, so that your Network Server can access that directory when you go to open the database.
There are many other possibilities, but hopefully these are enough to give you an idea about what's going on.
来源:https://stackoverflow.com/questions/30006470/derby-automatically-start-server-within-java-application-and-connect-to-database