简体   繁体   中英

JDBC Mixed Case Table and Column Names

Is it possible in Java, using JDBC, to insert into a DB2 table (or any RDBMS) where the table names and columns are mixed case? For example, I want to execute SQL such as this:

insert into schemaname."TableName" ("ColumnName1", "ColumnName2") VALUES ('a', 'b')

So I try putting that into a format such as the following, substituting single quotes for the double quotes used for table and column names shown above:

Integer x = jdbcTemplate.update("insert into schemaname.'TableName' "
                        + "('ColumnName1', 'ColumnName2') VALUES (?,?)",
                        new Object[] { col1, col2});

However, this (and any other formatting I've tried) gives me sql errors that seem to take issue with my syntax (eg, DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704). Any help is much appreciated!

Single quotes are for character literals (aka "strings"), not for identifiers. You need to use double quotes the same way you do in plain SQL. However inside a Java String, you need to escape the double quotes:

jdbcTemplate.update("insert into schemaname.\"TableName\" "
                    + "(\"ColumnName1\", \"ColumnName2\") VALUES (?,?)",
                    new Object[] { col1, col2});

This is one of the reasons why using quoted identifiers is usually not such a good idea.

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