
使用数据库接口工具ODBC以及查询工具Microsoft
Query读取数据库的表结构。结果:
使用Microsoft
Query读取数据库的表结构,对于非计算机专业人员而言,清除了数据挖掘的入门障碍。结论:Microsoft
Query读取数据库表结构,使用简单,功能强大,是数据挖掘的好工具。
【关键词】
Microsoft
Query;
表结构
Microsoft
Query
是Microsoft
Office的一个自带程序,用于检索外部的数据源,其用途非常广泛[1]。Microsoft
Query支持的外部数据源有:Microsoft
SQL
Server
OLAP
Services7.0以上、Microsoft
Access
2000以上、dBASE、Microsoft
FoxPro、Microsoft
Excel、Oracle、Paradox、SQL
Server以及文本文件数据库。读取其它数据库,可安装其专用的ODBC接口。在医院统计工作中,经常需要自定义的查询数据[2],这就需要对数据库的表结构有一个全面的了解。而作为普通用户,终端上一般只安装了Microsoft
Office而没有安装数据库开发工具。因此,Microsoft
Query就为普通用户提供了读取数据库的一条捷径。以下给出Microsoft
Query读取数据库表结构的方法。
1
创建ODBC
本例中,服务器名sgcwserver,数据库名bagl。
打开Windows控制面板/管理工具/ODBC,进入“ODBC数据源管理器”界面。进入“创建到SQL
SERVER的新数据源”界面,输入数据源名称sgcwserver_bagl,描述:病案库数据源。服务器:sgcwserver。
按[下一步],选择“使用用户输入登录ID和密码的SQL
Server验证”。录入登录ID及密码。本例登录ID为sa,密码为空。按[下一步],选择所需连接的数据库bagl。按[下一步],选择“测试数据源”,测试成功后确定。
2
从数据库中读取表结构
运行Microsoft
Query程序,程序位于Office安装目录中,如"C:\Program
Files\Microsoft
Office\Office10\MSQRY32.EXE",运行后显示:
在菜单中选择‘文件/执行SQL’,录入以下SQL语句:
SELECT
"表名"=case
when
a.colorder=1
then
d.name
else
''
end,
"表说明"=case
when
a.colorder=1
then
isnull(f.value,'')
else
''
end,
"字段号"=a.colorder,"字段名"=a.name,
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) {
this.dataSource = 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 java.util.List<DsClientColumnInfo>clientTableInfos = new ArrayList<DsClientColumnInfo>()
16 try {
17 connection = this.jdbcTemplate.getDataSource().getConnection()
18 //获得列的信息
19 resultSet = connection.getMetaData().getColumns(null, null, tableName, null)
20 while (resultSet.next()) {
21 //获得字段名称
22 String name = resultSet.getString("COLUMN_NAME")
23 //获得字段类型名称
24 String type = resultSet.getString("TYPE_NAME")
25 //获得字段大小
26 int size = resultSet.getInt("COLUMN_SIZE")
27 //获得字段备注
28 String remark = resultSet.getString("REMARKS")
29 DsClientColumnInfo info = new DsClientColumnInfo(null, null, null, name, remark, size, type, "false")
30 clientTableInfos.add(info)
31 }
32
33 //获得主键的信息
34 resultSet = connection.getMetaData().getPrimaryKeys(null, null, tableName)
35 while(resultSet.next()){
36 String primaryKey = resultSet.getString("COLUMN_NAME")
37 //设置是否为主键
38 for (DsClientColumnInfo dsClientColumnInfo : clientTableInfos) {
39 if(primaryKey != null &&primaryKey.equals(dsClientColumnInfo.getClientColumnCode()))
40 dsClientColumnInfo.setIsParmaryKey("true")
41 else
42 dsClientColumnInfo.setIsParmaryKey("false")
43 }
44 }
45
46 //获得外键信息
47 resultSet = connection.getMetaData().getImportedKeys(null, null, tableName)
48 while(resultSet.next()){
49 String exportedKey = resultSet.getString("FKCOLUMN_NAME")
50 //设置是否是外键
51 for (DsClientColumnInfo dsClientColumnInfo : clientTableInfos) {
52 if(exportedKey != null &&exportedKey.equals(dsClientColumnInfo.getClientColumnCode()))
53 dsClientColumnInfo.setIsImportedKey("true")
54 else
55 dsClientColumnInfo.setIsImportedKey("false")
56 }
57 }
58
59
60 } catch (Exception e) {
61 e.printStackTrace()
62 throw new RuntimeException("获取字段信息的时候失败,请将问题反映到维护人员。" + e.getMessage(), e)
63 } finally{
64 if(resultSet != null)
65 try {
66 resultSet.close()
67 } catch (SQLException e) {
68 e.printStackTrace()
69throw new DataAccessFailureException("关闭结果集resultSet失败。",e)
70 }finally{
71 if(connection != null)
72 try {
73 connection.close()
74 } catch (SQLException e) {
75 e.printStackTrace()
76throw new DataAccessFailureException("关闭连接connection失败。",e)
77 }
78 }
79 }
80
81 Set set = new HashSet()
82 set.addAll(clientTableInfos)
83 clientTableInfos.clear()
84 clientTableInfos.addAll(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 = this.jdbcTemplate.getDataSource().getConnection()
17 String databaseName = SynXmlAnalysis.getElementValueByName(DATABASE_NAME)
18 resultSet = connection.getMetaData().getTables(null, databaseName, null, types)
19 while(resultSet.next()){
20 String tableName = resultSet.getString("TABLE_NAME")
21 String remark = resultSet.getString("REMARKS")
22 map.put(tableName, remark)
23 }
24 } catch (SQLException e) {
25 e.printStackTrace()
26 throw new DataAccessFailureException(e)
27 }catch (Exception e) {
28 e.printStackTrace()
29 }finally{
30 if(resultSet != null)
31 try {
32 resultSet.close()
33 } catch (SQLException e) {
34 e.printStackTrace()
35throw new DataAccessFailureException("关闭结果集resultSet失败。",e)
36 }finally{
37 if(connection != null)
38 try {
39 connection.close()
40 } catch (SQLException e) {
41 e.printStackTrace()
42throw new DataAccessFailureException("关闭连接connection失败。",e)
43 }
44 }
45
46 }
47 return map
48 }
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)