简体   繁体   中英

Insert in Oracle data from Java loop

I tried to execute this code in order to generate random data in Oracle table:

DriverManager.registerDriver(new oracle.jdbc.OracleDriver());

        // Connect to the database
        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "admin", "qwerty");

        PreparedStatement pstmt
            = conn.prepareStatement("BEGIN "
                + "FOR loop_counter IN 1..10 "
                + "LOOP "
                + "INSERT INTO EVENTS (EVENTID, SOURCE, TYPE, EVENT_DATE, DESCRIPTION) "
                + "VALUES (loop_counter, loop_counter, 'warning', "
                + "TO_DATE(TRUNC(DBMS_RANDOM.VALUE(TO_CHAR(DATE '2000-01-01','J') ,TO_CHAR(DATE '9999-12-31','J'))),'J'), "
                + "DBMS_RANDOM.string('x',15)); "
                + "END LOOP; "
                + "COMMIT; "
                + "END; ");

        pstmt.execute();
        pstmt.close();
        conn.close();

This code is successfully executed in PL/SQL deloper but for some reason it hangs in JUnit test execution. Do you have any idea why?

It would be simpler if you did your looping in Java and performed a batch insert. This existing answer should help you:

Efficient way to do batch INSERTS with JDBC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM