[英]How to insert Big Integer in prepared statement java
I want to insert a Big integer value using prepared statement, i have one string variable called xid (41527820021925053) 我想使用预处理语句插入一个大整数值,我有一个名为xid的字符串变量(41527820021925053)
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setObject(1,XOBJ);
preparedStatement.setObject(2,YOBJ);
preparedStatement.setBigInteger(3, xid);
preparedStatement.setInt(4, 23);
preparedStatement.executeUpdate();
preparedStatement.close();
I am new to this how to achieve this. 我是新手,如何实现这一目标。
PreparedStatement
doesn't have a setBigInteger()
method. PreparedStatement
没有setBigInteger()
方法。
Use one of these methods: 使用以下方法之一:
setBigDecimal(3, new BigDecimal(xid))
setLong(3, Long.parseLong(xid))
if value can fit within a long
setLong(3, Long.parseLong(xid))
if value是否适合long
setString(3, xid)
to let the JDBC driver convert the string for you setString(3, xid)
让JDBC驱动程序为你转换字符串 UPDATE UPDATE
With the following comment made by OP , the second option above (now highlighted) is the correct option to use, since PostgreSQL bigint
is the same a Java long
. 通过OP发出以下注释 ,上面的第二个选项(现在突出显示)是正确的选项,因为PostgreSQL
bigint
与Java long
相同。
guys i am using postgres & it has bigint ["UniqueIdGenerator"()] data-type,which is a 17 digit big integer.
我正在使用postgres并且它有bigint [“UniqueIdGenerator”()]数据类型,这是一个17位大整数。
If you are talking about an integer or long value in a String, then you should be able to just use setString(idx, yourValue)
, setObject(idx, yourValue)
, or setObject(idx, yourValue, java.sql.Types.BIGINT)
the driver should convert this to the target type. 如果你在谈论String中的整数或长整数值,那么你应该只能使用
setString(idx, yourValue)
, setObject(idx, yourValue)
或setObject(idx, yourValue, java.sql.Types.BIGINT)
驱动程序应将此转换为目标类型。
If you are talking about a java.math.BigInteger
, then a JDBC 4.1 (or higher) compliant driver should allow you to set a BigInteger
value to a BIGINT
, CHAR
, VARCHAR
or LONGVARCHAR
column using setObject(idx, yourBigInteger)
, or setObject(idx, yourBigInteger, targetType)
where targetType
is for example java.sql.Types.BIGINT
or java.sql.Types.VARCHAR
. 如果您正在讨论
java.math.BigInteger
,那么JDBC 4.1(或更高版本)兼容驱动程序应该允许您使用setObject(idx, yourBigInteger)
或setObject(idx, yourBigInteger, targetType)
将BigInteger
值设置为BIGINT
, CHAR
, VARCHAR
或LONGVARCHAR
列setObject(idx, yourBigInteger, targetType)
其中targetType
例如是java.sql.Types.BIGINT
或java.sql.Types.VARCHAR
。
However, be aware that not all drivers implement this support. 但是,请注意并非所有驱动程序都实现此支持。
See JDBC 4.1 specification section 3.1 Overview of changes , table B-4 Mapping from Java Object Types to JDBC Types , table B-5 Conversions Performed by setObject
and setNull
Between Java Object Types and Target JDBC Types . 请参阅JDBC 4.1规范部分3.1 更改概述 ,表B-4 从Java对象类型到JDBC类型的映射 ,表B-5 由
setObject
和setNull
执行的转换Java对象类型和目标JDBC类型之间的转换 。 Or alternatively, JDBC 4.2 specification , but then only tables B-4 and B-5. 或者, JDBC 4.2规范 ,但只有表B-4和B-5。
You can try 你可以试试
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setObject(1,XOBJ);
preparedStatement.setObject(2,YOBJ);
preparedStatement.setBigDecimal(3, BigDecimal.valueOf(Long.parseLong(xid))); //or you can try below
preparedStatement.setBigDecimal(3, new BigDecimal(xid)); //both are correct
preparedStatement.setInt(4, 23);
preparedStatement.executeUpdate();
preparedStatement.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.