简体   繁体   English

找不到带有jar文件的Derby Embedded抛出数据库异常

[英]Derby Embedded with jar file throw database not found exception

Hi i created a derby embedded db with a simple java application. 嗨,我用一个简单的Java应用程序创建了一个derby嵌入式数据库。 When test run on eclipse it run perfectly.And then i export as a runnable jar file .Run via cmd gives exception database not found..!!! 当在eclipse上运行测试时,它可以完美运行。然后我将其导出为可运行的jar文件。通过cmd运行会导致未找到异常数据库。

public class Main {

    public static void main(String[] args) throws SQLException {
    final String driver="org.apache.derby.jdbc.EmbeddedDriver";
    final String url="jdbc:derby:db/testdb";

    try {
        Class.forName(driver);
        Connection connection=DriverManager.getConnection(url);

        //connection.createStatement().execute("create table channels(channel varchar(20),topic varchar(20))");
    //  connection.createStatement().execute("insert into channels (channel,topic) values('hbo','action')");
    //  System.out.println("saved");
        PreparedStatement preStmt=connection.prepareStatement("select * from channels");
        ResultSet set=null;
        set=preStmt.executeQuery();

        while(set.next()){


            System.out.print(set.getString(1));
            System.out.println(set.getString(2));

        }       

    } catch (ClassNotFoundException e) {

        e.printStackTrace();
    }

    }

Errors 失误

Exception in thread "main" SQL Exception: Database 'db/testdb' not found.
        at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Sourc

My need is when i run jar file on other java enabled pc it must run..!!! 我的需要是,当我在其他启用Java的PC上运行jar文件时,它必须运行.. !!! i already tried on other pc it gives me same error..! 我已经在其他电脑上尝试过,它给了我同样的错误..! How can i make database created....!! 我该如何创建数据库。 Someone know please help..! 有人知道请帮忙..!

After building the jar, go to the project folder and copy the dist folder. 构建jar之后,转到项目文件夹并复制dist文件夹。 Move it to a new location and also copy the database folder inside the new dist folder you just moved. 将其移动到新位置,并将数据库文件夹复制到刚移动的新dist文件夹中。 That should do it; 那应该做; most of the time when people have problem with Derby it is because of Java file paths. 在大多数情况下,人们对Derby遇到问题是因为Java文件路径。

The magic about Derby schema creation is via jdbc url itself. 关于Derby模式创建的魔术是通过jdbc url本身。

Let me use Java 8 Derby to elaborate. 让我使用Java 8 Derby进行详细说明。 Run cmd . 运行cmd

Add Derby related to environment path: 添加与环境路径相关的Derby:

cd D:\Project\derbydb
set JAVA_HOME=C:/Program Files/Java/jdk1.8.0_92
set DERBY_HOME=C:/Program Files/Java/jdk1.8.0_92/db
set PATH=%PATH%;%DERBY_HOME%/bin

Execute ij command to work with Derby. 执行ij命令以与Derby一起使用。

D:\Project\derbydb>ij
ij version 10.11
ij> CONNECT 'jdbc:derby:testdb;create=true';

When url="jdbc:derby:testdb;create=true" , [D:\\Project\\derbydb\\testdb] folder is automatically initialized from where it is run. url =“ jdbc:derby:testdb; create = true”时 ,将从运行位置自动初始化[D:\\ Project \\ derbydb \\ testdb]文件夹。 Then, we can use Derby normally like any other databases. 然后,我们可以像其他任何数据库一样正常使用Derby。

ij> CREATE TABLE cart (
      item VARCHAR(50),
      price DECIMAL (10,5),
      dt TIMESTAMP,
      primary key (item)
    );
ij> select * from cart;

After Derby repository exists, then we can connect it from anywhere. Derby存储库存在后,我们可以从任何地方进行连接。

C:\Users\oraclesoon>ij
ij version 10.11
ij> CONNECT 'jdbc:derby:D:\\Project\\derbydb\\testdb';
ij> select * from cart;

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

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