日期:2014-05-16  浏览次数:20469 次

Spring jdbctemplate 得到SQL的元数据
	public static void testFind() {
		DataSource dataSource = getDataSource();
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		String sql = "select * from (select * from qpf_data_source) where 1=0";
		List<KeyValue<String, String>> list = jdbcTemplate.query(sql,
				new ResultSetExtractor<List<KeyValue<String, String>>>() {

					@Override
					public List<KeyValue<String, String>> extractData(
							ResultSet rs) throws SQLException,
							DataAccessException {
						ResultSetMetaData metaData = rs.getMetaData();
						int count = metaData.getColumnCount();
						List<KeyValue<String, String>> l = new ArrayList<KeyValue<String, String>>();
						for (int i = 0; i < count; i++) {
							String fieldName = metaData.getColumnName(i + 1).toLowerCase();
							int type = metaData.getColumnType(i + 1);
							String typeName = metaData.getColumnTypeName(i + 1).toLowerCase();
							System.out.println(fieldName + " : " + type + " : "
									+ typeName);
							l.add(new KeyValue<String, String>(fieldName,
									typeName));
						}
						return l;
					}
				});

		for (KeyValue<String, String> obj : list) {
			System.out.println(obj);
		}
	}

?输出:

?

id : 2 : number

data_source_name : 12 : varchar2

data_source_text : 12 : varchar2

driver_class_name : 12 : varchar2

url : 12 : varchar2

user_name : 12 : varchar2

user_password : 12 : varchar2

validation_query : 12 : varchar2

create_time : 93 : date

KeyValue [key=id, value=number]

KeyValue [key=data_source_name, value=varchar2]

KeyValue [key=data_source_text, value=varchar2]

KeyValue [key=driver_class_name, value=varchar2]

KeyValue [key=url, value=varchar2]

KeyValue [key=user_name, value=varchar2]

KeyValue [key=user_password, value=varchar2]

KeyValue [key=validation_query, value=varchar2]

KeyValue [key=create_time, value=date]