Java SQL ResultSet Read readBitBoolean(ResultSet result, int index)

Here you can find the source of readBitBoolean(ResultSet result, int index)

Description

Read a Boolean (encoded in a bit or tinyint or string field or whatever) from the results set.

License

Apache License

Parameter

Parameter Description
results The result set.
index The index.

Exception

Parameter Description
SQLException an exception

Return

The Boolean.

Declaration

public static Boolean readBitBoolean(ResultSet result, int index) throws SQLException 

Method Source Code

//package com.java2s;
/**********************************************************************************
 * $URL$//from   ww w .j av  a 2  s  . c  o  m
 * $Id$
 ***********************************************************************************
 *
 * Copyright (c) 2007, 2008 The Regents of the University of Michigan & Foothill College, ETUDES Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **********************************************************************************/

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {
    /**
     * Read a Boolean (encoded in a bit or tinyint or string field or whatever) from the results set.
     * 
     * @param results
     *        The result set.
     * @param index
     *        The index.
     * @return The Boolean.
     * @throws SQLException
     */
    public static Boolean readBitBoolean(ResultSet result, int index) throws SQLException {
        Object o = result.getObject(index);
        if (o == null)
            return null;

        Boolean rv = null;
        if (o instanceof Boolean) {
            rv = (Boolean) o;
        } else if (o instanceof Integer) {
            rv = Boolean.valueOf(((Integer) o).intValue() == 1);
        } else if (o instanceof BigDecimal) {
            rv = Boolean.valueOf(((BigDecimal) o).intValue() == 1);
        } else if (o instanceof String) {
            if (((String) o).length() > 0) {
                String s = (String) o;

                if (s.equals("1") || s.equals("0")) {
                    rv = Boolean.valueOf(s.equals("1"));
                } else if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("false")) {
                    rv = Boolean.valueOf(s.equalsIgnoreCase("true"));
                } else {
                    rv = Boolean.valueOf(((String) o).charAt(0) == '\u0001');
                }
            }
        }

        return rv;
    }
}

Related

  1. getValue(ResultSet resultSet, String columnName, E defaultValue)
  2. getValue(ResultSet rs, String sField)
  3. getValue4Type(ResultSet rs, String column, Class typeClass)
  4. getValueByObjectType(ResultSetMetaData metaData, ResultSet rs, int index)
  5. getValueFromResultSet(int index, final ResultSet resultSet, final Class type)
  6. readBytes(ResultSet rs, int pos)
  7. readDate(ResultSet result, int index)
  8. readMap(final ResultSet rs)
  9. readObject(java.sql.ResultSet resultSet, int index)