Java SQL ResultSet Read getId(ResultSet key)

Here you can find the source of getId(ResultSet key)

Description

Extract key from given ResultSet.

License

Open Source License

Parameter

Parameter Description
key resultSet with key

Exception

Parameter Description
SQLException when operation fails

Return

key from given result set

Declaration

public static Long getId(ResultSet key) throws SQLException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**/*from  ww w  . ja va 2  s. com*/
     * Extract key from given ResultSet.
     * 
     * @param key resultSet with key
     * @return key from given result set
     * @throws SQLException when operation fails
     */
    public static Long getId(ResultSet key) throws SQLException {
        if (key.getMetaData().getColumnCount() != 1) {
            throw new IllegalArgumentException("Given ResultSet contains more columns");
        }
        if (key.next()) {
            Long result = key.getLong(1);
            if (key.next()) {
                throw new IllegalArgumentException("Given ResultSet contains more rows");
            }
            return result;
        } else {
            throw new IllegalArgumentException("Given ResultSet contain no rows");
        }
    }
}

Related

  1. getHashMap(ResultSet resultSet)
  2. getHeaders(ResultSetMetaData rsmd)
  3. getHighPrecisionString(ResultSet rs, int ix, int sql_type)
  4. getHtmlRows(ResultSet results)
  5. getHtmlTable(ResultSet results)
  6. getIndex(ResultSet rs, int index)
  7. getList(ResultSet resultSet, String columnLabel, Class clazz)
  8. getList(ResultSet rs)
  9. getListFromRS(Class clazz, ResultSet rs)