Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class TableData {
- List<String> headerNames;
- List<List<Object>> data;
- public final int COLS;
- public final int ROWS;
- public static TableData emptyTable() {
- return new TableData();
- }
- public TableData(List<String> headerNames, List<List<Object>> data) {
- super();
- this.headerNames = headerNames;
- this.data = data;
- COLS = headerNames.size();
- ROWS = data.size();
- }
- /**
- * Returns value at j-th column at i-th row, with indices starting
- * at 0, and typecast the result with the class provided as argument
- *
- * throws <code>ClassCastException</code> if the given class is not
- * applicable to the object returned
- **/
- public <T> T getValue(int i, int j, Class<T> cls) throws ClassCastException{
- return cls.cast(data.get(i).get(j));
- }
- @Override
- public String toString() {
- return "TableData [headerNames=" + headerNames + ", data=" + data + "]";
- }
- private TableData() {
- ROWS = 0;
- COLS = 0;
- }
- }
- /**
- * Returns <code>TableData</code> object for a given <code> tableName</code>
- *<p>
- *
- * @param tableName the table name in DB, for which we need to retreive data.
- * @param args Optional argument which is comma separated column names if we want
- * to load only these columns, not every column in memory
- * @return a <code>TableData</code> object that contains the data produced
- * by the given query along with headerNames; Returns an Empty table in case of an exception.
- */
- public TableData getCompleteTable(String tablename, String... args) {
- String query = "select * from " + tablename;
- if (args.length > 0) {
- query = "select " + args[1] + " from " + tablename;
- }
- try (Statement stmt = createReadStatement(); ResultSet rs = stmt.executeQuery(query);) {
- ResultSetMetaData meta = rs.getMetaData();
- ArrayList<String> headerNames = new ArrayList<>();
- List<List<Object>> data = new ArrayList<>();
- for (int i = 1; i <= meta.getColumnCount(); i++) {
- headerNames.add(meta.getColumnName(i));
- }
- while (rs.next()) {
- ArrayList<Object> cols = new ArrayList<>(meta.getColumnCount());
- for (int i = 1; i <= meta.getColumnCount(); i++) {
- // TODO: How stable it is, for atleast JDBC mysql driver?
- cols.add(rs.getObject(i));
- }
- data.add(cols);
- }
- rs.close();
- stmt.close();
- return new TableData(headerNames, data);
- } catch (Exception e) {
- // TODO: Throw exception upstream or return empty table to mark error?
- e.printStackTrace();
- return TableData.emptyTable();
- }
- }
- TableData t = dlDb.getCompleteTable("users");
- if(t.ROWS==0){
- //TODO: Either Table is empty or we were unable to retrieve, handle accordignly
- }
- for (int i = 0; i < t.ROWS; i++) {
- //As the developer, we know the correct datatype of our columns.
- // We can also have a mapping from column index to class.
- nameToId.put(t.getValue(i, 1, String.class), t.getValue(i, 0, Integer.class));
- }
- public static class TableData {
- // List of Maps: each item in the List corresponds to one DB row
- // The Map keys are column names, map values are, well, values
- List<Map<String, Object>> data = null;
- public static TableData emptyTable() {
- return new TableData();
- }
- public TableData(List<Map<String, Object>> data) {
- this.data = data;
- }
- /**
- * Returns value of given column at i-th row, with indices starting at 0, and
- * typecast the result with the class provided as argument
- *
- * throws <code>ClassCastException</code> if the given class is not applicable
- * to the object returned
- **/
- public <T> T getValue(int i, String column, Class<T> cls) throws ClassCastException {
- return cls.cast(data.get(i).get(column));
- }
- private TableData() {
- }
- }
- import org.apache.commons.dbutils.*;
- import org.apache.commons.dbutils.handlers.*;
- public TableData getCompleteTable(String tablename, String... args) {
- String query = "select * from " + tablename;
- if (args.length > 0) {
- query = "select " + args[1] + " from " + tablename;
- }
- try (Connection conn = ...) {
- MapListHandler handler = new MapListHandler();
- QueryRunner run = new QueryRunner();
- List<Map<String, Object>> result = run.query(conn, query, handler);
- return new TableData(data);
- } catch (Exception e) {
- // TODO: Throw exception upstream or return empty table to mark error?
- e.printStackTrace();
- return TableData.emptyTable();
- }
- }
Add Comment
Please, Sign In to add comment