Guest User

Untitled

a guest
Apr 11th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. package com.common;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.ResultSetMetaData;
  6. import java.sql.Timestamp;
  7. import java.sql.Types;
  8. import java.util.HashMap;
  9. import java.util.LinkedHashMap;
  10. import java.util.Map;
  11.  
  12. import com.common.Query;
  13.  
  14. public class GetColumnValuesFromDifferentTypes {
  15. @SuppressWarnings("unused")
  16. public static void printColumns(String query){
  17. ABIDAO abidao = new ABIDAO();
  18. int numberOfColumns=0,columnType = 0;
  19. String columnValue = "", columnName = "";
  20. Map<String, String> headerMap = new LinkedHashMap<String, String>();
  21. Connection connection = abidao.getConnection();
  22. if (connection != null) {
  23. try {
  24. headerMap = getHeader(query);
  25. System.out.println("====================================================================");
  26. System.out.println("Header Map : "+headerMap);
  27. System.out.println("====================================================================");
  28. PreparedStatement reportTablePS = connection.prepareStatement(query);
  29. ResultSet reportTable_rst = reportTablePS.executeQuery();
  30. ResultSetMetaData reportTable_rsmd = reportTable_rst.getMetaData();
  31. numberOfColumns = reportTable_rsmd.getColumnCount();
  32.  
  33. int i =0;
  34. while (reportTable_rst.next()) {
  35. for (int columnIterator = 1; columnIterator <= numberOfColumns; columnIterator++) {
  36. columnValue = null;
  37. columnName = reportTable_rsmd.getColumnName(columnIterator);
  38. columnType = reportTable_rsmd.getColumnType(columnIterator);
  39.  
  40. if(columnType == Types.CHAR || columnType == Types.VARCHAR || columnType == Types.LONGVARCHAR)
  41. {
  42. columnValue = reportTable_rst.getString(columnName);
  43. }else if(columnType == Types.INTEGER || columnType == Types.BIGINT || columnType == Types.SMALLINT || columnType == Types.NUMERIC)
  44. {
  45. long templong = reportTable_rst.getLong(columnName);
  46. if(!reportTable_rst.wasNull())
  47. {
  48. columnValue = Long.toString(templong);
  49. }
  50. }else if(columnType == Types.DECIMAL || columnType == Types.DOUBLE || columnType == Types.FLOAT || columnType == Types.REAL)
  51. {
  52. double tempDouble1 = reportTable_rst.getDouble(columnName);
  53. if(!reportTable_rst.wasNull())
  54. {
  55. columnValue = Double.toString(tempDouble1);
  56. }
  57. } else if(columnType == Types.TIME || columnType == Types.TIMESTAMP || columnType == Types.DATE)
  58. {
  59. Timestamp sqlTimeStamp = reportTable_rst.getTimestamp(columnName);
  60. if(!reportTable_rst.wasNull())
  61. {
  62. columnValue = sqlTimeStamp.toString();
  63. }
  64. }
  65. System.out.println("columnValue : "+columnValue);
  66. System.out.println(",");
  67.  
  68. }
  69. System.out.println("====================================================================");
  70. }
  71. } catch (Exception ex) {
  72. ex.printStackTrace();
  73. }finally {
  74. try {
  75. abidao.closeConnection(connection, null, null);
  76. } catch (Exception ex) {
  77. ex.printStackTrace();
  78. }
  79. }
  80. }
  81. }
  82. @SuppressWarnings({ "unused" })
  83. public static Map<String, String> getHeader(String query) {
  84. ABIDAO abidao = new ABIDAO();
  85. String columnName="";
  86. int numberOfColumns=0,rowCount=1;
  87. Map<String, String> headerNamesMap = new LinkedHashMap<String, String>();
  88. Connection connection = abidao.getConnection();
  89. if (connection != null) {
  90. try {
  91. PreparedStatement reportTablePS = connection.prepareStatement(query);
  92. ResultSet reportTable_rst = reportTablePS.executeQuery();
  93. ResultSetMetaData rsmd = reportTable_rst.getMetaData();
  94. numberOfColumns = rsmd.getColumnCount();
  95. headerNamesMap.put("SNO","SNO");
  96. while (reportTable_rst.next()) {
  97. for (int i = 1; i <= numberOfColumns; i++) {
  98. columnName = rsmd.getColumnName(i).toUpperCase();
  99. if(!headerNamesMap.containsKey(columnName)){
  100. headerNamesMap.put(columnName, columnName);
  101. }
  102. }
  103. rowCount++;
  104. }
  105.  
  106. } catch (Exception ex) {
  107. ex.printStackTrace();
  108. }finally {
  109. try {
  110. abidao.closeConnection(connection, null, null);
  111. } catch (Exception ex) {
  112. ex.printStackTrace();
  113. }
  114. }
  115. }
  116. return headerNamesMap;
  117. }
  118. @SuppressWarnings("rawtypes")
  119. public static void main(String[] args) {
  120. String query = "";
  121. Map dynamicQueryMap = new HashMap();
  122. Query queryObj = new Query();
  123.  
  124. try {
  125. dynamicQueryMap = queryObj.getComplexReportQuery();
  126. if(dynamicQueryMap.containsKey("query")) query = dynamicQueryMap.get("query").toString();
  127. } catch (Exception ex) {
  128. ex.printStackTrace();
  129. }
  130. printColumns(query);
  131.  
  132. }
  133. }
  134.  
  135. select
  136. pi.po_number,poi.unit_cost,pi.placed_date CreateDate,
  137. case when isnull(pi.date_closed) then pi.scheduled_arrival_date
  138. else pi.date_closed end as ReceviedDate,
  139. poi.order_quantity,poi.roll_number,p.product_sku product_name,
  140. pc.product_category_name,poi.rec_vendor_quantity,pv.vendor_name,p.et_conversion_unit_quantity,pi.note
  141. from
  142. purchase_order as pi,
  143. purchase_order_inventory as poi,
  144. product_vendors as pv,
  145. products AS p,
  146. product_categories AS pc
  147. where
  148. pi.purchase_order_id=poi.purchase_order_id and
  149. pc.product_category_id=p.product_category_id and
  150. poi.product_id = p.product_id and
  151. poi.product_category_id=pc.product_category_id and
  152. pi.vendor_id=pv.product_vendor_id and
  153. ( ( pi.date_closed >= '2012-01-01' and pi.date_closed <='2012-09-05 23:59:59' )
  154. or ( pi.scheduled_arrival_date >= '2012-01-01' and pi.scheduled_arrival_date <='2012-09-05 23:59:59') ) and
  155. pi.po_type=0
  156. and pi.status_id = 0 and poi.transaction_type = 0
  157. order by pi.po_number ;
  158.  
  159. Statement stmt = ...;
  160. ...
  161. stmt.close();
  162. stmt = ...
Advertisement
Add Comment
Please, Sign In to add comment