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: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  va  2 s .c  om
}

From source file:Main.java

public static byte[] getBytes(ResultSet rs) throws Exception {
    byte[] res = {};

    int col = rs.getMetaData().getColumnCount();
    for (int i = 1; i <= col; i++) {
        System.out.println(i + ":" + rs.getMetaData().getColumnTypeName(i));
        byte[] obj = {};//rs.getBytes(i);
        byte[] n = new byte[res.length + obj.length];
        System.arraycopy(res, 0, n, 0, res.length);
        System.arraycopy(obj, 0, n, res.length, obj.length);
        res = n;/*  www.  j av  a 2 s .c  om*/
    }

    return res;
}

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) + "   ");
        }
        System.out.println();
    }

}

From source file:Main.java

private static void addChildren(Writer writer, ResultSet rs) throws SQLException, IOException {
    ResultSetMetaData metaData = rs.getMetaData();
    int nbColumns = metaData.getColumnCount();
    StringBuffer buffer = new StringBuffer();
    while (rs.next()) {
        buffer.setLength(0);/*from w w w  .j a v a  2 s . c  om*/
        buffer.append("<" + metaData.getTableName(1) + ">");
        for (int i = 1; i <= nbColumns; i++) {
            buffer.append("<" + metaData.getColumnName(i) + ">");
            buffer.append(rs.getString(i));
            buffer.append("</" + metaData.getColumnName(i) + ">");
        }
        buffer.append("</" + metaData.getTableName(1) + ">");
        writer.write(buffer.toString());
    }
}

From source file:GetColumnNamesFromResultSet_MySQL.java

public static void getColumnNames(ResultSet rs) throws SQLException {
    if (rs == null) {
        return;// w  w  w .jav a  2  s  .c o  m
    }
    ResultSetMetaData rsMetaData = rs.getMetaData();
    int numberOfColumns = rsMetaData.getColumnCount();

    // get the column names; column indexes start from 1
    for (int i = 1; i < numberOfColumns + 1; i++) {
        String columnName = rsMetaData.getColumnName(i);
        // Get the name of the column's table name
        String tableName = rsMetaData.getTableName(i);
        System.out.println("column name=" + columnName + " table=" + tableName + "");
    }
}

From source file:Main.java

public static Element appendResultSetToNode(Element root, String rowTag, ResultSet rs) throws SQLException {
    Document doc = root.getOwnerDocument();

    ResultSetMetaData meta = rs.getMetaData();
    int columnCount = meta.getColumnCount();
    int rowCount = 0;
    while (rs.next()) {
        Element rowElement = doc.createElement(rowTag);
        rowElement.setAttribute("row", "" + rowCount);
        for (int i = 1; i <= columnCount; i++) {
            rowElement.setAttribute(meta.getColumnName(i), rs.getString(i));
        }//from  ww w . j a v a2  s.  com
        rowCount++;
        root.appendChild(rowElement);
    }

    return root;
}

From source file:modelo.ApiManager.java

public static List resultSetToArrayList(ResultSet rs) throws SQLException {
    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();
    ArrayList list = new ArrayList();

    while (rs.next()) {
        HashMap row = new HashMap(columns);
        for (int i = 1; i <= columns; ++i) {
            row.put(md.getColumnName(i), rs.getObject(i));
        }//from  w  w w  .j  a v a 2  s .  co m
        list.add(row);
    }

    return list;
}

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 ww  .  j  a  v  a 2  s.  c  o  m
    }
  }
}

From source file:com.github.mgramin.liwide_rest.util.Convertor.java

public static JSONArray convertToJSON(ResultSet resultSet) throws Exception {
    JSONArray jsonArray = new JSONArray();
    while (resultSet.next()) {
        int total_rows = resultSet.getMetaData().getColumnCount();
        JSONObject obj = new JSONObject();
        for (int i = 0; i < total_rows; i++) {
            obj.put(resultSet.getMetaData().getColumnLabel(i + 1).toLowerCase(), resultSet.getObject(i + 1));
        }//from w w  w .j  a va2 s.c om
        jsonArray.put(obj);
    }
    return jsonArray;
}

From source file:GetColumnNamesFromResultSet_Oracle.java

public static void getColumnNames(ResultSet rs) throws SQLException {
    if (rs == null) {
        return;// ww w  .j a v a  2s .c  om
    }
    // get result set meta data
    ResultSetMetaData rsMetaData = rs.getMetaData();
    int numberOfColumns = rsMetaData.getColumnCount();

    // get the column names; column indexes start from 1
    for (int i = 1; i < numberOfColumns + 1; i++) {
        String columnName = rsMetaData.getColumnName(i);
        // Get the name of the column's table name
        String tableName = rsMetaData.getTableName(i);
        System.out.println("column name=" + columnName + " table=" + tableName + "");
    }
}