如何将 JavaDB 数据库集成到我的主 Java 包中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2984350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I integrate a JavaDB database into my main java package
提问by Stefanos Kargas
I am working on a desktop application which uses JavaDB. I am using NetBeans 6.8 and JDK 6 Update 20
我正在开发一个使用 JavaDB 的桌面应用程序。我正在使用 NetBeans 6.8 和 JDK 6 Update 20
I created the database I need and connected to it through my application using ClientDriver:
我创建了我需要的数据库并使用ClientDriver以下方法通过我的应用程序连接到它:
String driver = "org.apache.derby.jdbc.ClientDriver";
String connectionURL = "jdbc:derby://localhost:1527/myDB;create=true;user=user;password=pass";
try {
Class.forName(driver);
} catch (java.lang.ClassNotFoundException e) {
e.printStackTrace();
}
try {
schedoDBConnection = DriverManager.getConnection(connectionURL);
} catch (Exception e) {
e.printStackTrace();
}
This works fine. But in that case the service of the database comes from NetBeans. If I move my application to another PC I won't be able to access my database. How can I integrate my JavaDB into my application?
这工作正常。但在那种情况下,数据库的服务来自 NetBeans。如果我将应用程序移动到另一台 PC,我将无法访问我的数据库。如何将我的 JavaDB 集成到我的应用程序中?
采纳答案by Pascal Thivent
If I move my application to another PC I won't be able to access my database. How can I integrate my JavaDB into my application?
如果我将应用程序移动到另一台 PC,我将无法访问我的数据库。如何将我的 JavaDB 集成到我的应用程序中?
NetBeans starts Derby in Network Servermode and Derby is running in another JVM. If you want to embed your database inside an application, you'll need to start Derby in embedded modefrom within your application. To do so, use the EmbeddedDriverprovided by derby.jar.
NetBeans 在网络服务器模式下启动 Derby,而 Derby 在另一个 JVM 中运行。如果要将数据库嵌入到应用程序中,则需要在应用程序中以嵌入模式启动 Derby 。为此,请使用EmbeddedDriver提供的derby.jar.
/*
If you are running on JDK 6 or higher, you do not
need to invoke Class.forName(). In that environment, the
EmbeddedDriver loads automatically.
*/
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection conn = DriverManager.getConnection("jdbc:derby:sample");
By default, the database will be created/loaded from the working directory. If you want more control, the recommended way is to set the derby.system.homesystem property. Have a look at Using Java DB in Desktop Applications.
默认情况下,数据库将从工作目录创建/加载。如果你想要更多的控制,推荐的方法是设置derby.system.home系统属性。查看在桌面应用程序中使用 Java DB。
See also
也可以看看
回答by rgarcia
after you create db derby and connect with localhost you need add line in file derby.properties locate in you directory database
在创建 db derby 并连接到 localhost 后,您需要在文件 derby.properties 中添加行,位于您的目录数据库中
derby.drda.host=10.0.0.40 //example IPAddress
save and go to connection in netbeans and change localhost to you ipaddress
保存并转到 netbeans 中的连接并将 localhost 更改为您的 ipaddress
回答by duffymo
The database isn't coming from Netbeans; it's built into the JDK itself.
数据库不是来自 Netbeans;它内置于 JDK 本身。
Clients need to have the JDBC driver JAR available to them. If you're running the database as as server, your users will have to start the server. Maybe that's the piece that Netbeans is doing for you that will have to be replaced.
客户端需要有可用的 JDBC 驱动程序 JAR。如果您将数据库作为服务器运行,则您的用户必须启动服务器。也许这就是 Netbeans 为您所做的必须更换的部分。

