Search code examples
javajdbcderbyjavadbin-memory-database

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?


Solution

  • I think you're missing NOT LOGGED at the end:

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