Java SQL Table Column resultSetContainsColumn(ResultSet rs, String column)

Here you can find the source of resultSetContainsColumn(ResultSet rs, String column)

Description

Scans through a result set's metadata in an attempt to find a column

License

Apache License

Parameter

Parameter Description
rs The ResultSet to scan
column The column to find

Exception

Parameter Description
SQLException Thrown in the event of an error whilst processing

Return

Boolean indicating if there was a column with said name

Declaration

public static boolean resultSetContainsColumn(ResultSet rs, String column) throws SQLException 

Method Source Code

//package com.java2s;
/*/*from   w  w w .  jav  a  2  s .  c  o m*/
 * Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
 * Copyright [2016-2018] EMBL-European Bioinformatics Institute
 * 
 * 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.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class Main {
    /**
     * Scans through a result set's metadata in an attempt to find a column
     * 
     * @param rs
     *            The ResultSet to scan
     * @param column
     *            The column to find
     * @return Boolean indicating if there was a column with said name
     * @throws SQLException
     *             Thrown in the event of an error whilst processing
     */
    public static boolean resultSetContainsColumn(ResultSet rs, String column) throws SQLException {
        ResultSetMetaData meta = rs.getMetaData();
        int total = meta.getColumnCount();
        for (int i = 1; i <= total; i++) {
            if (meta.getColumnName(i).equals(column)) {
                return true;
            }
        }
        return false;
    }
}

Related

  1. printTableColumn(Connection conn, String sql)
  2. readMultiDouble(ResultSet resultSet, String columnName)
  3. readString(final ResultSet resultSet, final String columnName)
  4. readStringFromBlob(ResultSet resultSet, String columnLabel)
  5. resultSetAsCount(ResultSet rs, String countColumn)
  6. resultSetHasColumn(ResultSet rs, String sColumnName)
  7. resultSetToOneColumnAsCommamaSeparatedString(ResultSet rs)
  8. resultSetToOneColumnAsList(ResultSet rs)
  9. resultSetToStringFormat(ResultSet rs, String separator, String quote, boolean column)