简单代码导出ORACLE数字字典

前话:老系统,居然连数据字典都没,全是SQL。无奈从数据库导出数字字典,很简单的代码,就是读oracel系统表。


把输出保存到文件,然后excel导入,用tab分隔就行了。

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Test {

	public static void main(String[] args) {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection con = DriverManager.getConnection(
					"jdbc:oracle:thin:@192.168.10.11:1521:hzdev", "xiao",
					"xiao");
			Statement stmt = con.createStatement();
			StringBuffer strbuf = new StringBuffer();
			strbuf.append("SELECT A.*,B.comments");
			strbuf.append(" FROM all_tab_columns A,DBA_COL_COMMENTS B");
			strbuf.append(" WHERE A.owner=B.owner");
			strbuf.append(" AND A.table_name=B.table_name");
			strbuf.append(" AND A.COLUMN_NAME=B.COLUMN_NAME");
			// owner是指要导出哪个用户的
			strbuf.append(" AND A.owner='XIAO'");
			strbuf.append(" ORDER BY A.TABLE_NAME");
			ResultSet rs = stmt.executeQuery(strbuf.toString());
			String tb = "";
			String tempTb=null;
			StringBuffer sb = new StringBuffer();

			int count=0;
			
			while (rs.next()) {
				tb = rs.getString("TABLE_NAME");
				if (!tb.equals(tempTb)) {
					count++;
					sb.append("\n");
					System.out.println(sb.toString());
					sb = new StringBuffer();
					sb.append("表名:"+tb);
					tempTb=tb;
					sb.append("\n字段名称\t");
					sb.append("字段类型\t");
					sb.append("长度\t");
					sb.append("数字长度\t");
					sb.append("小数位数\t");
					sb.append("是否要空\t");
					sb.append("字段说明\t");
				}
				
				sb.append("\n"+rs.getString("COLUMN_NAME") + "\t");
				sb.append(rs.getString("DATA_TYPE") + "\t");
				sb.append(rs.getString("DATA_LENGTH") + "\t");
				sb.append(rs.getString("DATA_PRECISION") + "\t");
				sb.append(rs.getString("DATA_SCALE") + "\t");
				sb.append(rs.getString("NULLABLE") + "\t");
				sb.append(rs.getString("COMMENTS") + "\t");
			} 
			System.out.println("表的个数:"+count);
			rs.close();
			stmt.close();
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 

你可能感兴趣的:(oracle)