Guest User

Untitled

a guest
Nov 9th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. CREATE TABLE public.states (
  2. state_id INTEGER DEFAULT nextval('states_seq'::regclass) NOT NULL,
  3. state_cd VARCHAR(2) NOT NULL,
  4. name VARCHAR(100) NOT NULL,
  5. tax_pct NUMERIC(10,2) DEFAULT 0.00 NOT NULL,
  6. active CHAR(1) DEFAULT 'Y'::bpchar NOT NULL,
  7. )
  8.  
  9. public class State implements Serializable {
  10.  
  11. private int stateId;
  12. private String stateCode;
  13. private String name;
  14. private BigDecimal taxPct = new BigDecimal(0);
  15. private Date expiryDate;
  16. private String createdBy;
  17. private Date createdOn;
  18. private String active;
  19.  
  20. //getters and setters here
  21. }
  22.  
  23. public class Main {
  24.  
  25. public static void main(String[] args) {
  26. String url = "jdbc:postgresql://gsi-547576.gsiccorp.net:5432/istore-db";
  27. String driver = "org.postgresql.Driver";
  28. String user = "postgres";
  29. String pwd = "postgres";
  30. Connection conn = null;
  31. List<State> states = null;
  32.  
  33. try {
  34. DbUtils.loadDriver(driver);
  35. conn = DriverManager.getConnection(url, user, pwd);
  36.  
  37. states = (List<State>) new QueryRunner().query(conn, "select * from states a where a.active='Y'", new BeanListHandler(State.class);
  38.  
  39. System.out.println("states:: " + states);
  40.  
  41. } catch (SQLException ex) {
  42. ex.printStackTrace();
  43. } finally {
  44. DbUtils.closeQuietly(conn);
  45. }
  46. }
  47.  
  48. }
  49.  
  50. public class StateBeanProcessor extends BeanProcessor {
  51.  
  52. @Override
  53. protected int[] mapColumnsToProperties(ResultSetMetaData rsmd, PropertyDescriptor[] props) throws SQLException {
  54. int[] mapping = super.mapColumnsToProperties(rsmd, props);
  55. /*Map database columns to fields in the order in which they appear
  56. 1st column in the DB will be mapped to 1st field in the Java
  57. class and so on.. */
  58. for(int i=0;i<mapping.length;++i) {
  59. mapping[i]=i;
  60. }
  61. }
  62. }
  63.  
  64. states = (List<State>) new QueryRunner().query(conn, "select * from states", new BeanListHandler(State.class,new BasicRowProcessor(new StateBeanProcessor())));
  65.  
  66. public static Map<String,String> mapColumnsToProperties = new HashMap<>();
  67. static {
  68. mapColumnsToProperties.put("measure_id","id");
  69. mapColumnsToProperties.put( "place_number","placeNum");
  70. mapColumnsToProperties.put( "measure_name","measureName");
  71. mapColumnsToProperties.put("item_list","itemList");
  72. mapColumnsToProperties.put("start_time","startTime");
  73. mapColumnsToProperties.put("end_time","endTime");
  74. mapColumnsToProperties.put("measure_time","measureTime");
  75.  
  76. }
  77. public static BeanProcessor beanProcessor = new BeanProcessor(mapColumnsToProperties);
  78. public static RowProcessor rowProcessor = new BasicRowProcessor( beanProcessor);
  79. ResultSetHandler<List<SafeScheme>> resultSetHandler = new BeanListHandler<SafeScheme>(SafeScheme.class,rowProcessor);
  80. safeSchemes = queryRunner.query(findListSQL, resultSetHandler);
  81.  
  82. public String changeColumnName(String columnName){
  83. if(columnName == null){
  84. return null;
  85. }
  86. if(columnName.contains("_")){
  87. char[] cs = columnName.toCharArray();
  88. int flag = -1;
  89. for(int index=0;index<columnName.toCharArray().length;index++){
  90. if(cs[index] == '_'){
  91. flag = index;
  92. break;
  93. }
  94. }
  95. columnName = columnName.substring(0, flag) + columnName.substring(flag+1,flag+2).toUpperCase() + columnName.substring(flag+2);
  96. return changeColumnName(columnName);
  97. }else{
  98. return columnName;
  99. }
  100. }
  101.  
  102. protected int[] mapColumnsToProperties(ResultSetMetaData rsmd,
  103. PropertyDescriptor[] props) throws SQLException {
  104.  
  105. int cols = rsmd.getColumnCount();
  106. int[] columnToProperty = new int[cols + 1];
  107. Arrays.fill(columnToProperty, PROPERTY_NOT_FOUND);
  108.  
  109. for (int col = 1; col <= cols; col++) {
  110. String columnName = rsmd.getColumnLabel(col);
  111. if (null == columnName || 0 == columnName.length()) {
  112. columnName = rsmd.getColumnName(col);
  113. }
  114. String propertyName = columnToPropertyOverrides.get(columnName);
  115. if (propertyName == null) {
  116. propertyName = changeColumnName(columnName);//add here
  117. }
  118. for (int i = 0; i < props.length; i++) {
  119.  
  120. if (propertyName.equalsIgnoreCase(props[i].getName())) {
  121. columnToProperty[col] = i;
  122. break;
  123. }
  124. }
  125. }
  126.  
  127. return columnToProperty;
  128. }
  129.  
  130. // TODO initialize
  131. QueryRunner queryRunner = null;
  132.  
  133. ResultSetHandler<List<State>> resultSetHandler =
  134. new BeanListHandler<State>(State.class, new BasicRowProcessor(new GenerousBeanProcessor()));
  135.  
  136. // best practice is mentioning only required columns in the query
  137. final List<State> states = queryRunner.query("select * from states a where a.active='Y'", resultSetHandler);
  138.  
  139. for (State state : states) {
  140. System.out.println(state.getStateId());
  141. }
Add Comment
Please, Sign In to add comment