如何使用Derby创建内存数据库表?

时间:2011-11-10 19:07:03

标签: java jdbc derby javadb in-memory-database

我想使用Derby / JavaDB为测试目的创建一个内存数据库。我已阅读Java DB Developer's Guide: Using in-memory databases,并在“构建路径”中使用JDK7中的derby.jar对此代码进行了测试:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DBTest {
    public static void main(String[] args) {
        final String sql = "DECLARE GLOBAL TEMPORARY TABLE memtable "+
                            "(id int, name varchar(10))";
        final String connURL = "jdbc:derby:memory:memdatabase;create=true";

        try(Connection conn = DriverManager.getConnection(connURL);
            PreparedStatement ps = conn.prepareStatement(sql);) {

            boolean success = ps.execute();
            System.out.println("Memory database created: " + success);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

但是在运行应用程序时,我收到此错误:

java.sql.SQLSyntaxErrorException: Syntax error: Encountered "<EOF>" at line 1, column 66.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.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.EmbedPreparedStatement.<init>(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedPreparedStatement20.<init>(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedPreparedStatement30.<init>(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedPreparedStatement40.<init>(Unknown Source)
    at org.apache.derby.jdbc.Driver40.newEmbedPreparedStatement(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
    at DBTest.main(DBTest.java:12)
Caused by: java.sql.SQLException: Syntax error: Encountered "<EOF>" at line 1, column 66.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
    ... 14 more
Caused by: ERROR 42X01: Syntax error: Encountered "<EOF>" at line 1, column 66.
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.impl.sql.compile.ParserImpl.parseStatement(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)
    ... 8 more

如何使用Derby / JavaDB创建内存数据库表?

1 个答案:

答案 0 :(得分:3)

我认为你最后错过了没有记录:

  declare global temporary table SESSION.t1(c11 int) not logged;