java 无法连接到我的 Derby 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7855889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:42:19  来源:igfitidea点击:

Can't connect to my Derby database

javajdbcderby

提问by sahar

I created a new database using Java DB named "Test", and I tried to create a connection using java DB Embedded driver, but when I enter Test in database name and add user name and pass and press OK, an error appears:

我使用名为“Test”的 Java DB 创建了一个新数据库,并尝试使用 Java DB Embedded 驱动程序创建连接,但是当我在数据库名称中输入 Test 并添加用户名并通过并按 OK 时,出现错误:

"Unable to add connection. cannot establish a connection to jdbc.derby.Test using org.apache.derby.jdbc.EmbeddedDriver (database 'Test' not found)"

“无法添加连接。无法使用 org.apache.derby.jdbc.EmbeddedDriver 建立到 jdbc.derby.Test 的连接(未找到数据库‘Test’)”

Why do I get this message?

为什么我会收到这条消息?

then, when I wrote my code

然后,当我写我的代码时

   String conStr = "jdbc:derby:Test";
    String driver2 = "org.apache.derby.jdbc.EmbeddedDriver";
    try {
        Class.forName(driver2);
        System.out.println("driver");
    } catch (ClassNotFoundException e1) {
        e1.printStackTrace();
    }
    try {
        Properties props = new Properties();
        props.put("user", "sahar");
        props.put("password", "123456"); 
        //Connection conn = DriverManager.getConnection(conStr);
        Connection conn = DriverManager.getConnection(conStr,props);
        System.out.println("connect ");
    } catch (SQLException e) {
        e.printStackTrace();
    }

it throws an exception "Database 'Test' not found"

它引发异常“未找到数据库‘测试’”

回答by

If indeed you have created a local database, try pointing to the absolute file-name instead of the relative one.

如果您确实创建了本地数据库,请尝试指向绝对文件名而不是相对文件名。

Unix:
jdbc:derby:/srv/databases/Test

Windows:
jdbc:derby:c:/databases/Test

Better way might be to access a database via a Derby network server

更好的方法可能是通过 Derby 网络服务器访问数据库

回答by corsiKa

Without seeing code it's hard to guess, but perhaps you need create=trueon your connection string.

没有看到代码很难猜测,但也许您需要create=true连接字符串。

final String connectionURL = "jdbc:derby:" + dbName + ";create=true";

(I pulled this copy and paste out of an app I have that uses an embedded database.)

(我从我拥有的使用嵌入式数据库的应用程序中提取并粘贴了此副本。)