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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:25:54  来源:igfitidea点击:

How to insert Big Integer in prepared statement java

javapostgresqljdbcprepared-statement

提问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

PreparedStatementdoesn't have a setBigInteger()method.

PreparedStatement没有setBigInteger()方法。

Use one of these methods:

使用以下方法之一:



UPDATE

更新

With the following comment made by OP, the second option above (now highlighted) is the correct option to use, since PostgreSQL bigintis 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 BigIntegervalue to a BIGINT, CHAR, VARCHARor LONGVARCHARcolumn using setObject(idx, yourBigInteger), or setObject(idx, yourBigInteger, targetType)where targetTypeis for example java.sql.Types.BIGINTor java.sql.Types.VARCHAR.

如果您在谈论 a java.math.BigInteger,那么符合 JDBC 4.1(或更高版本)的驱动程序应该允许您使用或where将BigInteger值设置为 a BIGINTCHARVARCHARLONGVARCHARsetObject(idx, yourBigInteger),例如或。setObject(idx, yourBigInteger, targetType)targetTypejava.sql.Types.BIGINTjava.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 setObjectand setNullBetween 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();