
在java中把数据库查询的结果保存到map中:
实现:获得ResultSet rs 时可以转化为ResultSetMetaData对象。
ResultSetMetaData可用于获取关于 ResultSet 对象中列的类型和属性信息的对象。
举例说明如下:
//获得ResultSetMetaData对象
ResultSetMetaData rsmd=rsgetMetaData();
//获得返回此 ResultSet 对象中的列数
int count = rsmdgetColumnCount();
while(rsnext()){
Map map=new HashMap();
for(int i=1;i<count;i++){
//获取指定列的表目录名称
String label=rsmdgetColumnLabel(i);
//以 Java 编程语言中 Object 的形式获取此 ResultSet 对象的当前行中指定列的值
Object object= rsgetObject(i);
//把数据库中的字段名和值对应为一个map对象中的一个键值对
mapput(labeltoLowerCase(), object);
}
把每条对象封装成的map对象放进list中
listadd(map);
}
就此就可以把任意resultet通过这个方法生成list对象。
mysql里面提供了很多方法来获取表结构和表列:如下方法
获得某表所有列的信息:
String sql = select from tname;//tname为某一表名
Connection conn = ;
Statement st = conncreateStatement();
ResultSet rs = strs = stexecuteQuery(sql);
ResultSetMetaData rsmd = rsgetMetaData();
int colcount = rsmdgetColumnCount();//取得全部列数
for(int i=0;i<colcount;i++){
String colname = rsmdgetColumnName(i);//取得全部列名
}
以上为某表字段具体查询,如果是查询表的信息,如在mysql服务器上那样的查询结果的话,可以用一下代码:
ResultSetexecuteQuery("show tables")可以的到所有的表信息。
ResultSetexecuteQuery("describe tname")可以得到表的字段信息。//tname为表名
1,在注入时初始化这两个模板。
/
注入数据源, 该数据源在Spring配置文件中配置
在注入时初始化这两个模板
@param dataSource
Method create author: yanwei
Method create dateTime: 2011-11-2 下午03:43:13
Method update author:
Method update dateTime:
/
@Resource
public void setDataSource(DataSource dataSource) {
thisdataSource = dataSource;
jdbcTemplate = new JdbcTemplate(dataSource);
simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
2,获取表结构信息。
1 /
2 获取表结构信息
3 @param tableName 表名
4 @return
5 @throws Exception
6 Method create author: yanwei
7 Method create dateTime: 2011-12-21 下午01:01:17
8 Method update author:
9 Method update dateTime:
10 /
11 public List<DsClientColumnInfo> getDsTableColumnInfo(String tableName) throws DataAccessFailureException{
12
13 ResultSet resultSet = null;
14 Connection connection = null;
15 javautilList<DsClientColumnInfo> clientTableInfos = new ArrayList<DsClientColumnInfo>();
16 try {
17 connection = thisjdbcTemplategetDataSource()getConnection();
18 //获得列的信息
19 resultSet = connectiongetMetaData()getColumns(null, null, tableName, null);
20 while (resultSetnext()) {
21 //获得字段名称
22 String name = resultSetgetString("COLUMN_NAME");
23 //获得字段类型名称
24 String type = resultSetgetString("TYPE_NAME");
25 //获得字段大小
26 int size = resultSetgetInt("COLUMN_SIZE");
27 //获得字段备注
28 String remark = resultSetgetString("REMARKS");
29 DsClientColumnInfo info = new DsClientColumnInfo(null, null, null, name, remark, size, type, "false");
30 clientTableInfosadd(info);
31 }
32
33 //获得主键的信息
34 resultSet = connectiongetMetaData()getPrimaryKeys(null, null, tableName);
35 while(resultSetnext()){
36 String primaryKey = resultSetgetString("COLUMN_NAME");
37 //设置是否为主键
38 for (DsClientColumnInfo dsClientColumnInfo : clientTableInfos) {
39 if(primaryKey != null && primaryKeyequals(dsClientColumnInfogetClientColumnCode()))
40 dsClientColumnInfosetIsParmaryKey("true");
41 else
42 dsClientColumnInfosetIsParmaryKey("false");
43 }
44 }
45
46 //获得外键信息
47 resultSet = connectiongetMetaData()getImportedKeys(null, null, tableName);
48 while(resultSetnext()){
49 String exportedKey = resultSetgetString("FKCOLUMN_NAME");
50 //设置是否是外键
51 for (DsClientColumnInfo dsClientColumnInfo : clientTableInfos) {
52 if(exportedKey != null && exportedKeyequals(dsClientColumnInfogetClientColumnCode()))
53 dsClientColumnInfosetIsImportedKey("true");
54 else
55 dsClientColumnInfosetIsImportedKey("false");
56 }
57 }
58
59
60 } catch (Exception e) {
61 eprintStackTrace();
62 throw new RuntimeException("获取字段信息的时候失败,请将问题反映到维护人员。" + egetMessage(), e);
63 } finally{
64 if(resultSet != null)
65 try {
66 resultSetclose();
67 } catch (SQLException e) {
68 eprintStackTrace();
69 throw new DataAccessFailureException("关闭结果集resultSet失败。",e);
70 }finally{
71 if(connection != null)
72 try {
73 connectionclose();
74 } catch (SQLException e) {
75 eprintStackTrace();
76 throw new DataAccessFailureException("关闭连接connection失败。",e);
77 }
78 }
79 }
80
81 Set set = new HashSet();
82 setaddAll(clientTableInfos);
83 clientTableInfosclear();
84 clientTableInfosaddAll(set);
85 return clientTableInfos;
86 }
3,获得数据库中所有的表。
1 /
2 获得数据库中所有的表
3 @return
4 Method create author: yanwei
5 Method create dateTime: 2012-1-5 上午11:23:54
6 Method update author:
7 Method update dateTime:
8 @throws SQLException
9 /
10 public Map<String, String> getDatabaseTables() throws DataAccessFailureException{
11 ResultSet resultSet = null;
12 Connection connection = null;
13 Map<String, String> map = new HashMap<String, String>();
14 try {
15 String[] types = {"TABLE"};
16 connection = thisjdbcTemplategetDataSource()getConnection();
17 String databaseName = SynXmlAnalysisgetElementValueByName(DATABASE_NAME);
18 resultSet = connectiongetMetaData()getTables(null, databaseName, null, types);
19 while(resultSetnext()){
20 String tableName = resultSetgetString("TABLE_NAME");
21 String remark = resultSetgetString("REMARKS");
22 mapput(tableName, remark);
23 }
24 } catch (SQLException e) {
25 eprintStackTrace();
26 throw new DataAccessFailureException(e);
27 }catch (Exception e) {
28 eprintStackTrace();
29 }finally{
30 if(resultSet != null)
31 try {
32 resultSetclose();
33 } catch (SQLException e) {
34 eprintStackTrace();
35 throw new DataAccessFailureException("关闭结果集resultSet失败。",e);
36 }finally{
37 if(connection != null)
38 try {
39 connectionclose();
40 } catch (SQLException e) {
41 eprintStackTrace();
42 throw new DataAccessFailureException("关闭连接connection失败。",e);
43 }
44 }
45
46 }
47 return map;
48 }
以上就是关于从数据库中读取表信息然后返回成Map集合对象全部的内容,包括:从数据库中读取表信息然后返回成Map集合对象、java 获取mysql 某个数据库中所有表及表的列的信息、怎么获得数据库表结构等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)