首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

Spring jdbctemplate 失去SQL的元数据

2013-12-20 
Spring jdbctemplate 得到SQL的元数据public static void testFind() {DataSource dataSource getDataSo

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>>>() {@Overridepublic 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]

热点排行