Java DataSource getTableColumnCount(DataSource dataSource, String selectedTable)

Here you can find the source of getTableColumnCount(DataSource dataSource, String selectedTable)

Description

get Table Column Count

License

Open Source License

Declaration

public static int getTableColumnCount(DataSource dataSource, String selectedTable) throws SQLException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Robert "Unlogic" Olofsson (unlogic@unlogic.se).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0-standalone.html
 ******************************************************************************/

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;

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

public class Main {
    public static int getTableColumnCount(DataSource dataSource, String selectedTable) throws SQLException {

        Connection connection = null;
        ResultSet rs = null;/*from  w  w w  .  j  a v  a2  s . co m*/

        try {

            connection = dataSource.getConnection();

            DatabaseMetaData meta = connection.getMetaData();

            rs = meta.getColumns(null, null, selectedTable, null);

            rs.last();

            return rs.getRow();

        } finally {
            closeResultSet(rs);
            closeConnection(connection);
        }
    }

    public static void closeResultSet(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
        }
    }

    public static void closeConnection(Connection connection) {
        try {
            if (connection != null && !connection.isClosed()) {
                connection.close();
            }
        } catch (SQLException e) {
        }
    }
}

Related

  1. getGlobalVariable(DataSource dataSource, String variable)
  2. getJdbcUrlFromDataSource(DataSource dataSource)
  3. getJNDIConnectionByContainer(String dataSource)
  4. getProductAllCount(DataSource ds, String key, String sql_allcount)
  5. getSchemaSeparator(DataSource dataSource)
  6. getTables(DataSource datasource)
  7. hasTable(DataSource ds, String table)
  8. initDataSource()
  9. initDatasource(String jndiName)