Java Derby使用“Schema name-of-schema”

时间:2017-05-29 06:43:33

标签: java derby javadb

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

$cursor=collection->find($query);
$cursor=iterator_to_array($cursor);
foreach($cursor as $doc){
  foreach($doc as $docc){
    echo $docc['name_of_report'];
  }
}

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

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);
}

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

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";

我一直在寻找这个错误,我无法在任何地方找到解决方案。

这是我得到的例外:

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$1.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.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.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$1.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.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.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);

1 个答案:

答案 0 :(得分:1)

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

  

因此,如果您的应用程序在其中创建任何对象之前尝试访问当前架构,您将看到架构不存在错误。您可能会在创建DROP TABLE之前尝试执行DROP TABLE,...

这是你的情况。正如您所提到的,您正在从执行脚本的IDE创建架构,因此第一次它不起作用,因为表Person不存在以及相应的架构。

要解除此问题,请在脚本中显式创建架构,或者至少在第一次运行comment drop table命令中。

希望它有所帮助!