为什么带有derby的可执行jar不起作用?

问题描述 投票:1回答:1

我正在开发一个简单的应用程序作为学校项目,旨在供客户在其计算机上使用。我决定使用derby DB,因为它可以在嵌入式模式下工作。但是,由于在eclipse中一切正常,当我尝试将其导出到可执行jar文件时,该应用无法正常运行。

我正在尝试连接到数据库并创建表(如果它们不存在,如代码所示)。第一次启动jar之后,一个名为sampleDB的新文件出现在屏幕上,因此我猜想已创建db。但是,除了GUI组件之外,该程序根本无法运行。我首先执行main中的那些表方法,所以我想它们可能有问题,但是,eclipse不会显示任何错误。

我提到过,这里有一段代码负责连接和创建表。

        public static Connection getConnection() throws Exception{
            try {    
                Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
                  //Getting the Connection object
                  String URL = "jdbc:derby:sampleDB;create=true";
                  conn = DriverManager.getConnection(URL);
                return conn;    
            }catch(Exception e) {System.out.println(e);}
            return null;
    }
        public static void table1() throws Exception {
             Connection conn = getConnection();
             try {
              java.sql.Statement stmt = conn.createStatement();
              if  (!doesTableExists("costs", conn)) {
             String query= "CREATE TABLE costs("
                     +"Id INT NOT NULL GENERATED ALWAYS AS IDENTITY, "
                     +"Name VARCHAR(60),"
                     +"value DOUBLE NOT NULL, "
                     +"driverdependent INT NOT NULL,"
                     +"costorpofit INT NOT NULL, "
                     +"fixedorvariable INT NOT NULL,"
                     +"driver VARCHAR(40),"
                     +"truck VARCHAR(40),"
                     +"semitrailer VARCHAR(40),"
                     +"Data TIMESTAMP,"
                     +"PRIMARY KEY (Id))";
             stmt.execute(query);
              }
        }catch(Exception e) {System.out.println(e);}
        }
        static boolean doesTableExists(String tableName, Connection conn)
                throws SQLException {
            DatabaseMetaData meta = conn.getMetaData();
            ResultSet result = meta.getTables(null, null, tableName.toUpperCase(), null);
            return result.next();
        }
        public static void table2() throws Exception {
            try {
         Connection conn = getConnection();
         java.sql.Statement stmt = conn.createStatement();
        if  (!doesTableExists("journeys", conn)) {
            String query= "CREATE TABLE journeys("
                 +"Id INT NOT NULL GENERATED ALWAYS AS IDENTITY, "
                 +"millage DOUBLE NOT NULL, "
                 +"driver VARCHAR(40),"
                 +"truck VARCHAR(40),"
                 +"semitrailer VARCHAR(40),"
                 +"Data TIMESTAMP,"
                 +"PRIMARY KEY (Id))";
        stmt.execute(query);
        }
        }catch(Exception e) {System.out.println(e);}
    }
        public static void table3() throws Exception {
            try {
             Connection conn = getConnection();
             java.sql.Statement stmt = conn.createStatement();
             if  (!doesTableExists("routes", conn)) {
             String query= "CREATE TABLE routes("
                     +"millage DOUBLE NOT NULL, "
                     +"Name VARCHAR(40))";
                stmt.execute(query);
             }
            }catch(Exception e) {System.out.println(e);}
            }
java swing jdbc derby
1个回答
0
投票

为什么带有derby的可执行jar不起作用? ..尝试连接到数据库并创建表。.

嵌入式DB是只读的。无法更新。

..我该怎么办?在我客户的计算机上安装derby(?)

就是这样。

© www.soinside.com 2019 - 2024. All rights reserved.