gpt4 book ai didi

java - 为什么 PreparedStatement.setNull 需要 sqlType?

转载 作者:太空狗 更新时间:2023-10-29 22:46:00 26 4
gpt4 key购买 nike

  1. 根据 PreparedStatement.setNull 的 java 文档:“注意:您必须指定参数的 SQL 类型”。方法需要列的SQL类型是什么原因?

  2. 我注意到传递 java.sql.Types.VARCHAR 也适用于非 varchar 列。是否存在 VARCHAR 不适用的场景(某些列类型或某些数据库提供商)?

谢谢。

最佳答案

According to the java docs of PreparedStatement.setNull: "Note: You must specify the parameter's SQL type". What is the reason that the method requires the SQL type of the column?

为了最大的兼容性;根据规范,有些数据库不允许将未类型化的 NULL 发送到基础数据源。

I noticed that passing java.sql.Types.VARCHAR also works for non-varchar columns. Are there scenarios in which VARCHAR won't be suitable (certain column types or certain DB providers)?

我不认为这种行为真的是规范的一部分,或者如果是,那么我确信那里正在进行某种隐式强制转换。在任何情况下,不建议依赖这种在底层数据存储更改时可能会中断的行为。为什么不指定正确的类型?

关于java - 为什么 PreparedStatement.setNull 需要 sqlType?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4243513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com