Search code examples
sqlderbycreate-table

SQLSyntaxErrorException while creating a table, using Apache Derby


I have the following line of code in my project which uses Apache Derby:

 stmt.executeUpdate("CREATE table "+productName+" (Time FLOAT NOT NULL PRIMARY KEY, AcidNo FLOAT, Viscosity FLOAT, Temperature FLOAT)");

When the variable productName begins with a letter (ie "RedProduct"), everything works fine. When productName begins with a digit (ie "6Green"), the table is created, but I get the following error:

java.sql.SQLSyntaxErrorException: Syntax error: Encountered "6" at line 1, column 15.
    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.executeQuery(Unknown Source)

Any ideas?? By the way, putting the table name in brackets did not help. Thanks!


Solution

  • Have you tried putting double quotes around the weird names?

    stmt.executeUpdate("CREATE table \"" + productName + 
        "\" (Time FLOAT NOT NULL PRIMARY KEY, AcidNo FLOAT, Viscosity FLOAT, Temperature FLOAT)");