postgresql 如何在准备好的语句java中插入大整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40376198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert Big Integer in prepared statement java
提问by Andreas
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.
我对如何实现这一点很陌生。
回答by Andreas
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 along
setString(3, xid)
to let the JDBC driver convert the string for you
setBigDecimal(3, new BigDecimal(xid))
setLong(3, Long.parseLong(xid))
如果值可以适合long
setString(3, xid)
让 JDBC 驱动程序为您转换字符串
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的以下评论,上面的第二个选项(现在突出显示)是正确的使用选项,因为 PostgreSQLbigint
与 Java 相同long
。
guys i am using postgres & it has bigint ["UniqueIdGenerator"()] data-type,which is a 17 digit big integer.
伙计们,我正在使用 postgres 并且它具有 bigint ["UniqueIdGenerator"()] 数据类型,这是一个 17 位的大整数。
回答by Mark Rotteveel
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.
如果您在谈论字符串中的整数或长值,那么您应该可以只使用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
.
如果您在谈论 a java.math.BigInteger
,那么符合 JDBC 4.1(或更高版本)的驱动程序应该允许您使用或where将BigInteger
值设置为 a BIGINT
、CHAR
、VARCHAR
或LONGVARCHAR
列setObject(idx, yourBigInteger)
,例如或。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 specificationsection 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. Or alternatively, JDBC 4.2 specification, but then only tables B-4 and B-5.
请参阅JDBC 4.1 规范部分 3.1更改概述、表 B-4从 Java 对象类型到 JDBC 类型的映射、表 B-5由Java 对象类型和目标 JDBC 类型执行的转换setObject
以及setNull
在 Java 对象类型和目标 JDBC 类型之间执行的转换。或者,JDBC 4.2 规范,但只有表 B-4 和 B-5。
回答by deadpool
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();