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

发布于 2024-09-04 20:20:26 字数 682 浏览 7 评论 0原文

我正在开发一个使用 JavaDB 的桌面应用程序。 我正在使用 NetBeans 6.8 和 JDK 6 Update 20,

我创建了我需要的数据库,并使用 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();
    }

这工作正常。但在这种情况下,数据库的服务来自 NetBeans。如果我将应用程序移至另一台电脑,我将无法访问我的数据库。如何将 JavaDB 集成到我的应用程序中?

I am working on a desktop application which uses JavaDB.
I am using NetBeans 6.8 and JDK 6 Update 20

I created the database I need and connected to it through my application using 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谜兔 2024-09-11 20:20:26

如果我将应用程序移至另一台 PC,我将无法访问我的数据库。如何将 JavaDB 集成到我的应用程序中?

NetBeans 以 网络服务器 模式启动 Derby,并且 Derby 运行在另一个 JVM。如果要将数据库嵌入到应用程序中,则需要在 嵌入模式在您的应用程序中。为此,请使用 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");

默认情况下,将从工作目录创建/加载数据库。如果您想要更多控制,建议的方法是设置 derby.system.home 系统属性。查看在桌面应用程序中使用 Java DB

另请参阅

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 starts Derby in Network Server mode 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 mode from within your application. To do so, use the EmbeddedDriver provided by 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.home system property. Have a look at Using Java DB in Desktop Applications.

See also

二智少女猫性小仙女 2024-09-11 20:20:26

该数据库不是来自 Netbeans;而是来自 Netbeans。它内置于 JDK 本身中。

客户端需要有可用的 JDBC 驱动程序 JAR。如果您将数据库作为服务器运行,您的用户将必须启动服务器。也许这就是 Netbeans 正在为您做的、必须被替换的部分。

The database isn't coming from Netbeans; it's built into the JDK itself.

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.

水水月牙 2024-09-11 20:20:26

创建 db derby 并与 localhost 连接后,您需要在文件 derby.properties 中添加行,定位到您的目录数据库

derby.drda.host=10.0.0.40  //example IPAddress

保存并转到 netbeans 中的连接并将 localhost 更改为您的 ipaddress

after you create db derby and connect with localhost you need add line in file derby.properties locate in you directory database

derby.drda.host=10.0.0.40  //example IPAddress

save and go to connection in netbeans and change localhost to you ipaddress

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文