连接到JDBC

时间:2017-07-04 19:06:27

标签: java jdbc derby

尝试使用JDBC和Apache Derby与Properties文件连接到本地数据库时,出现以下异常:

java.sql.SQLException: Database 'Mobsters' not found.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.newSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleDBNotFound(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.<init>(Unknown Source)
at org.apache.derby.jdbc.InternalDriver$1.run(Unknown Source)
at org.apache.derby.jdbc.InternalDriver$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.derby.jdbc.InternalDriver.getNewEmbedConnection(Unknown Source)
at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source)
at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source)
at org.apache.derby.jdbc.AutoloadedDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at com.adrian.mobsters.gui.controllers.MainController.getDBConnection(MainController.java:401)
at com.adrian.mobsters.gui.controllers.MainController$2.call(MainController.java:388)
at com.adrian.mobsters.gui.controllers.MainController$2.call(MainController.java:376)
at javafx.concurrent.Task$TaskCallable.call(Task.java:1423)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:748)
Caused by: ERROR XJ004: Database 'Mobsters' not found.
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.wrapArgsForTransportAcrossDRDA(Unknown Source)
    ... 22 more

我正在使用属性文件:

create=true
databaseName=Mobsters
user=myuser
password=mypw!
shutdown=true

我知道数据库不存在。这不是创建数据库吗?这是嵌入式应用程序数据库,如果它不存在,应该创建它。

用于设置数据库连接的代码:

public void initializeDB() {
    Task<Void> task = new Task() {
        @Override
        protected Void call() throws Exception {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            Properties properties = loadDBProperties();
            // Decide on the db system directory: <userhome>/.addressbook/
            String userHomeDir = System.getProperty("user.home", ".");
            String systemDir = userHomeDir + "/.mobsters";
            new File(systemDir).mkdir();
            // Set the db system directory.
            System.setProperty("derby.system.home", systemDir);

            getDBConnection(properties);

            MobsterDBUtils.createMobsterTable(getDBConnection());
            return null;
        }
    };

    new Thread(task).start();
}

public void getDBConnection(Properties properties) {
    String url = "jdbc:derby:";
    try {
         connection.set(DriverManager.getConnection(url, properties));
    } catch (SQLException ex) {
        Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public Properties loadDBProperties() {
    Properties prop = new Properties();

    InputStream is = MainController.class.getResourceAsStream("/derby/DerbyConfig.properties");

    try {
        prop.load(is);
    } catch (IOException ex) {
        Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
    }

    return prop;
}

据我所知,正在正确导入属性文件:

enter image description here

(这不是机密登录信息)

1 个答案:

答案 0 :(得分:0)

向网址添加shutdown=true会关闭连接。添加此属性后,不会创建数据库。完成数据库连接后应该使用它。