日期:2014-05-16  浏览次数:20529 次

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

?

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

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

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

 

public class ConnectionOracle {

 

/**

* @param args

*/

String sd="oracle.jdbc.driver.OracleDriver";

// String sc="jdbc:oracle:thin:@192.168.1.170:1521:cst";

String sc="jdbc:oracle:thin:@192.168.1.223:1521:test";

String userName = "adminss";

String password = "123456";

// String sd="com.mysql.jdbc.Driver";

// String sc="jdbc:mysql://localhost:3306/payManagerDB?useUnicode=true&characterEncoding=utf8";

Connection con=null;

Statement stmt=null;

ResultSet rs=null;


public ConnectionOracle()

  {

  try

  {

  Class.forName(sd);

  }

  catch(Exception e)

  {

  System.err.println(e.getMessage());

  }

  }

public static void main(String[] args) {

// TODO Auto-generated method stub


}

public ResultSet executeQuery(String sql) throws SQLException

  {

  con=DriverManager.getConnection(sc,userName,password);

  Statement stmt=con.createStatement();

  rs=stmt.executeQuery(sql);

  return rs;

  }

public void executeUpdate(String sql) throws SQLException

  {

  con=DriverManager.getConnection(sc,userName,password);

  Statement stmt=con.createStatement();

  stmt.executeUpdate(sql);

  }

public void close() throws SQLException

  {

if(rs!=null)

rs.close();

if(stmt!=null)

stmt.close();

if(con!=null)

con.close();

  }

}

 

?

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

?

import java.io.File;

import java.io.FileOutputStream;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

 

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFFont;

import org.apache.poi.hssf.usermodel.HSSFRichTextString;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.hssf.util.Region;

 

/**

 * 将oracle中的数据表结构导入到excel中保存

 * @class DataToExcel

 * @description 

 * @author 李智慧

 * @copyRight copyright(c) 2011 广东南航易网通电子商务有限公司,Rights Reserved

 * @time Dec 27, 2011 10:02:08 AM

 */

public class DataToExcel {

public static void main(String[] args) {

String result = "";

List listAll = new ArrayList();

System.out.println("正在读取数据库中所有的表");

try {

List tableList = getTableList();

System.out.println("数据库表读取完成");

for(int i=0;i<tableList.size();i++){

String[] strings = (String[]) tableList.get(i);

String tableName = strings[0].toString();

List list = new ArrayList();

list.add(tableName);

list.add(getStructOfTable(tableName));

System.out.println("正在生成表"+tableName+"的结构");

listAll.add(list);

}

result = TableStructInfoToExcel(listAll,"D:");

System.out.println("数据库中表结构导入已完成");

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

File file = new File(e.getMessage().toString());

if(file.exists()){

file.delete();

}

}

System.out.println(result);

//showView(list); 

}


/**

* 获取数据库中所有的表

* @return

*/

public static List getTableList(){

String sql = "select object_name From user_objects Where object_type='TABLE'";

return getResult(sql,1);

}


/**

* 根据表明

* @param tableName

* @return

*/

public static List getStructOfTable(String tableName){

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"+

" WHERE u.table_name='"+tableName+"' and u.table_name=c.table_name and c.column_name=u.column_name";

ret