How to insert Big Integer into a prepared java statement

I want to insert a large integer value using a prepared statement, I have one string variable called 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 how to achieve this.

+4
source share
3 answers

PreparedStatementdoesn't have a method setBigInteger().

Use one of the following methods:


UPDATE

OP, ( ) - , PostgreSQL bigint - , Java long.

, postgres, bigint [ "UniqueIdGenerator" ()] , 17- .

+9

String, setString(idx, yourValue), setObject(idx, yourValue) setObject(idx, yourValue, java.sql.Types.BIGINT), .

java.math.BigInteger, JDBC 4.1 ( ) BigInteger BIGINT, CHAR, VARCHAR LONGVARCHAR setObject(idx, yourBigInteger) setObject(idx, yourBigInteger, targetType), targetType - , , java.sql.Types.BIGINT java.sql.Types.VARCHAR.

, .

. JDBC 4.1 3.1 , B-4 Java JDBC, B-5 , setObject setNull Java JDBC. , , JDBC 4.2, B-4 B-5.

+1

    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();
0

Source: https://habr.com/ru/post/1659592/


All Articles