简体   繁体   中英

Java Derby does not find database

Java Derby does not find a database even with "create=true" sentence.

The code is following:

public class Main{

public static void main(String[] args) {        
        try {
        DriverManager.getConnection("jdbc:derby:javaDb;create=true");
        } catch (SQLException e){
            e.printStackTrace();
        }
} 

In CMD, from libs folder:

 java -jar derbyrun.jar ij version ij 10.15 connect 'jdbc:derby:javaDb'; ERROR XJ004: Database 'javaDb' not found. show connections No connection available.

If I have interpreted your question correctly...

Quick Answer

(a) Run the ij start-up command in the same location where your Java program created the new database (see below).

or

(b) Create the DB using ij at the same time as you connect to it (also see below).


More Notes

When you create a new Derby DB using the Java code in your question, that will create the DB in the root folder of where the code (the Java project) was executed.

Look for a javaDb folder there, containing the Derby database files.

But when you try to connect to that database at the ij> command prompt, after running this:

java -jar derbyrun.jar ij

...and then using connect 'jdbc:derby:javaDb'; you are telling ij that the DB is in the lib directory of the Derby installation folder.

Those are probably two different locations - hence you get your "not found" error.

You can locate and copy the javaDb folder from where it was created (your Java app) to the Derby installation's lib folder. But probably not a good idea. You should keep the lib folder clean.

Alternatively...

You can execute the java -jar derbyrun.jar ij command from the parent folder of where the javaDb folder is located (ie from your Java project root folder:

java -jar %DERBY_HOME%\lib\derbyrun.jar ij

(I am assuming that by "CMD" you mean a Windows command prompt.)

If DERBY_HOME is not defined, then just use the full path to the Derby lib folder.

Then, at the ij> prompt, retry your connection command:

connect 'jdbc:derby:javaDb';

So, for example, for me, the Java command is this:

java -jar C:\derbydb\db-derby-10.16.1.1-bin\lib\derbyrun.jar ij

And I run the above command from where my Java project is located:

C:\Users\me\Documents\JavaDerbyDemo

Use ij For DB Creation

What you maybe want to do instead of all of the above is to use ij to actually create the DB as you first try to connect to it in ij :

connect 'jdbc:derby:javaDbTwo;create=true';

That will create a javaDbTwo folder (containing your new, empty Derby DB) in the same location where you run the command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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