當前位置:首頁 » 操作系統 » 獲取資料庫表結構

獲取資料庫表結構

發布時間: 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!

熱點內容
sql插入兩張表 發布:2025-05-11 09:55:33 瀏覽:757
日本編程語言 發布:2025-05-11 09:53:52 瀏覽:843
手機店設置的初始密碼一般是多少 發布:2025-05-11 09:33:15 瀏覽:402
昂科威選擇哪個配置 發布:2025-05-11 09:25:50 瀏覽:36
怎麼解決安卓視頻全屏卡頓 發布:2025-05-11 09:14:55 瀏覽:726
匯編從編譯到執行 發布:2025-05-11 09:09:04 瀏覽:258
安卓系統低版本如何升級 發布:2025-05-11 09:04:44 瀏覽:252
認證類型加密演算法 發布:2025-05-11 08:58:35 瀏覽:562
android停靠 發布:2025-05-11 08:42:23 瀏覽:647
超時代加密 發布:2025-05-11 08:41:29 瀏覽:781