"Schema name-of-schema does not exist" 与 Java 德比

"Schema name-of-schema does not exist" with Java Derby

我目前正在用 Java 制作一个需要访问 Derby 数据库的程序。但是,当我尝试 运行 时,我收到错误 "Schema RCVT does not exist"。 而且,正如我编写的那样,程序结束了。 这是我的代码:

public ResultSetTableModel(String mController,
                           String URL,
                           String USERNAME,
                           String PASSWORD,
                           String mQuery) throws SQLException, ClassNotFoundException {
    Class.forName(mController);
    mConnection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
    mStatement = mConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    mConnected = true;
    setConsult(mQuery);
}

以下是我发送给此方法的参数:

private static final String CONTROLLER = "org.apache.derby.jdbc.EmbeddedDriver";
private static final String URL = "jdbc:derby:GOT";
private static final String USERNAME = "rcvt";
private static final String PASSWORD = "rcvt";
private static final String PREDETERMINED_CONSULT = "SELECT * FROM Person";

这是我在项目中从 ij> 执行的 SQL 脚本:

DROP TABLE Person;
CREATE TABLE Person (
    id int PRIMARY KEY NOT NULL GENERATED ALWAYS AS IDENTITY ,
    age int NOT NULL,
    name VARCHAR (15) NOT NULL,
    house VARCHAR (15) NOT NULL,
    allegiance VARCHAR (15) NOT NULL,
    alias VARCHAR (30) NOT NULL,
    continent VARCHAR (10) NOT NULL,
    region VARCHAR (25) NOT NULL,
    title VARCHAR (50) NOT NULL,
    active BOOLEAN NOT NULL
);

INSERT INTO Person (age, name, house, allegiance, alias, continent, region, title, active) VALUES (43, 'Cersei', 'Lannister', 'Lannister', 'The Lioness', 'Westeros', 'The Crownlands', 'Queen Of The Andals And The First Men', TRUE);

我已经为这个错误寻找了一段时间,但我无法在任何地方找到解决方案。

这是我得到的异常:

"C:\Program Files\Java\jdk1.8.0_131\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2017.1.2\lib\idea_rt.jar=50596:C:\Program Files\JetBrains\IntelliJ IDEA 2017.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar;C:\Users\Vidriales Trujillo R\IdeaProjects\AccesoDB\out\production\AccesoDB;C:\Program Files\Java\jdk1.8.0_131\db\lib\derby.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derby.war;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbynet.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbyrun.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbytools.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbyclient.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbyLocale_cs.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbyLocale_es.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbyLocale_fr.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbyLocale_hu.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbyLocale_it.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbyLocale_pl.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbyLocale_ru.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbyLocale_de_DE.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbyLocale_ja_JP.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbyLocale_ko_KR.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbyLocale_pt_BR.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbyLocale_zh_CN.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbyLocale_zh_TW.jar;C:\Program Files\Java\jdk1.8.0_131\db\lib\derbyoptionaltools.jar" Runner
java.sql.SQLSyntaxErrorException: Schema 'RCVT' does not exist
    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.TransactionResourceImpl.wrapInSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedStatement.executeQuery(Unknown Source)
    at ResultSetTableModel.setConsult(ResultSetTableModel.java:88)
    at ResultSetTableModel.<init>(ResultSetTableModel.java:28)
    at ConsultPersons.<init>(ConsultPersons.java:41)
    at GUI.<init>(GUI.java:9)
    at Runner.run(Runner.java:10)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access0(EventQueue.java:97)
    at java.awt.EventQueue.run(EventQueue.java:709)
    at java.awt.EventQueue.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: ERROR 42Y07: Schema 'RCVT' does not exist
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getSchemaDescriptor(Unknown Source)
    at org.apache.derby.iapi.sql.StatementUtil.getSchemaDescriptor(Unknown Source)
    at org.apache.derby.impl.sql.compile.QueryTreeNode.getSchemaDescriptor(Unknown Source)
    at org.apache.derby.impl.sql.compile.QueryTreeNode.getSchemaDescriptor(Unknown Source)
    at org.apache.derby.impl.sql.compile.TableName.bind(Unknown Source)
    at org.apache.derby.impl.sql.compile.FromBaseTable.bindNonVTITables(Unknown Source)
    at org.apache.derby.impl.sql.compile.FromList.bindTables(Unknown Source)
    at org.apache.derby.impl.sql.compile.SelectNode.bindNonVTITables(Unknown Source)
    at org.apache.derby.impl.sql.compile.DMLStatementNode.bindTables(Unknown Source)
    at org.apache.derby.impl.sql.compile.DMLStatementNode.bind(Unknown Source)
    at org.apache.derby.impl.sql.compile.CursorNode.bindStatement(Unknown Source)
    at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source)
    at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source)
    at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source)
    ... 21 more
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ConsultPersons.<init>(ConsultPersons.java:63)
    at GUI.<init>(GUI.java:9)
    at Runner.run(Runner.java:10)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access0(EventQueue.java:97)
    at java.awt.EventQueue.run(EventQueue.java:709)
    at java.awt.EventQueue.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

当它到达这一行时:

mTableModel = new ResultSetTableModel(CONTROLLER, URL, USERNAME, PASSWORD, PREDETERMINED_CONSULT);

哪个调用了这个方法:

public void setConsult(String mConsult) throws SQLException,
            IllegalStateException {

        if (!mConnected) {
            throw new IllegalStateException("Theres no DB connection");
        }
        mAllResults = mStatement.executeQuery(mConsult);
        mMetaData = mAllResults.getMetaData();
        mAllResults.last();
        mNumberOfRows = mAllResults.getRow();
        fireTableStructureChanged();
    }

看起来问题出在您的 SQL 脚本中。来自官方FAQ:

So you will see the schema not exists error if your application tries to access the current schema before any objects have been created in it. Possibilities are you try to perform a DROP TABLE before creating it, ...

这就是你的情况。正如您所提到的,您正在从执行脚本的 IDE 创建模式,因此第一次它不会工作,因为 table Person 不存在以及相应的模式。

要摆脱困境,请在您的脚本中显式创建模式,或者至少在第一个 运行 注释删除 table 命令中。

希望对您有所帮助!