1

I am trying to create following table, but i continiously getting error. It is seems for me that Java Db tries to parse "create table ORDER" as "ORDER BY". I lso tried to type "CREATE TABLE" but, as expected, no result. How it is possible to solve that problem?

public void createTable() throws SQLException {
        String createString = "create table ORDER "
                + "(ORDER_ID int  NOT NULL, " + "ORDER_NUMBER int NOT NULL, "
                + "PERSON_ID int NOT NULL, " + "PRIMARY KEY (ORDER_ID), "
                + "FOREIGN KEY (PERSON_ID) REFERENCES PERSON (PERSON_ID))";
        Statement stmt = null;
        System.out.println(createString);
        try {

            DatabaseMetaData meta = con.getMetaData();
            ResultSet tables = meta.getTables(null, null, "ORDER",
                    new String[] { "TABLE2" });
            int size = 0;
            while (tables.next()) {
                size++;
            }
            if (size == 0) {
                System.out.println("table created");
                stmt = con.createStatement();
                stmt.executeUpdate(createString);

            }
        } catch (SQLException e) {
            System.out.println("No connection");
            e.printStackTrace();
            // TODO make exeption
        } finally {
            if (stmt != null) {
                stmt.close();
            }
        }
    }

error

java.sql.SQLSyntaxErrorException: Syntax error: Encountered "ORDER" at line 1, column 14.

    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.EmbedStatement.execute(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedStatement.executeUpdate(Unknown Source)
    at com.tuto.p4.OrderTable.createTable(OrderTable.java:38)
    at com.tuto.p4.Main.main(Main.java:18)
Caused by: java.sql.SQLException: Синтаксическая ошибка: Encountered "ORDER" at line 1, column 14.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
    ... 10 more
Caused by: ERROR 42X01: Syntax error: Encountered "ORDER" at line 1, column 14.
    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)
    ... 4 more

2 Answers 2

3

I do believe "ORDER" is a reserved word so you should not be using it as a table name or column name. Try simple synonyms that describe the your function, like "POSITION" or "SORTED", etc. Good luck!

EDIT: IF you really must use that word, you can try wrapping it in quotes like

create table `ORDER`

to force the sql engine to try and read it as a string.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, change of name works. Unfortunaly the task - to create table ORDER
java.sql.SQLSyntaxErrorException: Lexical error at line 1, column 15. Encountered: "" (96), after : "". So, ORDER` also does not work
The righ answer - suround word with "", I to use reserved name as a table or row name type "NAME"
0

Or you can use brackets like this to create your table:

create table `ORDER` ...

2 Comments

java.sql.SQLSyntaxErrorException: Syntax error: Encountered "\'ORDER\'" at line 1, column 15.
' is not the same as ` !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.