(学习记录)
代码中Table类与Field类请参照:http://meijia.blog.51cto.com/8684191/1563874
可参考api调整相关参数。
(同样注意格式)
1. 方法如下
public List<Table> export() {
List<Table> tableList = new ArrayList<Table>();
Connection conn = DBUtil.getConnection();
ResultSet tableRs = null; // 存库元数据
ResultSet colRs = null;//存储表元数据
try {
DatabaseMetaData dbmd = conn.getMetaData();//返回连接到的数据库此 Connection 对象所连接的数据库的元数据
//获取所有表
List<String> tableNameList = new ArrayList<String>();
tableRs = dbmd.getTables(null, "%", "%", new String[]{"TABLE"}); //所有表
while (tableRs.next()) {
String tableName = tableRs.getString("TABLE_NAME");//表名
tableNameList.add(tableName);
}
List<Field> fieldList = null;//存储每一个表的所有字段
Table table = null;
for (String name : tableNameList ) {
table = new Table();
//获取表的字段
colRs = dbmd.getColumns(null, "%", name, "%");//当前表的字段
Field field = null;
fieldList = new ArrayList<Field>();
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;
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。