简体   繁体   中英

How to create an in-memory database table using Derby?

I would like to create an in-memory database for testing purposes using Derby/JavaDB. I have read Java DB Developer's Guide: Using in-memory databases and tested with this code with derby.jar from JDK7 in the "build path":

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

But when running the application, I get this error:

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

How can I create an in-memory database table with Derby/JavaDB?

我想你最后遗漏的是没有记录:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM