具有 oracle NUMBER 类型的 jdbc 准备语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17345975/
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-09-19 01:45:49  来源:igfitidea点击:

jdbc prepared statement with oracle NUMBER type

javaoraclejdbcojdbc

提问by medium

I have a java application which sets up a jdbc connection to an Orcale database. I am attempting to insert data into the database but am confused when it comes to the oracle NUMBER type. I have three columns in my table which are of these types respectively.

我有一个 java 应用程序,它设置了一个到 Orcale 数据库的 jdbc 连接。我正在尝试将数据插入到数据库中,但是当涉及到 oracle NUMBER 类型时,我感到很困惑。我的表中有三列分别属于这些类型。

NUMBER(38,0)
NUMBER(20,0)
NUMBER(16,0)

My first question is what type of java type should I put the data in, in order to use it in a prepared statement.

我的第一个问题是我应该将数据放入什么类型的 java 类型,以便在准备好的语句中使用它。

My second question is what set operation can I use in a prepared statement in order to insert the data.

我的第二个问题是我可以在准备好的语句中使用什么设置操作来插入数据。

Lets just assume we are working with NUMBER(38,0). Would I set the java type to a BigInteger? If I had an integer 1 would it be

让我们假设我们正在使用 NUMBER(38,0)。我会将 java 类型设置为 BigInteger 吗?如果我有一个整数 1 会是

 BigInteger one = new BigInteger(1);

Then in my preparedStatement it would be

然后在我的 PreparedStatement 中

 PreparedStatement pstmt = conn.prepareStatement("INSERT INTO TABLE(bigInt) VALUES(?)");
 pstmt.setLong(1, one);

This seems to not work, so I assume that this is not correct. Any help would be appreciated.

这似乎不起作用,所以我认为这是不正确的。任何帮助,将不胜感激。

采纳答案by GriffeyDog

setLong()cannot take a BigInteger. If you truly have values exceeding the range of longin your database, then you may need to use setBigDecimal(), as there is no setBigInteger(), and your variable would have to be of type BigDecimal. If longencompasses the range of values in your database, then just use longand setLong().

setLong()不能拿BigInteger. 如果您的数据库中确实有超出范围的值long,那么您可能需要使用setBigDecimal(),因为没有setBigInteger(),并且您的变量必须是 类型BigDecimal。如果long包含数据库中的值范围,则只需使用longsetLong()

回答by Marcio Duarte

you can try this way:

你可以试试这样:

 oracle.sql.NUMBER numberValue = new oracle.sql.NUMBER(bigIntegerValue);
 cs.setObject(id, numberValue, OracleTypes.NUMBER);

where bigIntegerValueis an instance of java.math.BigInteger, it works for me

bigIntegerValue的实例在哪里java.math.BigInteger,它对我有用