1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| import java.lang.reflect.InvocationTargetException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
public class DAO {
public void update(String sql, Object ... args) { Connection conn = null; PreparedStatement psmt = null; try { conn = JDBCTools.getConnection(); psmt = conn.prepareStatement(sql); for(int i = 0;i < args.length;i ++) { psmt.setObject(i+1, args[i]); } psmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); }finally { JDBCTools.release(null, psmt, conn); } }
public <T> T get(Class<T> clazz, String sql, Object ... args) { List<T> list = getForList(clazz, sql, args); if(list.size()>0) { return list.get(0); } return null; }
public <T> List<T> getForList(Class<T> clazz, String sql, Object ... args){ List<T> list = new ArrayList<>(); Connection conn = null; PreparedStatement psmt = null; ResultSet rs = null; try { conn = JDBCTools.getConnection(); psmt = conn.prepareStatement(sql); for(int i = 0;i < args.length;i ++) { psmt.setObject(i+1, args[i]); } rs = psmt.executeQuery(); List<Map<String, Object>> values = handleResultSetToMapList(rs); list = transferMapListToBeanList(clazz, values); } catch (Exception e) { e.printStackTrace(); } return list; }
@SuppressWarnings("unchecked") public <E> E getForValue(String sql, Object ... args) { Connection conn = null; PreparedStatement psmt = null; ResultSet rs = null; try { conn = JDBCTools.getConnection(); psmt = conn.prepareStatement(sql); for(int i = 0;i < args.length;i ++) { psmt.setObject(i+1, args[i]); } rs = psmt.executeQuery(); if(rs.next()) { return (E)rs.getObject(1); } } catch(Exception ex) { ex.printStackTrace(); }finally { JDBCTools.release(rs, psmt, conn); } return null; }
private List<String> getColumnLabels(ResultSet rs) throws SQLException{ List<String> labels = new ArrayList<>(); ResultSetMetaData rsmd = rs.getMetaData(); for(int i = 0;i < rsmd.getColumnCount();i++) { labels.add(rsmd.getColumnLabel(i+1)); } return labels; }
private List<Map<String, Object>> handleResultSetToMapList(ResultSet rs) throws SQLException{ List<String> labels = this.getColumnLabels(rs); List<Map<String,Object>> values = new ArrayList<>(); Map<String,Object> map = null; while(rs.next()){ map = new HashMap<>(); for(String key :labels) { Object value = rs.getObject(key); map.put(key, value); } values.add(map); } return values; }
private <T> List<T> transferMapListToBeanList(Class<T> clazz, List<Map<String, Object>> values) throws InstantiationException, IllegalAccessException, InvocationTargetException { List<T> list = new ArrayList<>(); T bean = null; if(values.size()>0) { for(Map<String,Object> m:values) { bean = clazz.newInstance(); for(Map.Entry<String, Object> entry:m.entrySet()) { String propertyName = entry.getKey(); Object propertyValue = entry.getValue(); BeanUtils.setProperty(bean, propertyName, propertyValue); } list.add(bean); } } return list; } }
|