1 21 package net.mlw.vlh.adapter.jdbc.util; 22 23 import java.lang.reflect.InvocationTargetException ; 24 import java.sql.ResultSet ; 25 import java.sql.ResultSetMetaData ; 26 import java.sql.SQLException ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 37 public class ResultSetMapGenerator 38 { 39 40 43 private static final Log LOGGER = LogFactory.getLog(ResultSetMapGenerator.class); 44 45 private ResultSet result; 46 47 private String [] names; 48 49 public ResultSetMapGenerator(ResultSet result, boolean useName, boolean lowerCase) throws SQLException 50 { 51 this.result = result; 52 ResultSetMetaData metadata = result.getMetaData(); 53 54 int columnCount = metadata.getColumnCount(); 55 names = new String [columnCount]; 56 for (int i = 0; i < columnCount; i++) 57 { 58 names[i] = (useName) ? metadata.getColumnName(i + 1) : metadata.getColumnLabel(i + 1); 59 if (names[i] == null || names[i].length() == 0) 60 { 61 names[i] = (useName) ? metadata.getColumnLabel(i + 1) : metadata.getColumnName(i + 1); 62 } 63 64 if (lowerCase) 65 { 66 names[i] = names[i].toLowerCase(); 67 } 68 } 69 LOGGER.debug(names); 70 } 71 72 75 public Map generateMap() throws SQLException , NoSuchMethodException , InvocationTargetException , IllegalAccessException 76 { 77 Map map = new HashMap (); 78 for (int i = 0; i < names.length; i++) 79 { 80 Object value = result.getObject(i + 1); 81 map.put(names[i], value); 82 if (LOGGER.isDebugEnabled()) 83 { 84 LOGGER.debug(names[i] + " - " + value); 85 } 86 } 87 88 return map; 89 } 90 91 94 public ResultSet getResultSet() 95 { 96 return result; 97 } 98 99 108 protected Class loadClass(String className) throws SQLException 109 { 110 try 111 { 112 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 113 if (cl == null) 114 { 115 cl = this.getClass().getClassLoader(); 116 } 117 return (cl.loadClass(className)); 118 } 119 catch (Exception e) 120 { 121 throw new SQLException ("Cannot load column class '" + className + "': " + e); 122 } 123 } 124 125 public static class Bean 126 { 127 128 } 129 } | Popular Tags |