Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a BigInt value from a resultset (Oracle db) in Java

I want to get a BIGINT value from Oracle database using JDBC.
getBigInt() or getBigInteger() does not work like getInt().

Here is the code snippet:

public List<Employee> getAllEmployees()
{
    List<Employee> employeeList = new ArrayList<Employee>();
    try
    {
        //typical jdbc coding
        Connection conn = DBUtil.getConnection();
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM employee1");
        while(rs.next())
        {
            Employee employee = new Employee(rs.getString("emp_id"), rs.getString("name"), rs.getBigInt("emp_mob"));
            employeeList.add(employee);
        }
        DBUtil.closeConnection(conn);  //close connection
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    
    return employeeList;
}

emp_mob column in the table contains big integer values.

like image 473
davis_john Avatar asked Oct 15 '25 13:10

davis_john


1 Answers

The BIGINT data type is an 8-byte binary number, which means that the matching Java type is a long, so use getLong():

long mob = rs.getLong("emp_mob");

If the column is NULL-able, use Java type Long, and call wasNull() after calling getLong():

Long mob = rs.getLong("emp_mob");
if (rs.wasNull())
    mob = null;

Alternatively, if you want a Java BigInteger, call getBigDecimal() and convert it:

BigDecimal decimal = rs.getBigDecimal("emp_mob");
BigInteger mob = (decimal == null ? null : decimal.toBigInteger());
like image 158
Andreas Avatar answered Oct 17 '25 04:10

Andreas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!