Example usage for java.sql ResultSetMetaData getColumnName

List of usage examples for java.sql ResultSetMetaData getColumnName

Introduction

In this page you can find the example usage for java.sql ResultSetMetaData getColumnName.

Prototype

String getColumnName(int column) throws SQLException;

Source Link

Document

Get the designated column's name.

Usage

From source file:PrintColumns.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;/*from   w w w . j  a  v  a2  s.  c  o  m*/
    String query = "select * from COFFEES";
    Statement stmt;

    try {
        Class.forName("myDriver.ClassName");

    } catch (java.lang.ClassNotFoundException e) {
        System.err.print("ClassNotFoundException: ");
        System.err.println(e.getMessage());
    }

    try {
        con = DriverManager.getConnection(url, "myLogin", "myPassword");

        stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery(query);
        ResultSetMetaData rsmd = rs.getMetaData();

        PrintColumnTypes.printColTypes(rsmd);
        System.out.println("");

        int numberOfColumns = rsmd.getColumnCount();

        for (int i = 1; i <= numberOfColumns; i++) {
            if (i > 1)
                System.out.print(",  ");
            String columnName = rsmd.getColumnName(i);
            System.out.print(columnName);
        }
        System.out.println("");

        while (rs.next()) {
            for (int i = 1; i <= numberOfColumns; i++) {
                if (i > 1)
                    System.out.print(",  ");
                String columnValue = rs.getString(i);
                System.out.print(columnValue);
            }
            System.out.println("");
        }

        stmt.close();
        con.close();
    } catch (SQLException ex) {
        System.err.print("SQLException: ");
        System.err.println(ex.getMessage());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    st = conn.createStatement();/*  ww  w .j a v  a2  s. c om*/
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    ResultSetMetaData rsMetaData = rs.getMetaData();

    int numberOfColumns = rsMetaData.getColumnCount();
    System.out.println("resultSet MetaData column Count=" + numberOfColumns);

    for (int i = 1; i <= numberOfColumns; i++) {
        System.out.println("column MetaData ");
        System.out.println("column number " + i);
        System.out.println(rsMetaData.getColumnDisplaySize(i));
        System.out.println(rsMetaData.getColumnLabel(i));
        System.out.println(rsMetaData.getColumnName(i));
        System.out.println(rsMetaData.getColumnType(i));
        System.out.println(rsMetaData.getColumnTypeName(i));
        System.out.println(rsMetaData.getColumnClassName(i));
        System.out.println(rsMetaData.getTableName(i));
        System.out.println(rsMetaData.getPrecision(i));
        System.out.println(rsMetaData.getScale(i));
        System.out.println(rsMetaData.isAutoIncrement(i));
        System.out.println(rsMetaData.isCurrency(i));
        System.out.println(rsMetaData.isWritable(i));
        System.out.println(rsMetaData.isDefinitelyWritable(i));
        System.out.println(rsMetaData.isNullable(i));
        System.out.println(rsMetaData.isReadOnly(i));
        System.out.println(rsMetaData.isCaseSensitive(i));
        System.out.println(rsMetaData.isSearchable(i));
        System.out.println(rsMetaData.isSigned(i));
        System.out.println(rsMetaData.getCatalogName(i));
        System.out.println(rsMetaData.getSchemaName(i));
    }
    st.close();
    conn.close();
}

From source file:com.ingby.socbox.bischeck.test.JDBCtest.java

