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

	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;
	}

 

你可能感兴趣的:(sql)