首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

结果集的通用模式-对表的字段名和相应值的封装

2012-12-28 
结果集的通用方式--对表的字段名和相应值的封装public OrderedMap findById(String mainIdField,String id

结果集的通用方式--对表的字段名和相应值的封装

public OrderedMap findById(String mainIdField,String id) {PreparedStatement ps = null;ResultSet rs = null;OrderedMap row = null;String sql = "select * from "+tableName + " where "+mainIdField+"='"+id+"'";try {if (debug)log.debug(sql);ps = connection.prepareStatement(sql);rs = ps.executeQuery();ResultSetMetaData rsmd = rs.getMetaData();int columnCount = rsmd.getColumnCount();if (rs.next()) {row = new LinkedMap();for (int i = 1; i <= columnCount; i++) {String columnName = rsmd.getColumnName(i).toUpperCase();row.put(columnName, rs.getObject(i));}}} catch (Exception e) {throw new RuntimeException(e);} finally {try {if (rs != null)rs.close();if (ps != null)ps.close();} catch (SQLException e1) {throw new RuntimeException(e1);}}return row;}

?

热点排行