为什么PreparedStatement.setNull需要sqlType? [英] Why does PreparedStatement.setNull requires sqlType?

查看:249
本文介绍了为什么PreparedStatement.setNull需要sqlType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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

  1. 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?

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

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:注意:你
必须指定参数的SQL
类型。
方法需要
列的SQL类型的原因是什么?

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发送到基础数据源。

For maximum compatibility; as per the specification, there are some databases which don't allow untyped NULL to be sent to the underlying data source.


我注意到了传递
java.sql.Types.VARCHAR也适用于
非varchar列。是否有
场景,其中VARCHAR不适合
(某些列类型或
某些数据库提供商)?

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)?

我不认为这种行为真的是规范的一部分,或者如果是,那么我肯定会有某种隐含的强制行为。在任何情况下,都不建议依赖于在基础数据存储区发生更改时可能会中断的此类行为。为什么不指定正确的类型?

I don't think that sort of behaviour really is part of the specification or if it is, then I'm sure there is some sort of implicit coercion going on there. In any case, relying on such sort of behaviour which might break when the underlying datastore changes is not recommended. Why not just specify the correct type?

这篇关于为什么PreparedStatement.setNull需要sqlType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