static public void main(String[] args)
        throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {

    CommandLineParser parser = new GnuParser();
    CommandLine line = null;/*from   w  ww.ja  v a  2s  . c  o  m*/

    // create the Options
    Options options = new Options();
    options.addOption("u", "usage", false, "show usage.");
    options.addOption("c", "connection", true, "the connection url");
    options.addOption("s", "sql", true, "the sql statement to run");
    options.addOption("m", "meta", true, "get the table meta data");
    options.addOption("C", "columns", true, "the number of columns to display, default 1");
    options.addOption("d", "driver", true, "the driver class");
    options.addOption("v", "verbose", false, "verbose outbout");

    try {
        // parse the command line arguments
        line = parser.parse(options, args);
    } catch (org.apache.commons.cli.ParseException e) {
        System.out.println("Command parse error:" + e.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("JDBCtest", options);
        Util.ShellExit(1);
    }

    if (line.hasOption("verbose")) {
        verbose = true;
    }

    if (line.hasOption("usage")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("Bischeck", options);
        Util.ShellExit(0);
    }

    String driverclassname = null;
    if (!line.hasOption("driver")) {
        System.out.println("Driver class must be set");
        Util.ShellExit(1);
    } else {
        driverclassname = line.getOptionValue("driver");
        outputln("DriverClass: " + driverclassname);
    }

    String connectionname = null;
    if (!line.hasOption("connection")) {
        System.out.println("Connection url must be set");
        Util.ShellExit(1);
    } else {
        connectionname = line.getOptionValue("connection");
        outputln("Connection: " + connectionname);
    }

    String sql = null;
    String tablename = null;

    if (line.hasOption("sql")) {
        sql = line.getOptionValue("sql");
        outputln("SQL: " + sql);

    }

    if (line.hasOption("meta")) {
        tablename = line.getOptionValue("meta");
        outputln("Table: " + tablename);
    }

    int nrColumns = 1;
    if (line.hasOption("columns")) {
        nrColumns = new Integer(line.getOptionValue("columns"));
    }

    long execStart = 0l;
    long execEnd = 0l;
    long openStart = 0l;
    long openEnd = 0l;
    long metaStart = 0l;
    long metaEnd = 0l;

    Class.forName(driverclassname).newInstance();
    openStart = System.currentTimeMillis();
    Connection conn = DriverManager.getConnection(connectionname);
    openEnd = System.currentTimeMillis();

    if (tablename != null) {
        ResultSet rsCol = null;
        metaStart = System.currentTimeMillis();
        DatabaseMetaData md = conn.getMetaData();
        metaEnd = System.currentTimeMillis();

        rsCol = md.getColumns(null, null, tablename, null);
        if (verbose) {
            tabular("COLUMN_NAME");
            tabular("TYPE_NAME");
            tabular("COLUMN_SIZE");
            tabularlast("DATA_TYPE");
            outputln("");
        }

        while (rsCol.next()) {
            tabular(rsCol.getString("COLUMN_NAME"));
            tabular(rsCol.getString("TYPE_NAME"));
            tabular(rsCol.getString("COLUMN_SIZE"));
            tabularlast(rsCol.getString("DATA_TYPE"));
            outputln("", true);
        }
    }

    if (sql != null) {
        Statement stat = conn.createStatement();
        stat.setQueryTimeout(10);

        execStart = System.currentTimeMillis();
        ResultSet res = stat.executeQuery(sql);
        ResultSetMetaData rsmd = res.getMetaData();
        execEnd = System.currentTimeMillis();

        if (verbose) {
            for (int i = 1; i < nrColumns + 1; i++) {
                if (i != nrColumns)
                    tabular(rsmd.getColumnName(i));
                else
                    tabularlast(rsmd.getColumnName(i));
            }
            outputln("");
        }
        while (res.next()) {
            for (int i = 1; i < nrColumns + 1; i++) {
                if (i != nrColumns)
                    tabular(res.getString(i));
                else
                    tabularlast(res.getString(i));
            }
            outputln("", true);
        }

        stat.close();
        res.close();
    }

    conn.close();

    // Print the execution times
    outputln("Open time: " + (openEnd - openStart) + " ms");

    if (line.hasOption("meta")) {
        outputln("Meta time: " + (metaEnd - metaStart) + " ms");
    }

    if (line.hasOption("sql")) {
        outputln("Exec time: " + (execEnd - execStart) + " ms");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    st = conn.createStatement();/*from   w  ww  .jav  a 2  s .  c  o m*/
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    ResultSetMetaData rsMetaData = rs.getMetaData();

    int numberOfColumns = rsMetaData.getColumnCount();
    System.out.println("resultSet MetaData column Count=" + numberOfColumns);

    for (int i = 1; i <= numberOfColumns; i++) {
        System.out.println("column MetaData ");
        System.out.println("column number " + i);
        // indicates the designated column's normal maximum width in
        // characters
        System.out.println(rsMetaData.getColumnDisplaySize(i));
        // gets the designated column's suggested title
        // for use in printouts and displays.
        System.out.println(rsMetaData.getColumnLabel(i));
        // get the designated column's name.
        System.out.println(rsMetaData.getColumnName(i));

        // get the designated column's SQL type.
        System.out.println(rsMetaData.getColumnType(i));

        // get the designated column's SQL type name.
        System.out.println(rsMetaData.getColumnTypeName(i));

        // get the designated column's class name.
        System.out.println(rsMetaData.getColumnClassName(i));

        // get the designated column's table name.
        System.out.println(rsMetaData.getTableName(i));

        // get the designated column's number of decimal digits.
        System.out.println(rsMetaData.getPrecision(i));

        // gets the designated column's number of
        // digits to right of the decimal point.
        System.out.println(rsMetaData.getScale(i));

        // indicates whether the designated column is
        // automatically numbered, thus read-only.
        System.out.println(rsMetaData.isAutoIncrement(i));

        // indicates whether the designated column is a cash value.
        System.out.println(rsMetaData.isCurrency(i));

        // indicates whether a write on the designated
        // column will succeed.
        System.out.println(rsMetaData.isWritable(i));

        // indicates whether a write on the designated
        // column will definitely succeed.
        System.out.println(rsMetaData.isDefinitelyWritable(i));

        // indicates the nullability of values
        // in the designated column.
        System.out.println(rsMetaData.isNullable(i));

        // Indicates whether the designated column
        // is definitely not writable.
        System.out.println(rsMetaData.isReadOnly(i));

        // Indicates whether a column's case matters
        // in the designated column.
        System.out.println(rsMetaData.isCaseSensitive(i));

        // Indicates whether a column's case matters
        // in the designated column.
        System.out.println(rsMetaData.isSearchable(i));

        // indicates whether values in the designated
        // column are signed numbers.
        System.out.println(rsMetaData.isSigned(i));

        // Gets the designated column's table's catalog name.
        System.out.println(rsMetaData.getCatalogName(i));

        // Gets the designated column's table's schema name.
        System.out.println(rsMetaData.getSchemaName(i));
    }

    st.close();
    conn.close();
}

From source file:RSMetaData.java

public static void printColumnNames(ResultSet rs) throws SQLException {
    ResultSetMetaData rmd = rs.getMetaData();
    for (int col = 1; col <= rmd.getColumnCount(); col++)
        System.out.println(rmd.getColumnName(col) + " ");
}

From source file:RSMetaData.java

public static void printColumnInfo(ResultSet rs) throws SQLException {
    ResultSetMetaData rsmd = rs.getMetaData();
    int cols = rsmd.getColumnCount();
    for (int colIdx = 1; colIdx <= cols; colIdx++) {
        String name = rsmd.getColumnName(colIdx);
        int type = rsmd.getColumnType(colIdx);
        String typeName = rsmd.getColumnTypeName(colIdx);
        System.out.println(name + ", " + type + ", " + typeName);
    }/*from   w ww. jav a  2  s . c  o  m*/
}

From source file:Main.java

private static void outputResultSet(ResultSet rs) throws Exception {
    ResultSetMetaData rsMetaData = rs.getMetaData();
    int numberOfColumns = rsMetaData.getColumnCount();
    for (int i = 1; i < numberOfColumns + 1; i++) {
        String columnName = rsMetaData.getColumnName(i);
        System.out.print(columnName + "   ");

    }//from www .j a va2 s. c  o m
    while (rs.next()) {
        for (int i = 1; i < numberOfColumns + 1; i++) {
            System.out.print(rs.getString(i) + "   ");
        }
        System.out.println();
    }

}

From source file:Main.java

private static void outputResultSet(ResultSet rs) throws Exception {
    ResultSetMetaData rsMetaData = rs.getMetaData();
    int numberOfColumns = rsMetaData.getColumnCount();
    for (int i = 1; i < numberOfColumns + 1; i++) {
        String columnName = rsMetaData.getColumnName(i);
        System.out.print(columnName + "   ");

    }/*from   w w  w .  j a  v  a 2 s.c  om*/
    System.out.println();
    System.out.println("----------------------");

    while (rs.next()) {
        for (int i = 1; i < numberOfColumns + 1; i++) {
            System.out.print(rs.getString(i) + "   ");
        }
    }

}

From source file:Main.java

private static void outputResultSet(ResultSet rs) throws Exception {
    ResultSetMetaData rsMetaData = rs.getMetaData();
    int numberOfColumns = rsMetaData.getColumnCount();
    for (int i = 1; i < numberOfColumns + 1; i++) {
        String columnName = rsMetaData.getColumnName(i);
        System.out.print(columnName + "   ");

    }/*from w w  w .j a v a  2s .  c o  m*/
    while (rs.next()) {
        for (int i = 1; i < numberOfColumns + 1; i++) {
            System.out.print(rs.getString(i) + "   ");
        }
        System.out.println();
    }
}

From source file:Main.java

private static void outputResultSet(ResultSet rs) throws Exception {
    ResultSetMetaData rsMetaData = rs.getMetaData();
    int numberOfColumns = rsMetaData.getColumnCount();
    for (int i = 1; i < numberOfColumns + 1; i++) {
        String columnName = rsMetaData.getColumnName(i);
        System.out.print(columnName + "   ");

    }/*from   w w  w.j a v a2 s  .c  o  m*/
    System.out.println();
    System.out.println("----------------------");

    while (rs.next()) {
        for (int i = 1; i < numberOfColumns + 1; i++) {
            System.out.print(rs.getString(i) + "   ");
        }
        System.out.println();
    }

}