0

Hi i created a derby embedded db with a simple java application. 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..!!!

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..!!! i already tried on other pc it gives me same error..! How can i make database created....!! Someone know please help..!

user3318622
  • 167
  • 2
  • 3
  • 11

2 Answers2

0

After building the jar, go to the project folder and copy the dist folder. Move it to a new location and also copy the database folder inside the new dist folder you just moved. That should do it; most of the time when people have problem with Derby it is because of Java file paths.

William Price
  • 4,033
  • 1
  • 35
  • 54
0

The magic about Derby schema creation is via jdbc url itself.

Let me use Java 8 Derby to elaborate. Run cmd.

Add Derby related to environment path:

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.

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. Then, we can use Derby normally like any other databases.

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.

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