/* * 通用查询、更新升级版 * */ public class BaseDao2 { static { try { Class.forName(ConfigUtil.getValue("driver")); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public Connection getConn(){ try { return DriverManager.getConnection(ConfigUtil.getValue("url"), ConfigUtil.getValue("username"), ConfigUtil.getValue("password")); } catch (SQLException e) { e.printStackTrace(); return null; } } public void close(ResultSet rs, PreparedStatement ps, Connection conn){ try { if(rs != null) { rs.close(); } if(ps != null){ ps.close(); } if(conn != null){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } /* * 返回一条查询记录 * 输入参数Object[] obj中存放sql语句中变量的值 * */ public Object search(String sql, Object[] obj, Class<?> clz){ Connection conn = getConn(); PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(sql); if(obj != null && obj.length>0){ for(int i=0; i<obj.length; i++) { ps.setObject(i + 1, obj[i]); } } rs = ps.executeQuery(); if(!rs.next()){ return null; } /* * 在此处应该要给对象赋值,但遇到了以下问题 * 如何确定列数? * 如何确定列名? * 如何确定查询的是哪个对象? * */ return doResultSet(rs,clz); } catch (SQLException e) { throw new RuntimeException(); }finally { close(null, ps, conn); } } /* * 运用java反射机制,编写通用类,Class<?>表示不知道传入的会是什么类型的数据,因此用?代替 * */ public Object doResultSet(ResultSet rs, Class<?> clz){ Object bean = null; try { bean = clz.newInstance(); //对象实例化 ResultSetMetaData metaData = rs.getMetaData(); //获取元数据 int colCount = metaData.getColumnCount(); //获取列数 for(int i=0; i<colCount; i++){ Object colValue = rs.getObject(i+1); //获取列的值 String colName = metaData.getColumnName(i+1); Field f = clz.getDeclaredField(colName); f.setAccessible(true); //取消某些检测 f.set(bean,colValue); //将从数据库获取的值传递给bean对象 } } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } return bean; } /* * 查询对象为List,即有多条查询记录 * */ public Object searchList(String sql,Object[] obj,Class<?> clz){ PreparedStatement ps = null; ResultSet rs = null; Connection conn = getConn(); try { ps = conn.prepareStatement(sql); if(obj != null && obj.length>0) { for (int i = 0; i < obj.length; i++) { ps.setObject(i + 1, obj[i+1]); } } rs = ps.executeQuery(); if(!rs.next()){ return null; } return doResultSetList(rs, clz); } catch (SQLException e) { e.printStackTrace(); return null; }finally { close(rs,ps,conn); } } public List<Object> doResultSetList(ResultSet rs,Class<?> clz){ List<Object> list = new ArrayList<Object>(); try { while(rs.next()) { Object bean = clz.newInstance(); ResultSetMetaData metaData = rs.getMetaData(); int colCount = metaData.getColumnCount(); for (int i = 0; i < colCount; i++) { String colName = metaData.getColumnName(i + 1); Object colValue = rs.getObject(i + 1); Field f = clz.getDeclaredField(colName); f.setAccessible(true); f.set(bean, colValue); } list.add(bean); } } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } return list; } public int update(String sql, Object[] obj){ Connection conn = getConn(); PreparedStatement ps = null; try { ps = conn.prepareStatement(sql); if(obj != null && obj.length>0) { for (int i = 0; i < obj.length; i++) { ps.setObject(i+1,obj[i]); } } return ps.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(); }finally { close(null,ps,conn); } } }