中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

利用Java如何實現導出oracle數據庫中的表結構

發布時間:2020-11-11 15:52:38 來源:億速云 閱讀:680 作者:Leah 欄目:編程語言

利用Java如何實現導出oracle數據庫中的表結構?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

 Java導出oracle表結構實例詳解

最近用到的,因為plsql是收費的,不讓用,找了很多方法終于發現了這個。

核心語句

SELECT DBMS_METADATA.GET_DDL(U.OBJECT_TYPE, U.object_name), U.OBJECT_TYPE  
FROM USER_OBJECTS U  
where U.OBJECT_TYPE = 'TABLE'  
or U.OBJECT_TYPE = 'VIEW'  
or U.OBJECT_TYPE = 'INDEX'  
or U.OBJECT_TYPE = 'PROCEDURE'  
or U.OBJECT_TYPE = 'SEQUENCE'  
or U.OBJECT_TYPE = 'TRIGGER'  
order by U.OBJECT_TYPE desc 

自己寫的Java方法,未做封裝。

package sql; 
 
import java.io.FileInputStream; 
import java.io.FileWriter; 
import java.sql.Clob; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Properties; 
 
public class Main { 
 
  private static final String TYPE_MARK = "-1"; 
   
  private static String SQL =  
    "SELECT DBMS_METADATA.GET_DDL(U.OBJECT_TYPE, U.object_name), U.OBJECT_TYPE " + 
    "FROM USER_OBJECTS U " + 
    "where U.OBJECT_TYPE = 'TABLE' " + 
    "or U.OBJECT_TYPE = 'VIEW' " + 
    "or U.OBJECT_TYPE = 'INDEX' " + 
    "or U.OBJECT_TYPE = 'PROCEDURE' " + 
    "or U.OBJECT_TYPE = 'SEQUENCE' " + 
    "or U.OBJECT_TYPE = 'TRIGGER' " + 
    "order by U.OBJECT_TYPE desc"; 
   
  private static String URL = "jdbc:oracle:thin:@192.168.1.2:1521:orcl"; 
  private static String USERNAME = "abc"; 
  private static String PASSWORD = "abc"; 
  private static String OUTFILE = "tables.sql"; 
   
  /** 
   * @param args 
   * @throws Exception 
   * @throws  
   */ 
  public static void main(String[] args) throws Exception { 
    // TODO Auto-generated method stub 
    Properties properties = new Properties(); 
    properties.load(new FileInputStream("config.properties")); 
    URL = properties.getProperty("url", URL); 
    USERNAME = properties.getProperty("username", USERNAME); 
    PASSWORD = properties.getProperty("password", PASSWORD); 
    OUTFILE = properties.getProperty("outfile", OUTFILE); 
    SQL = properties.getProperty("sql", SQL); 
     
    FileWriter fw = new FileWriter(OUTFILE); 
    Class.forName("oracle.jdbc.driver.OracleDriver"); 
    Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD); 
    Statement statement = con.createStatement(); 
    ResultSet rs = statement.executeQuery(SQL); 
    Clob ddl; 
    String type = TYPE_MARK; 
    int count = 0; 
    List<String> list = new ArrayList<String>(); 
    while(rs.next()) { 
      ddl = rs.getClob(1); 
      fw.write(ddl.getSubString(1L, (int)ddl.length())); 
      if(!rs.getString(2).equals(type)) { 
        if(!type.equals(TYPE_MARK)) { 
          list.add(type + "," + count); 
          type = rs.getString(2); 
          count = 1; 
        } else { 
          type = rs.getString(2); 
          count ++; 
        } 
      } else 
        count ++; 
    } 
    list.add(type + "," + count); 
    fw.flush(); 
    fw.close(); 
    rs.close(); 
    statement.close(); 
    con.close(); 
    for(String type1 : list) 
      System.out.print(type1.split(",")[0] + ":" + type1.split(",")[1] + ";"); 
    System.out.println(); 
  } 
 
} 

config.properties

url=jdbc:oracle:thin:@192.168.1.2:1521:orcl 
username=abc 
password=abc 
outfile=tables.sql 
sql=SELECT DBMS_METADATA.GET_DDL(U.OBJECT_TYPE, U.object_name), U.OBJECT_TYPE \ 
FROM USER_OBJECTS U \ 
where U.OBJECT_TYPE = 'TABLE' \ 
or U.OBJECT_TYPE = 'VIEW' \ 
or U.OBJECT_TYPE = 'INDEX' \ 
or U.OBJECT_TYPE = 'PROCEDURE' \ 
or U.OBJECT_TYPE = 'SEQUENCE' \ 
or U.OBJECT_TYPE = 'TRIGGER' \ 
order by U.OBJECT_TYPE desc 

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

延庆县| 北流市| 镇坪县| 内丘县| 陕西省| 鹤山市| 九台市| 蓬溪县| 嫩江县| 咸丰县| 交城县| 平阴县| 疏勒县| 迁安市| 孙吴县| 谷城县| 沈丘县| 蒙山县| 手游| 若尔盖县| 罗平县| 南和县| 内丘县| 定襄县| 阿荣旗| 乌鲁木齐县| 宁远县| 崇明县| 景谷| 嵊泗县| 扎鲁特旗| 新巴尔虎右旗| 屯昌县| 刚察县| 高青县| 瑞安市| 衡水市| 凭祥市| 万全县| 封开县| 广河县|