Example usage for java.sql ResultSetMetaData getColumnTypeName

List of usage examples for java.sql ResultSetMetaData getColumnTypeName

Introduction

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

Prototype

String getColumnTypeName(int column) throws SQLException;

Source Link

Document

Retrieves the designated column's database-specific type name.

Usage

From source file:Main.java

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    Class.forName(DRIVER);/*from w w w .  j  a  v a2  s .  c o 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 args[]) throws Exception {
    Connection conn = null;/* www .  j a v  a  2s . 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:RSMetaDataMethods.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;//from  w w  w.j  a  v a2  s. com
    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("select * from COFFEES");
        ResultSetMetaData rsmd = rs.getMetaData();

        int numberOfColumns = rsmd.getColumnCount();
        for (int i = 1; i <= numberOfColumns; i++) {
            String colName = rsmd.getColumnName(i);
            String tableName = rsmd.getTableName(i);
            String name = rsmd.getColumnTypeName(i);
            boolean caseSen = rsmd.isCaseSensitive(i);
            boolean writable = rsmd.isWritable(i);
            System.out.println("Information for column " + colName);
            System.out.println("    Column is in table " + tableName);
            System.out.println("    DBMS name for type is " + name);
            System.out.println("    Is case sensitive:  " + caseSen);
            System.out.println("    Is possibly writable:  " + writable);
            System.out.println("");
        }

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

        stmt.close();
        con.close();

    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    }
}

From source file:ResultSetMetaDataExample.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();

    boolean notDone = true;
    String sqlStr = null;/*from w w w.  ja v  a  2s  . c o m*/
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    while (notDone) {
        sqlStr = br.readLine();
        if (sqlStr.startsWith("SELECT") || sqlStr.startsWith("select")) {
            ResultSet rs = stmt.executeQuery(sqlStr);
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            for (int x = 1; x <= columnCount; x++) {
                String columnName = rsmd.getColumnName(x);
                System.out.print(columnName);
            }
            while (rs.next()) {
                for (int x = 1; x <= columnCount; x++) {
                    if (rsmd.getColumnTypeName(x).compareTo("CURRENCY") == 0)
                        System.out.print("$");
                    String resultStr = rs.getString(x);
                    System.out.print(resultStr + "\t");
                }
            }
        } else if (sqlStr.startsWith("exit"))
            notDone = false;
    }
    stmt.close();
    con.close();
}

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();/*w  w w.  ja  v  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);

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

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

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();//w  ww  .ja va 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);

    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: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 w w . java2s.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:PrintColumns.java

public static void printColTypes(ResultSetMetaData rsmd) throws SQLException {
    int columns = rsmd.getColumnCount();
    for (int i = 1; i <= columns; i++) {
        int jdbcType = rsmd.getColumnType(i);
        String name = rsmd.getColumnTypeName(i);
        System.out.print("Column " + i + " is JDBC type " + jdbcType);
        System.out.println(", which the DBMS calls " + name);
    }//w ww .  ja v a  2  s.  com
}

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  w  w.j a  v a2  s .c  om
}

From source file:Student.java

  public static void checkData() throws Exception {
  Class.forName("org.hsqldb.jdbcDriver");
  Connection conn = DriverManager.getConnection("jdbc:hsqldb:data/tutorial", "sa", "");
  Statement st = conn.createStatement();

  ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" });
  while (mrs.next()) {
    String tableName = mrs.getString(3);
    System.out.println("\n\n\n\nTable Name: "+ tableName);

    ResultSet rs = st.executeQuery("select * from " + tableName);
    ResultSetMetaData metadata = rs.getMetaData();
    while (rs.next()) {
      System.out.println(" Row:");
      for (int i = 0; i < metadata.getColumnCount(); i++) {
        System.out.println("    Column Name: "+ metadata.getColumnLabel(i + 1)+ ",  ");
        System.out.println("    Column Type: "+ metadata.getColumnTypeName(i + 1)+ ":  ");
        Object value = rs.getObject(i + 1);
        System.out.println("    Column Value: "+value+"\n");
      }/*from w w w . ja  v a 2s  . c o m*/
    }
  }
}