/* * 通用查詢、更新升級版 * */ 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); } } }