the KD
the KD

Reputation: 13

SQL Exception : Unexpected Token - UCanAccess

I'm trying to create a new MS Access table from a Java program. The SQL query is below. I get an error saying:

net.UcanacessSQLException:UCAExc:::4.0.1 unexpected token: REQ-MTI

PreparedStatement prepStmt;

String createStmt = "CREATE TABLE [" + tableName + "] ([Test_Case_ID] CHAR (20) PRIMARY KEY, [Test_Name] CHAR (120)," 
    + " [Test_Description] CHAR (100), [Req-MTI] CHAR (15), [Req-Card_ID] CHAR (50), [Req-H19] CHAR (20));";

prepStmt = conn.prepareStatement(createStmt);
prepStmt.executeUpdate();

I can't use underscores instead of the hyphen. I've tried putting all the table names in square brackets, quotations, ect, but nothing seems to be working. I've taken the generated string and pasted it into a MS Access query and it creates the table fine there.

Any help would be appreciated. Thanks!

Upvotes: 1

Views: 1718

Answers (2)

Gord Thompson
Gord Thompson

Reputation: 123549

I was able to sort-of reproduce your issue under UCanAccess 4.0.2. It seems to be getting confused by the spaces following the CHAR keyword. I could get the code to run if I specified the fields as, e.g., CHAR(20) instead of CHAR (20).

Edit re: question update

I was able to reproduce the issue under UCanAccess 4.0.1. The workaround is to use a Statement instead of a PreparedStatement to execute the DDL. (It's not even necessary to remove the spaces as described above, at least under 4.0.1.)

Upvotes: 1

Pavan Chandaka
Pavan Chandaka

Reputation: 12751

If column names contain any characters except letters, numbers, and underscores, the name must be delimited by enclosing it in back quotes (`)

so try changing your column name to :

`Req-MTI`

Upvotes: 1

Related Questions