// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package testprep;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class TestPrep4 {
    private static final int N = 10000;
    private static void delete(Connection con) throws SQLException {
        Statement stmt = con.createStatement();
        stmt.executeUpdate("DELETE FROM tt");
        stmt.close();
    }
    private static void testStatSing(Connection con) throws SQLException {
        delete(con);
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < N; i++) {
            Statement stmt = con.createStatement();
            String s = "Some text bla bla bla";
            stmt.executeUpdate("INSERT INTO tt VALUES (" + i + ",'" + s + "')");
            stmt.close();
        }
        long t2 = System.currentTimeMillis();
        System.out.println("Statement not reused: " + (t2 - t1));
    }
    private static void testStatMulti(Connection con) throws SQLException {
        delete(con);
        long t1 = System.currentTimeMillis();
        Statement stmt = con.createStatement();
        for(int i = 0; i < N; i++) {
            String s = "Some text bla bla bla";
            stmt.executeUpdate("INSERT INTO tt VALUES (" + i + ",'" + s + "')");
        }
        stmt.close();
        long t2 = System.currentTimeMillis();
        System.out.println("Statement reused: " + (t2 - t1));
    }
    private static void testPrepStatSing(Connection con) throws SQLException {
        delete(con);
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < N; i++) {
            PreparedStatement pstmt = con.prepareStatement("INSERT INTO tt VALUES (?, ?)");
            pstmt.setInt(1, i);
            pstmt.setString(2, "Some text bla bla bla");
            pstmt.executeUpdate();
            pstmt.close();
        }
        long t2 = System.currentTimeMillis();
        System.out.println("PreparedStatement not reused: " + (t2 - t1));
    }
    private static void testPrepStatMulti(Connection con) throws SQLException {
        delete(con);
        long t1 = System.currentTimeMillis();
        PreparedStatement pstmt = con.prepareStatement("INSERT INTO tt VALUES (?, ?)");
        for(int i = 0; i < N; i++) {
            pstmt.setInt(1, i);
            pstmt.setString(2, "Some text bla bla bla");
            pstmt.executeUpdate();
        }
        pstmt.close();
        long t2 = System.currentTimeMillis();
        System.out.println("PreparedStatement reused: " + (t2 - t1));
    }
    public static void main(String[] args) throws Exception {
        Class.forName("oracle.jdbc.OracleDriver");
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "arne", "xxxx");
        testStatSing(con);
        testStatMulti(con);
        testPrepStatSing(con);
        testPrepStatMulti(con);
        con.close();
    }
}
