메뉴 건너뛰기

tnt_lang

jsp/java batch 작업

박상현 2002.02.13 00:40 조회 수 : 2020 추천:22

웹에서 발견한 소스입니다..
유용하게 쓰일수도 있을것 같아 올립니다.
사이트 url;
http://home.clara.net/dwotton/dba/java_insert/test3.txt

소스코드
/*
   test3.java
   ----------

   This java class demonstrates inserting 500,000 rows into a table, one row at
   a time. With just a single commit at the end of the entire job.

   Auto-commit is switched off.

   Bind variables are used for the insert statements. This reduces parsing effort.

   You will need to change a few constants (database name, schema owner, etc.) before
   running this program in your environment. See the code below for details.

   compile using:

       set CLASSPATH=.;C:oracleora81jdbclibclasses111.zip

       javac test3.java



   Written: 18/10/01, Dave Wotton
            For more information about this test, including results and conclusions,
            refer to http://home.clara.net/dwotton/dba/java_insert.htm
*/

import java.io.*;
import java.sql.*;
import java.util.*;
import java.text.*;

public class test3
{
     public static void main (String[] args) throws SQLException  
     {

        int recCount = 0;
        String record = null;

        // You will need to make the following substitutions before running the program:
        //
        //   server_ipname -> the IP name of your Oracle server
        //   oracle_sid    -> the SID of your Oracle instance
        //   Login         -> the owner of the schema holding the dave_test1 table
        //   Password      -> the schema owner's password.

        String connect_string   = "jdbc:oracle:thin:@server_ipname:1521:oracle_sid";
        String Login            = "scott";
        String Password         = "tiger";


        // create a Date variable ...

        java.util.Date now = new java.util.Date();

        // Define a dateformatter to display date variables ...

        DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.DEFAULT);

        // Timestamp the start of the run ...

        long startTime = System.currentTimeMillis();

        // convert the startTime into Date format

        now.setTime(startTime);

        // and display the start of run stats

        System.out.println("Time now: " + df.format(now) );

        // Load Oracle driver ...

        DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
    
        // Connect to the database ...

        Connection conn =
            DriverManager.getConnection (connect_string, Login, Password );

        // Disable auto-commit ...

        conn.setAutoCommit (false);

        try {

           // define a buffered reader, connected to the file "dave_test.in" ...

           FileReader fr     = new FileReader("dave_test.in");
           BufferedReader br = new BufferedReader(fr);

           // define a string to hold the records returned by the buffered reader ...

           record = new String();

           // Prepare an Oracle statement (with bind variables) to perform the update ...

           PreparedStatement pstmt =
                conn.prepareStatement ("insert into dave_test1 values(?,?,?,?)" );


           // Do the real processing ...
           // ----------------------

           while ((record = br.readLine()) != null)
           {
              recCount++;

              // define a tokenizer to split the input record into fields, using ':' as
              // the delimiter.

              StringTokenizer st = new StringTokenizer(record, ":");

              String l_key  = st.nextToken();  // an integer
              String l_col1 = st.nextToken();  // a small word
              String l_col2 = st.nextToken();  // a digit in the range 0-6
              String l_col3 = st.nextToken();  // a long string

              // and insert the row into the database ...

              pstmt.setInt(1,Integer.parseInt( l_key.trim() ) );
              pstmt.setString(2,l_col1.trim() );
              pstmt.setInt(3,Integer.parseInt( l_col2.trim() ) );
              pstmt.setString(4,l_col3.trim() );

              pstmt.executeUpdate ();     // Do the update

              // if ( recCount > 100 ) { break; }
      
           }

           Statement stmt = conn.createStatement ();

           stmt.executeQuery ("commit");

           pstmt.close();    // close the prepared statement object used for the inserts

           stmt.close();     // close the statement object used for the commit

           conn.close();     // close the database connection

           // Timestamp the end of the run ...

           long stopTime = System.currentTimeMillis();

           // Calculate the duration ...

           long runTime = stopTime - startTime;

           // convert to mm:ss ...

           long mins = runTime / 60000;

           long secs = ( runTime % 60000 ) / 1000;

           String mins_str = mins + "" ;

           if ( mins_str.length() == 1 ) { mins_str = '0' + mins_str; }

           String secs_str = secs + "";

           if ( secs_str.length() == 1 ) { secs_str = '0' + secs_str; }

           String Duration = mins_str + ":" + secs_str;

           // convert the stopTime into Date format

           now.setTime(stopTime);

           // and display the end of run stats

           System.out.println("Time now: " + df.format(now) + " Run time: " + runTime + "ms (" + Duration + ")" );

        } catch (IOException e) {
           // catch possible io errors from readLine()
           System.out.println("Uh oh, got an IOException error!");
           e.printStackTrace();
        }

    } // end of main

} // end of class
위로