如何将 derby 数据库与构建 .jar 文件一起使用?

how can i use derby database with the build .jar file?

我正在使用 java 和 derby 数据库。

当我开始连接 netbeans 中的 "jdbc:derby"-数据库和 运行 我的代码时,程序出现并且一切正常。

通过打开构建的“.jar”文件,程序会立即出现并立即关闭。

那么我必须如何配置构建 .jar 文件与 derby 数据库一起工作的 netbeans?

(我认为 derby 是基于文件的,所以构建过程必须生成类似数据库文件的东西)

如果您的程序在执行您的jar 文件期间崩溃,则可能存在异常。如果它在 Netbeans 中有效,则异常可能是由 derby.jar.

的路径引起的

我首先想到的是您使用相对路径将 derby.jar 包含在您的应用程序中,并且您没有将 derby.jar 移动到您的项目库中。

尝试执行以下操作:

1:将derby.jar复制到项目文件夹中的目录(例如/lib)

2:使用 /lib 文件夹中的相对路径包含 jar

编辑:

下面是如何在新建数据库时创建数据库的示例代码: Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

        Connection con = DriverManager.getConnection("jdbc:derby:C:\Users\TheReaver\MyDB;create=true;user=thereaver;password=12345");
        PreparedStatement st = con.prepareStatement("SELECT ID, NAME FROM APP.TABLE1");
        try
        {
            ResultSet res = st.executeQuery();
        }
        catch(SQLException e) {
            //if table not found, then create all tables
            st.executeQuery("CREATE TABLE TABLE1(ID int, NAME varchar(20)))");
            //execute all statements that create your database
            //or execute an sql file that stores all your queries
        }