欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

导出Oracle数据库所有表结构

程序员文章站 2022-03-15 15:03:20
...

代码一:DBUtil类
    该类请参考我写的《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数据库中,运行就可以创建表结构了(没有主键)。另外,没有把运行结果写到一个文件中,是因为我觉得这样更方便。其他方面,后续仍可完善。