Database not found error when connecting to Derby network server

ⅰ亾dé卋堺 提交于 2019-12-10 13:47:13

问题


I'm having problems initializing my javadb network server and setting a connection to it. It's a JavaFX program.

This is what I have so far:

try {
        Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
        javadbserver = new NetworkServerControl();
        javadbserver.start(null);
    } catch (ClassNotFoundException e) {
        Logger.getLogger(MainGuiController.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("Where is your JavaDB embedded Driver?");
        return;
    }

    String dbName = "mydb";
    String dbUser = "auser";
    String dbPass = "password";

    PreparedStatement prepstmt;

    try {
        this.conn = DriverManager.getConnection("jdbc:derby://localhost:1527/mydb;user=auser;password=password");
        System.out.println("Went through!");
    } catch (SQLException ex) {
        Logger.getLogger(MainGuiController.class.getName()).log(Level.SEVERE, null, ex);
    }

I always catch the second exception.

If I right click on the javadb service in netbeans and choose connect, everything runs smoothly. [Actually it'd be nice to know what code or program java runs in the background when I select that]

In my projects list under libraries I see derby.jar, derbyclient.jar and derbynet.jar

What am I doing wrong? Please help!

Here's the error I get

java.sql.SQLNonTransientConnectionException: The connection was refused because the database mydb was not found.
    at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
    at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
    at org.apache.derby.jdbc.ClientDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(DriverManager.java:579)
    at java.sql.DriverManager.getConnection(DriverManager.java:243)
    at mydb.MainGuiController.initialize(MainGuiController.java:105)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2152)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)
    at mydb.mydb.start(mydb.java:37)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.apache.derby.client.am.DisconnectException: The connection was refused because the database mydb was not found.
    at org.apache.derby.client.net.NetConnectionReply.parseRDBNFNRM(Unknown Source)
    at org.apache.derby.client.net.NetConnectionReply.parseAccessRdbError(Unknown Source)
    at org.apache.derby.client.net.NetConnectionReply.parseACCRDBreply(Unknown Source)
    at org.apache.derby.client.net.NetConnectionReply.readAccessDatabase(Unknown Source)
    at org.apache.derby.client.net.NetConnection.readSecurityCheckAndAccessRdb(Unknown Source)
    at org.apache.derby.client.net.NetConnection.flowSecurityCheckAndAccessRdb(Unknown Source)
    at org.apache.derby.client.net.NetConnection.flowUSRIDPWDconnect(Unknown Source)
    at org.apache.derby.client.net.NetConnection.flowConnect(Unknown Source)
    at org.apache.derby.client.net.NetConnection.<init>(Unknown Source)
    at org.apache.derby.client.net.NetConnection40.<init>(Unknown Source)
    at org.apache.derby.client.net.ClientJDBCObjectFactoryImpl40.newNetConnection(Unknown Source)

回答1:


By the JDBC url, it's looks like you're trying to connect to a Derby server vs. an embedded instance. If you are trying to connect to a server instance, here are some considerations:

  • did you start the server yourself, did mydb already exists?
  • if not, did you pass in the correct params to create (e.g. ;create=true) example: jdbc:derby://localhost:1527/dbname;create=true
  • if mydb did exists, are you pointing the server to the correct location?
    • also, depending on what was used to start derby (e.g. embedded vs network driver) default database locations are different as well. You can read about it here

Basically the exception you're getting is that Derby's saying it can't find your database - it's basically a path issue.




回答2:


If you're using netbeans, you should fix this by going into the connection properties and adding a property. Under "property" type "create" and under "value" type "true".



来源:https://stackoverflow.com/questions/17375504/database-not-found-error-when-connecting-to-derby-network-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!