gpt4 book ai didi

java - 仅限 String 类型的 JDBC setObject

转载 作者:行者123 更新时间:2023-11-30 10:59:48 26 4
gpt4 key购买 nike

我正在使用包装 JDBC 的 JDBC 模板 (SPRING)。在运行时,我所有的变量值(绑定(bind)变量)都保存为字符串(对其实际类型有一些识别:bigint\varchar 等)。当我使用 setObject 时,我不确定是否需要将变量值转换为它的真实类型,或者我可以将变量作为字符串发送到 setObject,数据库将根据数据库中的列名转换它(即如果它与 BigInt 进行比较,那么它会将字符串转换为 big int,然后进行查询等。)

谢谢,

最佳答案

如果我正确理解您的问题,那么我认为您不能使用 setObject 来传递将转换为非字符串 SQL 类型(int、bigint)的字符串。

this question .以下是该问题的答案。

It is not the job of setObject to determine the correct conversion to the column type. The setObject javadoc says, "The JDBC specification specifies a standard mapping from Java Object types to SQL types. The given argument will be converted to the corresponding SQL type before being sent to the database." So it is solely looking at the Java type of the object passed to it and converting that to a SQL type. So you pass it a String and it converts it to a varchar which is appropriate. If you want setObject to do a conversion to a different type, that is the reason for the additional setObject variant which takes a target sql type to convert to, but that doesn't help your situation where you don't know what the target type is.

关于java - 仅限 String 类型的 JDBC setObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31791081/

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