`
ZhengMingwei
  • 浏览: 135030 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • Jdbc
SQL 
阅读更多
	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;
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics