导出oracle数据库所有表结构到excel中

来源:互联网 发布:人脸识别算法 开源 编辑:程序博客网 时间:2024/04/30 08:20

这个是基本的连接数据库代码(注意要加入连接oracle数据库的jar包):

导入的数据库就是String sc="jdbc:oracle:thin:@192.168.1.223:1521:test";对应的test数据库,所有的表结构都可以导出来。(需要修改为自己对应的ip和端口和数据库名称,上面的是我的地址和数据库)

Java代码  收藏代码
  1. import java.sql.Connection;  
  2.   
  3. import java.sql.DriverManager;  
  4.   
  5. import java.sql.ResultSet;  
  6.   
  7. import java.sql.SQLException;  
  8.   
  9. import java.sql.Statement;  
  10.   
  11.    
  12.   
  13. public class ConnectionOracle {  
  14.   
  15.    
  16.   
  17. /** 
  18.  
  19. * @param args 
  20.  
  21. */  
  22.   
  23. String sd="oracle.jdbc.driver.OracleDriver";  
  24.   
  25. // String sc="jdbc:oracle:thin:@192.168.1.170:1521:cst";  
  26.   
  27. String sc="jdbc:oracle:thin:@192.168.1.223:1521:test";  
  28.   
  29. String userName = "adminss";  
  30.   
  31. String password = "123456";  
  32.   
  33. // String sd="com.mysql.jdbc.Driver";  
  34.   
  35. // String sc="jdbc:mysql://localhost:3306/payManagerDB?useUnicode=true&characterEncoding=utf8";  
  36.   
  37. Connection con=null;  
  38.   
  39. Statement stmt=null;  
  40.   
  41. ResultSet rs=null;  
  42.   
  43.   
  44. public ConnectionOracle()  
  45.   
  46.   {  
  47.   
  48.   try  
  49.   
  50.   {  
  51.   
  52.   Class.forName(sd);  
  53.   
  54.   }  
  55.   
  56.   catch(Exception e)  
  57.   
  58.   {  
  59.   
  60.   System.err.println(e.getMessage());  
  61.   
  62.   }  
  63.   
  64.   }  
  65.   
  66. public static void main(String[] args) {  
  67.   
  68. // TODO Auto-generated method stub  
  69.   
  70.   
  71. }  
  72.   
  73. public ResultSet executeQuery(String sql) throws SQLException  
  74.   
  75.   {  
  76.   
  77.   con=DriverManager.getConnection(sc,userName,password);  
  78.   
  79.   Statement stmt=con.createStatement();  
  80.   
  81.   rs=stmt.executeQuery(sql);  
  82.   
  83.   return rs;  
  84.   
  85.   }  
  86.   
  87. public void executeUpdate(String sql) throws SQLException  
  88.   
  89.   {  
  90.   
  91.   con=DriverManager.getConnection(sc,userName,password);  
  92.   
  93.   Statement stmt=con.createStatement();  
  94.   
  95.   stmt.executeUpdate(sql);  
  96.   
  97.   }  
  98.   
  99. public void close() throws SQLException  
  100.   
  101.   {  
  102.   
  103. if(rs!=null)  
  104.   
  105. rs.close();  
  106.   
  107. if(stmt!=null)  
  108.   
  109. stmt.close();  
  110.   
  111. if(con!=null)  
  112.   
  113. con.close();  
  114.   
  115.   }  
  116.   
  117. }  
  118.   
  119.    

 

下面的代码读取数据库中所有表以及结构到excel中(如果出现问题,首先看是不是你已经打开了这个excel,要先关闭):

 

