【Oracle笔记】一个实现导出Oracle数据库所有表结构的代码

代码一:DBUtil类

    该类请参考我写的【Oracle笔记】一个完美的JDBC连接Oracle数据库的示例代码。

代码二:DBCreate类

package util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.naming.spi.DirStateFactory.Result;

public class DBCreate {
	public static void main(String[] args) {
		String sql2="select object_name From user_objects Where object_type='TABLE'";
		String sql="select * from user_tab_columns where table_name=?";
		Connection connection=DBUtil.getConnection();
		String str1="";
		try {
			PreparedStatement preparedStatement=connection.prepareStatement(sql);
			PreparedStatement preparedStatement2=connection.prepareStatement(sql2);
			ResultSet result2=preparedStatement2.executeQuery();			
			while (result2.next())// 当结果集不为空时
			{	
				int i=1;
				String temp=result2.getString("OBJECT_NAME");
				preparedStatement.setString(i, temp);
			    //System.out.print("表名:"+result2.getString("OBJECT_NAME")+"\t");
			    
			    ResultSet result=preparedStatement.executeQuery();
			    StringBuilder sb=new StringBuilder();
			    sb.append("CREATE TABLE "+temp+"(");
				while (result.next())// 当结果集不为空时
				{
					sb.append(result.getString("COLUMN_NAME")+"  "+
				(result.getString("DATA_TYPE")=="DATE"?"DATE":result.getString("DATA_TYPE")+"("+result.getString("DATA_LENGTH")+")")+",");
				}
				System.out.println(sb.substring(0, sb.length()-1)+");");
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
}

运行结果:

CREATE TABLE MY_EDUCATION(EDUCATION  VARCHAR2(8),EDUCATION_DESC  VARCHAR2(120),COMPANY  VARCHAR2(80),TRAN_TIMESTAMP  VARCHAR2(68),TRAN_TIME  NUMBER(22));
CREATE TABLE MY_INDUSTRY(INDUSTRY  VARCHAR2(80),INDUSTRY_DESC  VARCHAR2(240),PARENT_INDUSTRY  VARCHAR2(80),RISK_LEVEL  VARCHAR2(4),INDUSTRY_LEVEL  VARCHAR2(8),DETAIL_IND  VARCHAR2(4),STANDARD_IND  VARCHAR2(4),COMPANY  VARCHAR2(80),TRAN_TIMESTAMP  VARCHAR2(68),TRAN_TIME  NUMBER(22));

总结:
  将运行结果copy到Oracle数据库中,运行就可以创建表结构了(没有主键)。另外,也能使用dump命令完成,可以参考我写的【Oracle笔记】数据库dump导入和导出。

你可能感兴趣的:(Oracle笔记)