当前位置:首页 » 操作系统 » 获取数据库表结构

获取数据库表结构

发布时间: 2022-12-23 09:44:21

‘壹’ 如何通过JDBC取得数据库表结构信息

JDBC中有个ResultSetMetaData类就是用来获取数据表结构信息的。
...
ResultSet rs = ....
ResultSetMetaData rsmd = rs.getMetaData();

//ResultSetMetaData中有如下方法:

getColumnCount(); 返回 ResultSet 中的列数。
getColumnName(int); 返回列序号为 int 的列名。
getColumnLabel(int); 返回此列暗含的标签。
isCurrency(int); 如果此列包含带有货币单位的一个数字,则返回 true。
isReadOnly(int); 如果此列为只读,则返回 true。
isAutoIncrement(int); 如果此列自动递增,则返回 true。这类列通常为键,而且始终是只读的。
getColumnType(int); 返回此列的 sql 数据类型。这些数据类型包括

‘贰’ oracle 查看表结构,表里的数据

1、首先,我们打开PLSQL工具连接到需要进行数据比对的ORACLE数据库。

‘叁’ 如何导出一个数据库的所有表结构

1.完整的导出整个数据库表结构即dmp文件方法:
Tools-->Export Tables ,选择Oracle Export 默认选项即可,在Output file选择一个输出目标
点击Export,即可导出表结构

2.导出表中数据:
Tools-->Export Tables :选择SQL Inserts 遇到大字段(clob,blob)会出问题导不出来,在此可以
选择需要导出的表,在Output file中选择导出的目标,点击Export即可导出,在此有几个选项
可以去掉所有的可选项直接导出

3.导出数据库中所有的或是所选表的数据库建表脚本
Tools-->Export User Objects去掉下面三个include *可选项,选择Single file)在Output file
中选择输出目标,点击Export即可

‘肆’ 如何导出mysql数据库表结构

在命令行下mysql的数据导出有个很好用命令mysqlmp,它的参数有一大把,可以这样查看: 
mysqlmp 

最常用的: 
mysqlmp -uroot -pmysql databasefoo table1 table2 > foo.sql 

这样就可以将数据库databasefoo的表table1,table2以sql形式导入foo.sql中,其中-uroot参数表示访问数据库的用户名是root,如果有密码还需要加上-p参数 

C:\Users\jack> mysqlmp -uroot -pmysql sva_rec date_drv > e:\date_drv.sql 

mysql的数据导入也是相当便捷的,如: 
mysql -uroot databasefoo < foo.sql 

这样就可以将foo.sql的数据全部导入数据库databasefoo 

     1.导出整个数据库 

mysqlmp -u用户名 -p密码  数据库名 > 导出的文件名 
C:\Users\jack> mysqlmp -uroot -pmysql sva_rec  > e:\sva_rec.sql 

2.导出一个表,包括表结构和数据 

mysqlmp -u用户名 -p 密码  数据库名 表名> 导出的文件名 
C:\Users\jack> mysqlmp -uroot -pmysql sva_rec date_rec_drv> e:\date_rec_drv.sql 

3.导出一个数据库结构 
C:\Users\jack> mysqlmp -uroot -pmysql -d sva_rec > e:\sva_rec.sql 

     4.导出一个表,只有表结构 
mysqlmp -u用户名 -p 密码 -d数据库名  表名> 导出的文件名 
C:\Users\jack> mysqlmp -uroot -pmysql -d sva_rec date_rec_drv> e:\date_rec_drv.sql 

5.导入数据库 

常用source 命令 
进入mysql数据库控制台, 
如mysql -u root -p 
mysql>use 数据库 
然后使用source命令,后面参数为脚本文件(如这里用到的.sql) 
mysql>source d:wcnc_db.sql

‘伍’ 如何获得数据库中表的结构

list 表名
得到每一个表的详细信息字段名,字段属性(类型,文字长度,备注)
describe 表名
具体的表名

‘陆’ 怎么获得数据库表结构

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();
69 throw new DataAccessFailureException("关闭结果集resultSet失败。",e);
70 }finally{
71 if(connection != null)
72 try {
73 connection.close();
74 } catch (SQLException e) {
75 e.printStackTrace();
76 throw 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();
35 throw new DataAccessFailureException("关闭结果集resultSet失败。",e);
36 }finally{
37 if(connection != null)
38 try {
39 connection.close();
40 } catch (SQLException e) {
41 e.printStackTrace();
42 throw new DataAccessFailureException("关闭连接connection失败。",e);
43 }
44 }
45
46 }
47 return map;
48 }

‘柒’ 如何从mysql数据库中获取一个表的表结构

列出表(列)结构:
mysql< DESCRIBE tableName;
mysql< DESCRIBE tableName columnName;
mysql< DESC tableName;
mysql< SHOW COLUMNS FROM tableName;
% mysqlshow dbName tableName!

热点内容
nds服务器ip地址 发布:2025-05-11 12:43:32 浏览:869
舒听澜卓禹安书名叫什么 发布:2025-05-11 12:36:44 浏览:268
java开发web应用 发布:2025-05-11 12:35:51 浏览:696
鲨鱼影视怎么缓存电视 发布:2025-05-11 12:35:48 浏览:549
ios小项目源码 发布:2025-05-11 12:35:47 浏览:756
为什么打开的三菱程序不能编译 发布:2025-05-11 12:16:40 浏览:21
ftp定价是怎么回事 发布:2025-05-11 12:09:18 浏览:334
android敏捷开发 发布:2025-05-11 11:56:49 浏览:80
脚本pon 发布:2025-05-11 11:52:27 浏览:826
ct5推荐哪个配置 发布:2025-05-11 11:47:45 浏览:742