3

I am using a JDBC connection to fetch data from an Access database. The database design is not my control. In the database there are columns that have "?" included in their names, for example: Open?, Paid?, and lots more.

When I try to fetch data with a PreparedStatement it gives me an error. The query is:

SELECT Open? FROM tblJobList WHERE WeekEnding=? 

I also tried to use brackets like [Open?], but the result is the same.

The error I receive is "Too few parameters ..." as I am pushing only one parameter into the PreparedStatement.

I can not use normal statement because of WeekEnding=? as this value is a Timestamp and I could not manage to work it with Statement. Only prepared statement works here.

Can anyone tell me how to use these kind of column names in a PreparedStatement?

3
  • Not sure about it, but did you try passing ? itself as the first parameter and the WeekEnding value as the second parameter? Commented May 8, 2013 at 3:01
  • No I didnt. Let me try Commented May 8, 2013 at 3:22
  • It created one more error. Earlier it was saying its short on 1 param now its say short on 2 parameters Commented May 8, 2013 at 3:27

4 Answers 4

2

use the " character

"SELECT \"Open?\" FROM tblJobList WHERE WeekEnding=?" 

tested this against oracle and appears to work with mssqlserver

How to select a column in SQL Server with a special character in the column name?

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

5 Comments

It doesnt give error but doesnt give back the result properly returns Column Name instead of value
my mistake. inside quotes becomes a literal.
Good suggestion, but unfortunately it does not work for Access ODBC (I just tested it in Eclipse and got the "Too few parameters" error when I tried to .execute() the prepared statement) so it appears not to help @user1614217 with their problem.
Problem is not with query but with prepared statment.
I havent find the solution for it. still struggling. But with my coding experience and knowledge I can tell the problem is not the SQL query its the Preparedstatments mechanism which replaces ? in the string.
1

Just to update this for current technologies:

While the JDBC-ODBC Bridge and Access ODBC were unable to handle a PreparedStatement with a column name containing a question mark, the UCanAccess JDBC driver handles it just fine, as can be confirmed with the following code:

String connectionUrl = "jdbc:ucanaccess://C:/Users/Public/UCanAccessTest.accdb"; Connection conn = DriverManager.getConnection(connectionUrl); String sql = "SELECT ID, [Open?] FROM tblJobList WHERE WeekEnding=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setDate(1, java.sql.Date.valueOf("2016-01-01")); ResultSet rs = ps.executeQuery(); while (rs.next()) { System.out.printf("%d: %s%n", rs.getInt("ID"), rs.getBoolean("Open?")); } conn.close(); 

For more information on UCanAccess, see

Manipulating an Access database from Java without ODBC

Comments

0

I am not sure but you can try // to escape the special meaning of ? and to use it as a normal character. Like:

"SELECT Open//? FROM tblJobList WHERE WeekEnding=?" 

You can get something similar to your problem here: Round bracket in string with JDBC prepared statement

1 Comment

No it doesnt Work. Problem is not with query but with prepared statment.
0

Escaping quotes in MSSQL is done by a double quote, so a '' or a "" will produce one escaped ' and ", respectively.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.