Java代码  收藏代码
  1. import java.io.File;  
  2.   
  3. import java.io.FileOutputStream;  
  4.   
  5. import java.sql.ResultSet;  
  6.   
  7. import java.sql.SQLException;  
  8.   
  9. import java.util.ArrayList;  
  10.   
  11. import java.util.Iterator;  
  12.   
  13. import java.util.List;  
  14.   
  15.    
  16.   
  17. import org.apache.poi.hssf.usermodel.HSSFCell;  
  18.   
  19. import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
  20.   
  21. import org.apache.poi.hssf.usermodel.HSSFFont;  
  22.   
  23. import org.apache.poi.hssf.usermodel.HSSFRichTextString;  
  24.   
  25. import org.apache.poi.hssf.usermodel.HSSFRow;  
  26.   
  27. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  28.   
  29. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  30.   
  31. import org.apache.poi.hssf.util.Region;  
  32.   
  33.    
  34.   
  35. /** 
  36.  
  37.  * 将oracle中的数据表结构导入到excel中保存 
  38.  
  39.  * @class DataToExcel 
  40.  
  41.  * @description  
  42.  
  43.  * @author 李智慧 
  44.  
  45.  * @copyRight copyright(c) 2011 广东南航易网通电子商务有限公司,Rights Reserved 
  46.  
  47.  * @time Dec 27, 2011 10:02:08 AM 
  48.  
  49.  */  
  50.   
  51. public class DataToExcel {  
  52.   
  53. public static void main(String[] args) {  
  54.   
  55. String result = "";  
  56.   
  57. List listAll = new ArrayList();  
  58.   
  59. System.out.println("正在读取数据库中所有的表");  
  60.   
  61. try {  
  62.   
  63. List tableList = getTableList();  
  64.   
  65. System.out.println("数据库表读取完成");  
  66.   
  67. for(int i=0;i<tableList.size();i++){  
  68.   
  69. String[] strings = (String[]) tableList.get(i);  
  70.   
  71. String tableName = strings[0].toString();  
  72.   
  73. List list = new ArrayList();  
  74.   
  75. list.add(tableName);  
  76.   
  77. list.add(getStructOfTable(tableName));  
  78.   
  79. System.out.println("正在生成表"+tableName+"的结构");  
  80.   
  81. listAll.add(list);  
  82.   
  83. }  
  84.   
  85. result = TableStructInfoToExcel(listAll,"D:");  
  86.   
  87. System.out.println("数据库中表结构导入已完成");  
  88.   
  89. catch (Exception e) {  
  90.   
  91. // TODO Auto-generated catch block  
  92.   
  93. e.printStackTrace();  
  94.   
  95. File file = new File(e.getMessage().toString());  
  96.   
  97. if(file.exists()){  
  98.   
  99. file.delete();  
  100.   
  101. }  
  102.   
  103. }  
  104.   
  105. System.out.println(result);  
  106.   
  107. //showView(list);   
  108.   
  109. }  
  110.   
  111.   
  112. /** 
  113.  
  114. * 获取数据库中所有的表 
  115.  
  116. * @return 
  117.  
  118. */  
  119.   
  120. public static List getTableList(){  
  121.   
  122. String sql = "select object_name From user_objects Where object_type='TABLE'";  
  123.   
  124. return getResult(sql,1);  
  125.   
  126. }  
  127.   
  128.   
  129. /** 
  130.  
  131. * 根据表明 
  132.  
  133. * @param tableName 
  134.  
  135. * @return 
  136.  
  137. */  
  138.   
  139. public static List getStructOfTable(String tableName){  
  140.   
  141. String sql = "SELECT u.column_name,u.data_type,u.data_length,u.data_precision,u.data_Scale,u.nullable,u.data_default,c.comments FROM user_tab_columns u,user_col_comments c"+  
  142.   
  143. " WHERE u.table_name='"+tableName+"' and u.table_name=c.table_name and c.column_name=u.column_name";  
  144.   
  145. return getResult(sql,8);  
  146.   
  147. }  
  148.   
  149.   
  150. /** 
  151.  
  152. * 获取结果的公用类 
  153.  
  154. * @param sql 
  155.  
  156. * @param length 
  157.  
  158. * @return 
  159.  
  160. */  
  161.   
  162. public static List getResult(String sql,int length){  
  163.   
  164. List list = new ArrayList();  
  165.   
  166. ResultSet rs=null;  
  167.   
  168. ConnectionOracle c=new ConnectionOracle();  
  169.   
  170. try {  
  171.   
  172. rs=c.executeQuery(sql);  
  173.   
  174. while(rs.next()){  
  175.   
  176. String[] string = new String[length];  
  177.   
  178. for(int i=1;i<length+1;i++){  
  179.   
  180. string[i-1] = rs.getString(i);  
  181.   
  182. }  
  183.   
  184. list.add(string);  
  185.   
  186. }  
  187.   
  188. c.close();  
  189.   
  190. catch (SQLException e) {  
  191.   
  192. e.printStackTrace();  
  193.   
  194. }  
  195.   
  196. return list;  
  197.   
  198. }  
  199.   
  200.   
  201. /** 
  202.  
  203. * 输出对应list中的数据 
  204.  
  205. * @param list 
  206.  
  207. */  
  208.   
  209. public static void showView(List list){  
  210.   
  211. for (Iterator iterator = list.iterator(); iterator.hasNext();) {  
  212.   
  213. String[] name = (String[]) iterator.next();  
  214.   
  215. for (int i = 0; i < name.length; i++) {  
  216.   
  217. System.out.println(name[i]);  
  218.   
  219. }  
  220.   
  221. }  
  222.   
  223. }  
  224.   
  225.   
  226. /** 
  227.  
  228. * 将数据导入到excel中 
  229.  
  230. * @param list 
  231.  
  232. * @param tableName 
  233.  
  234. * @param path 
  235.  
  236. * @return 
  237.  
  238. * @throws Exception 
  239.  
  240. */  
  241.   
  242. public static String TableStructInfoToExcel(List list,String path) throws Exception {  
  243.   
  244. String FileName="";  
  245.   
  246. FileOutputStream fos = null;  
  247.   
  248. HSSFRow row = null;  
  249.   
  250. HSSFCell cell = null;  
  251.   
  252. HSSFCellStyle style = null;  
  253.   
  254. HSSFFont font = null;  
  255.   
  256. int currentRowNum = 0;  
  257.   
  258. String[] tableFiled = {"column_name","data_type","data_length","data_precision","data_Scale","nullable","data_default","comments"};  
  259.   
  260. try{  
  261.   
  262. FileName = path +"\\"+"CSN数据库中表结构.xls";  
  263.   
  264. fos = new FileOutputStream(FileName);  
  265.   
  266. //创建新的sheet并设置名称  
  267.   
  268. HSSFWorkbook wb = new HSSFWorkbook();  
  269.   
  270. HSSFSheet s = wb.createSheet();  
  271.   
  272. wb.setSheetName(0"CSN数据库表结构");  
  273.   
  274. style = wb.createCellStyle();  
  275.   
  276. font = wb.createFont();  
  277.   
  278.   
  279. for(int z=0;z<list.size();z++){  
  280.   
  281. List listBean = (List) list.get(z);  
  282.   
  283. //新建一行,再在行上面新建一列  
  284.   
  285. row = s.createRow(currentRowNum);  
  286.   
  287. int pad = currentRowNum;  
  288.   
  289. currentRowNum++;  
  290.   
  291. //设置样式  
  292.   
  293. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);   //字体加粗  
  294.   
  295. style.setFont(font);  
  296.   
  297. style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中  
  298.   
  299. style.setFillForegroundColor((short13);// 设置背景色  
  300.   
  301. style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
  302.   
  303. style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框  
  304.   
  305. style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框  
  306.   
  307. style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框  
  308.   
  309. style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框  
  310.   
  311. for(int i=0;i<tableFiled.length;i++){  
  312.   
  313. cell = row.createCell((short) i);  
  314.   
  315. cell.setCellValue("");  
  316.   
  317. cell.setCellStyle(style);  
  318.   
  319. }  
  320.   
  321. row.getCell((short0).setCellValue("数据库表"+listBean.get(0).toString()+"的结构");  
  322.   
  323. //创建第二行  
  324.   
  325. row = s.createRow(currentRowNum);  
  326.   
  327. currentRowNum++;  
  328.   
  329. for(int i=0;i<tableFiled.length;i++){  
  330.   
  331. //创建多列并设置每一列的值和宽度  
  332.   
  333. cell = row.createCell((short) i);  
  334.   
  335. cell.setCellValue(new HSSFRichTextString(tableFiled[i]));  
  336.   
  337. s.setColumnWidth((short)i,(short)5000);  
  338.   
  339. }  
  340.   
  341. List list2 = (List) listBean.get(1);  
  342.   
  343. for(int i=0;i<list2.size();i++){  
  344.   
  345. row = s.createRow(currentRowNum);  
  346.   
  347. currentRowNum++;  
  348.   
  349. String[] strings = (String[]) list2.get(i);  
  350.   
  351. for(int j=0;j<strings.length;j++){  
  352.   
  353. cell = row.createCell((short) j);  
  354.   
  355. cell.setCellValue(new HSSFRichTextString(strings[j]));  
  356.   
  357. }  
  358.   
  359. }  
  360.   
  361. //合并单元格  
  362.   
  363. s.addMergedRegion(new Region(pad,(short)0,pad,(short)(tableFiled.length-1)));  
  364.   
  365. currentRowNum ++;  
  366.   
  367. }  
  368.   
  369.   
  370. wb.write(fos);  
  371.   
  372. fos.close();  
  373.   
  374. }catch (Exception e) {  
  375.   
  376. e.printStackTrace();  
  377.   
  378. fos.close();  
  379.   
  380. throw new Exception(FileName);  
  381.   
  382. }  
  383.   
  384. return FileName;  
  385.   
  386. }  
  387.   
  388. }  
 
最后总结一下:
关键性的代码:
一:读取数据库中的所有表
Java代码  收藏代码
  1. select object_name From user_objects Where object_type='TABLE';  
 
二:读取对应表中的结构,下面测试是用CITYNB(这里没有加主键和外键信息,以后加上,有兴趣的朋友可以自己完成,弄好以后,希望给我借鉴一下,呵呵,相互学习嘛,如果还有问题,请联系我:lizhihui19871127@163.com)
Java代码  收藏代码
  1. SELECT u.column_name,u.data_type,u.data_length,u.data_precision,u.data_Scale,u.nullable,u.data_default,c.comments FROM user_tab_columns u,user_col_comments c  
  2.   WHERE u.table_name='CITYNB' and u.table_name=c.table_name and c.column_name=u.column_name;  
 
转载:http://lizhihui19871127.iteye.com/blog/1327999
0 0
原创粉丝点击