Oracle資料庫連接產生DataX的job-Json

來源:互聯網
上載者:User

標籤:acl   code   com   name   ati   設定   final   user   標準   

package com.bbkj.main;import com.bbkj.DbUtils.ConnectionPoolManager;import com.bbkj.DbUtils.DbUtil;import com.bbkj.DbUtils.IConnectionPool;import java.io.*;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.HashSet;import java.util.Set;/** * Created by Administrator on 2016/12/7. */public class Json {    public static void main(String [] args) throws InterruptedException {        testOracle();    }    /**     * 一個非常標準的串連Oracle資料庫的範例程式碼     */    public static void testOracle()    {        Connection con = null;// 建立一個資料庫連接        PreparedStatement pre = null;// 建立先行編譯語句對象,一般都是用這個而不用Statement        ResultSet result = null;// 建立一個結果集對象        try        {            con = DbUtil.pool.getConnection();            String sql = "select * from user_tables";// 先行編譯語句,“?”代表參數            pre = con.prepareStatement(sql);// 執行個體化先行編譯語句            //pre.setString(1, "");// 設定參數,前面的1表示參數的索引,而不是表中列名的索引            result = pre.executeQuery();// 執行查詢,注意括弧中不需要再加參數            StringBuilder sbstr =new StringBuilder();            //fos.write(rs.getInt(1));            InputStreamReader fis = new InputStreamReader(                    new FileInputStream(System.getProperty("user.dir")+"/template/template.json"));            BufferedReader  bis = new BufferedReader(fis);            String data = null;            while ((data =bis.readLine())!=null){                sbstr.append(data);            }            data = sbstr.toString();            while (result.next()) {                StringBuilder sb =new StringBuilder();                String subsql = "select * from user_tab_columns where table_name = ‘" + result.getString("table_name") + "‘";                PreparedStatement subpre =con.prepareStatement(subsql);                ResultSet subresult = subpre.executeQuery();                int  i=0;                FileOutputStream fos = new FileOutputStream("E:\\bobo\\dbfile\\"+result.getString("table_name")+".json",false);                PrintStream p = new PrintStream(fos);                while (subresult.next()) {                    if(i>0){                        sb.append(",");                    }                    sb.append(" ").append(subresult.getString("COLUMN_NAME"))                            .append(" ");                    i++;                }                System.out.println(sb.toString());                data = data.replaceAll("\\$\\{table\\}",result.getString("table_name"));                data = data.replaceAll("\\$\\{fields\\}",sb.toString());                p.print(data.toString());                p.close();                fos.flush();            }        }        catch (Exception e)        {            e.printStackTrace();        }        finally        {            try            {                // 逐一將上面的幾個對象關閉,因為不關閉的話會影響效能、並且佔用資源                // 注意關閉的順序,最後使用的最先關閉                if (result != null)                    result.close();                if (pre != null)                    pre.close();                if (con != null) {                    DbUtil.pool.releaseConn(con);                    con.close();                }                System.out.println("資料庫連接已關閉!");            }            catch (Exception e)            {                e.printStackTrace();            }        }    }}

這裡用到了一個別人寫的資料庫連接池:我的另外一篇文章有轉載http://www.cnblogs.com/HendSame-JMZ/articles/6145456.html

//取得該使用者下所有的表
select * from user_tables;
//取得表名為Sysuser的注釋資訊
select * from user_tab_comments where table_name = ‘SYSUSER‘;
//取得該使用者下表名為Sysuser表的結構
select * from user_tab_columns where table_name=‘SYSUSER‘;
//取得該使用者下表名為sysuser表中欄位的注釋資訊
select * from user_col_comments where table_name = ‘SYSUSER‘;
//取得該使用者下所有表的中文名稱和英文名稱
select t2.table_name,t2.comments from user_tables t1,user_tab_comments t2 where t1.table_name=t2.table_name;
TABLE_NAME COMMENTS
------------------------------ -----------------------------------------------
SYSUSER 使用者表
//取得表sysuser中的英文欄位名,中文欄位名,欄位類型,欄位長度
select t1.TABLE_NAME,t1.COLUMN_NAME,t2.COMMENTS,t1.DATA_TYPE,t1.DATA_LENGTH from user_tab_columns t1, user_col_comments t2 where t1.TABLE_NAME=‘SYSUSER‘ and t1.TABLE_NAME=t2.TABLE_NAME and t1.COLUMN_NAME=t2.COLUMN_NAME;
TABLE_NAME COLUMN_NAME COMMENTS DATA_TYPE DATA_LENGTH
----------- ----------------------------------------------------------------------
SYSUSER SYSUSERID 使用者ID VARCHAR2 50
SYSUSER SYSUSERNAME 使用者名稱 VARCHAR2 50
SYSUSER SYSUSERPASSWORD 密碼

 

Oracle資料庫連接產生DataX的job-Json

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.