Java SQL ResultSet createTable(ResultSet rs)

Here you can find the source of createTable(ResultSet rs)

Description

create Table

License

Apache License

Declaration

public static TabularData createTable(ResultSet rs) throws Exception 

Method Source Code

//package com.java2s;
/*//  ww w  .  jav  a  2s. co  m
 // Licensed to DynamoBI Corporation (DynamoBI) under one
 // or more contributor license agreements.  See the NOTICE file
 // distributed with this work for additional information
 // regarding copyright ownership.  DynamoBI licenses this file
 // to you under the Apache License, Version 2.0 (the
 // "License"); you may not use this file except in compliance
 // with the License.  You may obtain a copy of the License at

 //   http://www.apache.org/licenses/LICENSE-2.0

 // Unless required by applicable law or agreed to in writing,
 // software distributed under the License is distributed on an
 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 // KIND, either express or implied.  See the License for the
 // specific language governing permissions and limitations
 // under the License.
 */

import java.sql.*;
import java.util.*;
import javax.management.openmbean.*;

public class Main {
    public static TabularData createTable(ResultSet rs) throws Exception {
        ResultSetMetaData rsMetaData = rs.getMetaData();
        int numCols = rsMetaData.getColumnCount();
        String[] headers = new String[numCols];
        OpenType[] allTypes = new OpenType[numCols];
        Vector[] values = new Vector[numCols];
        Object[] allValues = new Object[numCols];

        for (int i = 0; i < numCols; i++) {
            headers[i] = rsMetaData.getColumnName(i + 1);
            allTypes[i] = new ArrayType(1, SimpleType.STRING);
            values[i] = new Vector();
        }

        CompositeType ct = new CompositeType("column and values",
                "column and values", headers, headers, allTypes);
        TabularType tt = new TabularType("column and values",
                "column with values", ct, headers);
        TabularData td = new TabularDataSupport(tt);

        while (rs.next()) {
            for (int i = 0; i < numCols; i++) {
                values[i].add(rs.getString(i + 1));
            }
        }

        for (int i = 0; i < numCols; i++) {
            allValues[i] = (String[]) values[i].toArray(new String[0]);
        }

        CompositeData entry = new CompositeDataSupport(ct, headers,
                allValues);
        td.put(entry);
        return td;
    }
}

Related

  1. consume(ResultSet rs)
  2. consume2(ResultSet rs)
  3. copyInt(ResultSet rs, int sourcePos, PreparedStatement stmt, int destPos)
  4. createLargeResultSetPreparedStatement(Connection conn, String sql)
  5. createLargeResultSetStatement(Connection conn)
  6. createUid(final String[] keys, final ResultSet rs, final String keySeparator)
  7. encodeHeader(ResultSetMetaData metaData)
  8. extractGeneratedId(ResultSet generatedKeys)
  9. extractIntResult(ResultSet rs, ResultSetMetaData rsmd, int index)