java - 如何使用 Firebird 的 jaybird JDBC 驱动程序将绑定(bind)值设置为 NULL?

标签 java jdbc firebird jaybird

当使用 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.0Firebird 2.1.3(32 位) 上重现该问题。 复制粘贴源代码并运行它会产生完全预期的输出。

附上屏幕截图以防万一:

enter image description here

更新

Windows XP上的Jaybird 2.2.0Firebird 2.5.1(32位)上进行测试,仍然无法重现该问题 -> 确切与预期相同的输出。

关于java - 如何使用 Firebird 的 jaybird JDBC 驱动程序将绑定(bind)值设置为 NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12106422/

相关文章:

java - 在 Hibernate 中将实体字段子集映射为单独的类的有效方法

java - 将 `META-INF/services` 用于驱动程序的内部管道

sql - 是否可以在 Firebird 的存储过程中返回多行?

java - 两个应用程序使用 JDBC 连接到 mysql 数据库 : one works,,另一个给出 "The Network Adapter could not establish the connection"

java - 如何在不从中删除先前结果的情况下更新结果集

postgresql - postgresql 和 firebird 记录集的不同行为

delphi - 使用 TClientDataSet 在 ApplyUpdates 后获取触发器生成的值

java - 如何将java Intent 转换为tasker Intent

java - 在 AWS Elasticache (Redis) 上使用 SAVE 命令的 Jedis

java - 找不到符号(arrayList 排序集合)