javaロットはoracleにデータを挿入します。

3087 ワード

プロジェクトが必要なので、6 Mのtxtのデータをoracleデータテーブルに挿入する必要があります。txtの中のデータは各行の単語です。統計によると、単語の総数は5040です。便利に見えるように、私はすべての方法をクラスの入り口に書いています。データベースの情報はtestで代替します。コードは下記の通りです。
public static void main(String[] args) throws IOException, Exception {
        // TODO Auto-generated method stub

        Connection conn = null;
        Class.forName("oracle.jdbc.driver.OracleDriver");
        conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:test",
                "test", "test");
        conn.setAutoCommit(false);
        PreparedStatement pst = conn
                .prepareStatement("insert into yq_seq_word values(seq_yq_seq_word.nextval,?)");

        InputStreamReader isr = new InputStreamReader(new FileInputStream(
                new File("C:/Users/Press-Lab/Desktop/test.txt")), "utf-8");
        BufferedReader read = new BufferedReader(isr);

        String s = null;
        int i = 1;
        long start = System.currentTimeMillis();
        while ((s = read.readLine()) != null) {
            //     100     
            if (i % 100 == 0) {
                //
                pst.executeBatch();
                //            ,          ,          。
                try {
                    conn.commit();
                } catch (Exception e) {
                    conn.rollback();
                }
            } else {
                pst.setString(1, s);
                pst.addBatch();
            }
            System.err.println(s);
            i++;
        }
        //       100   ,      100     ,         。              
        //
        pst.executeBatch();
        try {
            conn.commit();
        } catch (Exception e) {
            conn.rollback();
        }
        long end = System.currentTimeMillis();
        //          
        System.out.println("  " + i + " ,  " + (end - start));
        read.close();
    }
以下は効果の印刷です。
5040本を挿入します。時間がかかります。22796。