();
Connection conn = DBUtil.getConnection();
ResultSet tableRs = null; // 存库元数据
ResultSet colRs = null;//存储表元数据
try {
DatabaseMetaData dbmd = conn.getMetaData();//返回连接到的数据库此 Connection 对象所连接的数据库的元数据
//获取所有表
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, "%");//当前表的字段
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读取库中表结构
本文路径:http://cdkjz.cn/article/jhsssd.html