简体   繁体   English

JDBC PreparedStatement在列名称中带有“?”

[英]JDBC PreparedStatement with “?” in the column name

I am using a JDBC connection to fetch data from an Access database. 我正在使用JDBC连接从Access数据库中获取数据。 The database design is not my control. 数据库设计不是我的控制。 In the database there are columns that have "?" 数据库中有带有“?”的列。 included in their names, for example: Open? 包括在他们的名字中,例如: Open? , Paid? Paid? , and lots more. ,还有更多。

When I try to fetch data with a PreparedStatement it gives me an error. 当我尝试使用PreparedStatement获取数据时,它给我一个错误。 The query is: 查询是:

SELECT Open? FROM tblJobList WHERE WeekEnding=?

I also tried to use brackets like [Open?] , but the result is the same. 我也尝试使用[Open?]类的括号,但结果是相同的。

The error I receive is "Too few parameters ..." as I am pushing only one parameter into the PreparedStatement. 我收到的错误是“参数太少...”,因为我仅将一个参数推入PreparedStatement。

I can not use normal statement because of WeekEnding=? 由于WeekEnding=?我无法使用普通语句WeekEnding=? as this value is a Timestamp and I could not manage to work it with Statement. 因为此值为时间戳,所以我无法使用Statement。 Only prepared statement works here. 只有准备好的语句在这里起作用。

Can anyone tell me how to use these kind of column names in a PreparedStatement? 谁能告诉我如何在PreparedStatement中使用这类列名?

use the " character 使用“字符

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

tested this against oracle and appears to work with mssqlserver 针对oracle测试了这一点,似乎可以与mssqlserver一起使用

How to select a column in SQL Server with a special character in the column name? 如何在SQL Server中选择列名称中包含特殊字符的列?

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: 虽然JDBC-ODBC Bridge和Access ODBC无法处理带有包含问号的列名的PreparedStatement,但UCanAccess JDBC驱动程序可以很好地处理它,可以通过以下代码确认:

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 有关UCanAccess的更多信息,请参见

Manipulating an Access database from Java without ODBC 在不使用ODBC的情况下从Java操作Access数据库

I am not sure but you can try // to escape the special meaning of ? 我不确定,但是您可以尝试//逃避special meaning of ?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 您可以在此处获得与您的问题类似的内容: 带有JDBC准备语句的字符串中的圆括号

在MSSQL中,转义引号是通过双引号来完成的,因此“”或“”将分别产生一个转义的'和'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM