[英]How to set bind values to NULL with Firebird's jaybird JDBC driver?
I'm running into a bit of a situation in corner cases when binding null
to a PreparedStatement
with Firebird's jaybird JDBC driver. 当使用Firebird的jaybird JDBC驱动程序将null
绑定到PreparedStatement
,我遇到了一些极端情况。 Here's a sample statement: 这是一个示例语句:
Class.forName("org.firebirdsql.jdbc.FBDriver");
// Observe the "local" in the connection string!
Connection con = DriverManager.getConnection(
"jdbc:firebirdsql:local:C:/data/firebird/test.db", "TEST", "TEST");
// With this connection, I'm not able to reproduce the issue:
Connection con1 = DriverManager.getConnection(
"jdbc:firebirdsql:localhost:C:/data/firebird/test.db", "TEST", "TEST");
PreparedStatement stmt = con.prepareStatement(
"SELECT cast(? as varchar(1)) FROM rdb$database");
stmt.setObject(1, null);
ResultSet rs = stmt.executeQuery();
rs.next();
System.out.println(rs.getString(1));
System.out.println(rs.wasNull());
The output of the above program is 上面程序的输出是
>
> false
The first line being an empty string. 第一行是一个空字符串。 It really should be 真的应该是
> null
> true
Changing this line ... 改变这条线...
stmt.setObject(1, null);
... into any of these lines ... ...进入任何这些行...
stmt.setString(1, null);
stmt.setNull(1, Types.VARCHAR);
... doesn't help either. ……也无济于事。 A workaround is to inline null
literals in SQL statements, instead of binding them to the prepared statement. 一种解决方法是在SQL语句中内联null
文字,而不是将它们绑定到准备好的语句。 What am I missing? 我想念什么?
Details: 细节:
I can't reproduce the issue on Jaybird 2.2.0 and Firebird 2.1.3 (32-bit) on Windows 7 x64 . 我无法在Windows 7 x64的 Jaybird 2.2.0和Firebird 2.1.3(32位)上重现该问题。 Copy pasting the source code and running it produces exactly the expected output. 复制粘贴源代码并运行它会产生预期的输出。
Screenshot attached just in case: 截图以防万一:
UPDATE UPDATE
Tested on Jaybird 2.2.0 & Firebird 2.5.1 (32bit) on Windows XP , still cannot reproduce the issue -> Exact same output as expected. 在Windows XP的 Jaybird 2.2.0和Firebird 2.5.1(32位)上进行了测试,仍然无法重现该问题->与预期的输出完全相同。
Apparently, this is a bug in the JDBC driver: 显然,这是JDBC驱动程序中的错误:
http://tracker.firebirdsql.org/browse/JDBC-271 http://tracker.firebirdsql.org/browse/JDBC-271
It only appears when using this sort of connection URL: 仅在使用以下类型的连接URL时出现:
// Observe the "local" in the connection string!
Connection con = DriverManager.getConnection(
"jdbc:firebirdsql:local:C:/data/firebird/test.db", "TEST", "TEST");
Not with this sort: 不适用于这种情况:
// With this connection, I'm not able to reproduce the issue:
Connection con1 = DriverManager.getConnection(
"jdbc:firebirdsql:localhost:C:/data/firebird/test.db", "TEST", "TEST");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.