Example usage for java.sql ResultSet getMetaData

List of usage examples for java.sql ResultSet getMetaData

Introduction

In this page you can find the example usage for java.sql ResultSet getMetaData.

Prototype

ResultSetMetaData getMetaData() throws SQLException;

Source Link

Document

Retrieves the number, types and properties of this ResultSet object's columns.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    Connection conn = null;/*from  www .  j  a v  a 2 s  .c  o  m*/
    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: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);/* w  ww. j a  v  a  2 s .  c  o 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.jav  a  2  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: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"));
    }//from 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 {

    Class.forName(DRIVER);/*  w ww .  ja va 2s . c om*/
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    Statement statement = connection.createStatement();
    String query = "SELECT stock_id, name, price FROM stocks";
    ResultSet resultSet = statement.executeQuery(query);

    ResultSetMetaData metadata = resultSet.getMetaData();
    int precision = metadata.getPrecision(3);
    int scale = metadata.getScale(3);

    System.out.println("Precision: " + precision);
    System.out.println("Scale    : " + scale);
}

From source file:Main.java

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

    DatabaseMetaData mtdt = conn.getMetaData();

    // list catalogs managed by this dbms
    System.out.println(mtdt.getCatalogTerm());

    ResultSet rs = mtdt.getCatalogs();

    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 . j ava2  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 {
    Class.forName(DRIVER);/*from w  w w  .ja  va2  s  . com*/
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    Statement statement = connection.createStatement();
    String query = "SELECT a.id, a.username, a.country_id, b.country_name " + "FROM users a "
            + "LEFT JOIN countries b ON a.country_id = b.id";
    ResultSet resultSet = statement.executeQuery(query);

    ResultSetMetaData metadata = resultSet.getMetaData();

    String tableName = metadata.getTableName(1);
    System.out.println("Table name of column 'id' = " + tableName);

    tableName = metadata.getTableName(4);
    System.out.println("Table name of column 'country name' = " + tableName);
}

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 v a 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 = 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();//from  w  ww  .  java  2 s.co m
    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();//from   www  .  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);

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