Tarek
Tarek

Reputation: 3160

How to ignore database tablename case-sensitivity using Spring's JdbcTemplate?

Our Java (JDK6) application must support different databases, such as Oracle, SQL Server and DB2. We use Spring 3.0 and JDBCTemplate for database access. One of our clients uses SQL Server 2005 with case sensitivity and automatically upper cases table names.

Obviously, queries such as "select * from mytablename m" do not work for said client, as he would have the table MYTABLENAME.

For the following code, for instance, I get a nice exception:

this.jdbcTemplate.queryForOject("select * from mytablename m");
`Exception in thread "main" org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [select * from mytablename m]; nested exception is java.sql.SQLException: Invalid object name 'mytablename'.`

I'm stuck with having to adapt the code - since I can't change my client's database options - so the most obvious solution would be to upper case all the table names in our queries. I know that'll work for this client, but what if a new client has a database with case sensitivity and lower case table names?

So far I haven't succeeded in finding a broader solution. Most answers I've found required changing the database's case-sensitivity or rewriting the queries. I've tried using: this.jdbcTemplate.setResultsMapCaseInsensitive(true); but if I understood well, it applies only to the results of my query, not the query itself.

Is there a way to execute a case-insensitive query using Spring's JdbcTemplate?

Upvotes: 2

Views: 4695

Answers (2)

Qwerky
Qwerky

Reputation: 18435

If you are supporting a large number of databases you might find more problems than just case sensitive names. This is a great opportunity to stress the importance of abstracting your data access with an interface, and writing implementations for each supported database vendor.

In your app you should code to the interface. As you are using spring you can use dependency injection to use the appropriate DB implementation in your data access layer; its just a trivial change to the application context configuration file.

Upvotes: 2

Ortwin Angermeier
Ortwin Angermeier

Reputation: 6183

I think your problem here is that user is a reserverd keyword on MS-SQL server.

Also take a look at here: Is SQL syntax case sensitive?

Upvotes: 0

Related Questions