get Columns from Database table - Java java.sql

Java examples for java.sql:Table

Description

get Columns from Database table

Demo Code


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

import java.util.HashMap;

import java.util.Map;

public class Main {

    public static Map<String, Integer> getColumns(Connection conn,
            String tableName) {/* w w w.  j a va 2  s  . com*/
        ResultSet rs = null;
        Map<String, Integer> result = new HashMap<String, Integer>();
        try {
            rs = getDatabaseMetaData(conn).getColumns(null, null,
                    tableName, null);
            while (rs.next()) {
                result.put(rs.getString("COLUMN_NAME"),
                        rs.getInt("DATA_TYPE"));
            }
        } catch (SQLException e) {
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
            }
        }
        return result;
    }

    private static DatabaseMetaData getDatabaseMetaData(Connection conn)
            throws SQLException {
        return conn.getMetaData();
    }
}

Related Tutorials