Get an integer that is returned as a result of a query. - Java java.sql

Java examples for java.sql:ResultSet

Description

Get an integer that is returned as a result of a query.

Demo Code


//package com.java2s;
import java.sql.Connection;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
    /**/*from   w w  w . ja  va2s.com*/
     * Get an integer that is returned as a result of a query.
     * @param conn
     * @param query
     * @return result
     * @throws SQLException
     */
    public static int getIntFromDB(Connection conn, String query)
            throws SQLException {

        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(query);
        int ret = Integer.MIN_VALUE;
        if (rs.next()) {
            ret = rs.getInt(1);
        }
        rs.close();
        st.close();
        return ret;
    }
}

Related Tutorials