Example usage for java.sql ResultSetMetaData getColumnCount

List of usage examples for java.sql ResultSetMetaData getColumnCount

Introduction

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

Prototype

int getColumnCount() throws SQLException;

Source Link

Document

Returns the number of columns in this ResultSet object.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "root";

    Class.forName(driver);//from  w ww  .ja v  a2  s.  co  m
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass);

    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM employee6");
    ResultSetMetaData md = rs.getMetaData();
    int col = md.getColumnCount();
    System.out.println("Number of Column : " + col);
    for (int i = 1; i <= col; i++) {
        String col_name = md.getColumnName(i);
        System.out.println(col_name);
    }
}

From source file:PrintResultSet.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Inventory");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("select * from employee");
    ResultSetMetaData md = rs.getMetaData();

    int nColumns = md.getColumnCount();
    for (int i = 1; i <= nColumns; i++) {

        System.out.print(md.getColumnLabel(i) + ((i == nColumns) ? "\n" : "\t"));
    }/*w  w  w.  j  av  a2  s. c o  m*/
    while (rs.next()) {
        for (int i = 1; i <= nColumns; i++) {
            System.out.print(rs.getString(i) + ((i == nColumns) ? "\n" : "\t"));
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    ResultSet rs = DriverManager.getConnection("jdbc:odbc:employee_xls").createStatement()
            .executeQuery("Select * from [Sheet1$]");

    ResultSetMetaData rsmd = rs.getMetaData();
    int numberOfColumns = rsmd.getColumnCount();

    System.out.println("No of cols " + numberOfColumns);
    while (rs.next()) {
        for (int i = 1; i <= numberOfColumns; i++) {
            String columnValue = rs.getString(i);
            System.out.println(columnValue);
        }//  ww w .  j a  v  a 2  s  . c o m
    }
    rs.close();
}

From source file:Main.java

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    Class.forName(DRIVER);//from  w  w w  .j  a v a  2 s.  co m
    Connection con = DriverManager.getConnection(CONNECTION_URL, USERNAME, PASSWORD);
    ResultSet rs = con.createStatement().executeQuery(QUERY);
    if (rs != null) {
        System.out.println("Column Type\t\t Column Name");

        ResultSetMetaData rsmd = rs.getMetaData();
        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
            System.out.println(rsmd.getColumnTypeName(i) + "\t\t\t" + rsmd.getColumnName(i));
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("DESCRIBE myTable");
    ResultSetMetaData md = rs.getMetaData();
    int col = md.getColumnCount();
    for (int i = 1; i <= col; i++) {
        String col_name = md.getColumnName(i);
        System.out.println(col_name);
    }/*from   w w  w .  j  a  va 2 s . c o  m*/
    DatabaseMetaData dbm = con.getMetaData();
    ResultSet rs1 = dbm.getColumns(null, "%", "myTable", "%");
    while (rs1.next()) {
        String col_name = rs1.getString("COLUMN_NAME");
        String data_type = rs1.getString("TYPE_NAME");
        int data_size = rs1.getInt("COLUMN_SIZE");
        int nullable = rs1.getInt("NULLABLE");
        System.out.println(col_name + " " + data_type + "(" + data_size + ")");
        if (nullable == 1) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Connection conn = null;/*from w ww.j  a va2s.c  om*/
    Statement s = null;
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    conn = DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};" + "Server=.\\SQLEXPRESS;"
            + "Trusted_Connection=yes;" + "Database=myDb");
    s = conn.createStatement();
    s.executeQuery("SELECT * FROM dbo.SalesSummary WHERE 0 = 1");
    ResultSet rs = s.getResultSet();
    ResultSetMetaData rsmd = rs.getMetaData();
    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
        System.out.println(String.format("-- Column %d --", i));
        System.out.println(String.format("Column name: %s", rsmd.getColumnName(i)));
        System.out.println(String.format("Database-specific type name: %s", rsmd.getColumnTypeName(i)));
        System.out.println(String.format("Column size (DisplaySize): %d", rsmd.getColumnDisplaySize(i)));
        System.out.println(String.format("java.sql.Type of column: %d", rsmd.getColumnType(i)));
        System.out.println();
    }
    s.close();
    conn.close();
}

From source file:PrintIndexedResultSet.java

public static void main(String args[]) throws Exception {
    String query = "SELECT STATE, COUNT(STATE) FROM MEMBER_PROFILES GROUP BY STATE";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Members");
    Statement stmt = con.createStatement();
    stmt.executeUpdate("CREATE INDEX STATE_INDEX ON MEMBER_PROFILES(STATE)");

    java.util.Date startTime = new java.util.Date();

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

    int nColumns = md.getColumnCount();
    for (int i = 1; i <= nColumns; i++) {
        System.out.print(md.getColumnLabel(i) + ((i == nColumns) ? "\n" : "\t"));
    }// w w w.ja va  2  s.  c om

    while (rs.next()) {
        for (int i = 1; i <= nColumns; i++) {
            System.out.print(rs.getString(i) + ((i == nColumns) ? "\n" : "\t"));
        }
    }
    java.util.Date endTime = new java.util.Date();
    long elapsedTime = endTime.getTime() - startTime.getTime();
    System.out.println("Elapsed time: " + elapsedTime);

    stmt.executeUpdate("DROP INDEX MEMBER_PROFILES.STATE_INDEX");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();

    DatabaseMetaData mtdt = conn.getMetaData();

    ResultSet rs = mtdt.getTables(conn.getCatalog(), "%", "%", null);

    ResultSetMetaData rsmd = rs.getMetaData();
    int numCols = rsmd.getColumnCount();
    for (int i = 1; i <= numCols; i++) {
        if (i > 1)
            System.out.print(", ");
        System.out.print(rsmd.getColumnLabel(i));
    }//from www. ja v  a  2 s  .c  o  m
    System.out.println("");
    while (rs.next()) {
        for (int i = 1; i <= numCols; i++) {
            if (i > 1)
                System.out.print(", ");
            System.out.print(rs.getString(i));
        }
        System.out.println("");
    }
    conn.close();
}

From source file:Main.java

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

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

    ResultSetMetaData rsMetaData = rs.getMetaData();

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

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();
    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.  jav  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);

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