当使用 Firebird 的 jaybird JDBC 驱动程序将 null
绑定(bind)到 PreparedStatement
时,我在极端情况下遇到了一些情况。这是一个示例声明:
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());
上述程序的输出是
>
> false
第一行是一个空字符串。确实应该这样
> null
> true
更改此行...
stmt.setObject(1, null);
...进入任何这些行...
stmt.setString(1, null);
stmt.setNull(1, Types.VARCHAR);
...也没有帮助。解决方法是在 SQL 语句中内联 null
文字,而不是将它们绑定(bind)到准备好的语句。我缺少什么?
详细信息:
- 数据库:Firebird WI-V2.5.1.26351
- JDBC 驱动程序:jaybird-2.2.0
- Java 版本:JDK 1.6.0_24
- 操作系统:Windows 7 x64
- JDBC 连接字符串:参见上文。
最佳答案
我无法在 Windows 7 x64 上的 Jaybird 2.2.0 和 Firebird 2.1.3(32 位) 上重现该问题。 复制粘贴源代码并运行它会产生完全预期的输出。
附上屏幕截图以防万一:
更新
在Windows XP上的Jaybird 2.2.0和Firebird 2.5.1(32位)上进行测试,仍然无法重现该问题 -> 确切与预期相同的输出。
关于java - 如何使用 Firebird 的 jaybird JDBC 驱动程序将绑定(bind)值设置为 NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12106422/