简体   繁体   English

如何将JavaDB数据库集成到我的主java包中

[英]How do I integrate a JavaDB database into my main java package

I am working on a desktop application which uses JavaDB. 我正在使用一个使用JavaDB的桌面应用程序。 I am using NetBeans 6.8 and JDK 6 Update 20 我正在使用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. 但在这种情况下,数据库的服务来自NetBeans。 If I move my application to another PC I won't be able to access my database. 如果我将我的应用程序移动到另一台PC,我将无法访问我的数据库。 How can I integrate my JavaDB into my application? 如何将JavaDB集成到我的应用程序中?

If I move my application to another PC I won't be able to access my database. 如果我将我的应用程序移动到另一台PC,我将无法访问我的数据库。 How can I integrate my JavaDB into my application? 如何将JavaDB集成到我的应用程序中?

NetBeans starts Derby in Network Server mode and Derby is running in another JVM. NetBeans在网络服务器模式下启动Derby,Derby在另一个JVM中运行。 If you want to embed your database inside an application, you'll need to start Derby in embedded mode from within your application. 如果要将数据库嵌入到应用程序中,则需要从应用程序中以嵌入模式启动Derby。 To do so, use the EmbeddedDriver provided by derby.jar . 为此,请使用derby.jar提供的EmbeddedDriver

/*
    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.home system property. 如果需要更多控制,建议的方法是设置derby.system.home系统属性。 Have a look at Using Java DB in Desktop Applications . 看看在桌面应用程序使用Java DB

See also 也可以看看

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

The database isn't coming from Netbeans; 该数据库不是来自Netbeans; it's built into the JDK itself. 它内置于JDK本身。

Clients need to have the JDBC driver JAR available to them. 客户端需要为他们提供JDBC驱动程序JAR。 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. 也许这就是Netbeans为你做的那件必须被替换的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM