();
Connection conn = DBUtil.getConnection();
ResultSet tableRs = null; // 存庫元數(shù)據(jù)
ResultSet colRs = null;//存儲表元數(shù)據(jù)
try {
DatabaseMetaData dbmd = conn.getMetaData();//返回連接到的數(shù)據(jù)庫此 Connection 對象所連接的數(shù)據(jù)庫的元數(shù)據(jù)
//獲取所有表
List tableNameList = new ArrayList();
tableRs = dbmd.getTables(null, "%", "%", new String[]{"TABLE"}); //所有表
while (tableRs.next()) {
String tableName = tableRs.getString("TABLE_NAME");//表名
tableNameList.add(tableName);
}
List fieldList = null;//存儲每一個表的所有字段
Table table = null;
for (String name : tableNameList ) {
table = new Table();
//獲取表的字段
colRs = dbmd.getColumns(null, "%", name, "%");//當(dāng)前表的字段
Field field = null;
fieldList = new ArrayList();
while (colRs.next()) {
field = new Field();
String columnName = colRs.getString("COLUMN_NAME");//名稱
String columnType = colRs.getString("TYPE_NAME");//類型
int datasize = colRs.getInt("COLUMN_SIZE");//字段長度
int digits = colRs.getInt("DECIMAL_DIGITS");
int nullable = colRs.getInt("NULLABLE");//返回1就表示可以是Null,而0就表示Not Null
field.setColumnName(columnName);
field.setTypeName(columnType);
field.setColumnSize(datasize);
field.setDecimal_digits(digits);
field.setNullable(nullable);
fieldList.add(field);
}
table.setTableName(name);
table.setField(fieldList);
tableList.add(table);
}
} catch (SQLException ex) {
Logger.getLogger(ExportOracleTable.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(colRs != null) {
try {
colRs.close();
} catch (SQLException ex) {
Logger.getLogger(ExportOracleTable.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(tableRs != null) {
try {
tableRs.close();
} catch (SQLException ex) {
Logger.getLogger(ExportOracleTable.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(conn != null) {
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(ExportOracleTable.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return tableList;
}
分享名稱:Oracle讀取庫中表結(jié)構(gòu)
URL網(wǎng)址:http://weahome.cn/article/jhsssd.html
-
在線咨詢
微信咨詢
電話咨詢
-
028-86922220(工作日)
18980820575(7×24)
-
提交需求
-
返回頂部