Guest User

Untitled

a guest
Jan 16th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. public static class TableData {
  2. List<String> headerNames;
  3. List<List<Object>> data;
  4. public final int COLS;
  5. public final int ROWS;
  6.  
  7. public static TableData emptyTable() {
  8. return new TableData();
  9. }
  10.  
  11. public TableData(List<String> headerNames, List<List<Object>> data) {
  12. super();
  13. this.headerNames = headerNames;
  14. this.data = data;
  15. COLS = headerNames.size();
  16. ROWS = data.size();
  17. }
  18.  
  19. /**
  20. * Returns value at j-th column at i-th row, with indices starting
  21. * at 0, and typecast the result with the class provided as argument
  22. *
  23. * throws <code>ClassCastException</code> if the given class is not
  24. * applicable to the object returned
  25. **/
  26. public <T> T getValue(int i, int j, Class<T> cls) throws ClassCastException{
  27. return cls.cast(data.get(i).get(j));
  28. }
  29.  
  30. @Override
  31. public String toString() {
  32. return "TableData [headerNames=" + headerNames + ", data=" + data + "]";
  33. }
  34.  
  35. private TableData() {
  36. ROWS = 0;
  37. COLS = 0;
  38. }
  39. }
  40.  
  41. /**
  42. * Returns <code>TableData</code> object for a given <code> tableName</code>
  43. *<p>
  44. *
  45. * @param tableName the table name in DB, for which we need to retreive data.
  46. * @param args Optional argument which is comma separated column names if we want
  47. * to load only these columns, not every column in memory
  48. * @return a <code>TableData</code> object that contains the data produced
  49. * by the given query along with headerNames; Returns an Empty table in case of an exception.
  50. */
  51. public TableData getCompleteTable(String tablename, String... args) {
  52. String query = "select * from " + tablename;
  53. if (args.length > 0) {
  54. query = "select " + args[1] + " from " + tablename;
  55. }
  56. try (Statement stmt = createReadStatement(); ResultSet rs = stmt.executeQuery(query);) {
  57. ResultSetMetaData meta = rs.getMetaData();
  58. ArrayList<String> headerNames = new ArrayList<>();
  59. List<List<Object>> data = new ArrayList<>();
  60.  
  61. for (int i = 1; i <= meta.getColumnCount(); i++) {
  62. headerNames.add(meta.getColumnName(i));
  63. }
  64.  
  65. while (rs.next()) {
  66. ArrayList<Object> cols = new ArrayList<>(meta.getColumnCount());
  67. for (int i = 1; i <= meta.getColumnCount(); i++) {
  68. // TODO: How stable it is, for atleast JDBC mysql driver?
  69. cols.add(rs.getObject(i));
  70. }
  71. data.add(cols);
  72. }
  73. rs.close();
  74. stmt.close();
  75. return new TableData(headerNames, data);
  76. } catch (Exception e) {
  77. // TODO: Throw exception upstream or return empty table to mark error?
  78. e.printStackTrace();
  79. return TableData.emptyTable();
  80. }
  81. }
  82.  
  83. TableData t = dlDb.getCompleteTable("users");
  84. if(t.ROWS==0){
  85. //TODO: Either Table is empty or we were unable to retrieve, handle accordignly
  86. }
  87. for (int i = 0; i < t.ROWS; i++) {
  88. //As the developer, we know the correct datatype of our columns.
  89. // We can also have a mapping from column index to class.
  90. nameToId.put(t.getValue(i, 1, String.class), t.getValue(i, 0, Integer.class));
  91. }
  92.  
  93. public static class TableData {
  94. // List of Maps: each item in the List corresponds to one DB row
  95. // The Map keys are column names, map values are, well, values
  96. List<Map<String, Object>> data = null;
  97.  
  98. public static TableData emptyTable() {
  99. return new TableData();
  100. }
  101.  
  102. public TableData(List<Map<String, Object>> data) {
  103. this.data = data;
  104. }
  105.  
  106. /**
  107. * Returns value of given column at i-th row, with indices starting at 0, and
  108. * typecast the result with the class provided as argument
  109. *
  110. * throws <code>ClassCastException</code> if the given class is not applicable
  111. * to the object returned
  112. **/
  113. public <T> T getValue(int i, String column, Class<T> cls) throws ClassCastException {
  114. return cls.cast(data.get(i).get(column));
  115. }
  116.  
  117. private TableData() {
  118. }
  119. }
  120.  
  121. import org.apache.commons.dbutils.*;
  122. import org.apache.commons.dbutils.handlers.*;
  123.  
  124. public TableData getCompleteTable(String tablename, String... args) {
  125. String query = "select * from " + tablename;
  126. if (args.length > 0) {
  127. query = "select " + args[1] + " from " + tablename;
  128. }
  129. try (Connection conn = ...) {
  130. MapListHandler handler = new MapListHandler();
  131. QueryRunner run = new QueryRunner();
  132.  
  133. List<Map<String, Object>> result = run.query(conn, query, handler);
  134. return new TableData(data);
  135. } catch (Exception e) {
  136. // TODO: Throw exception upstream or return empty table to mark error?
  137. e.printStackTrace();
  138. return TableData.emptyTable();
  139. }
  140. }
Add Comment
Please, Sign In to add comment