// 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 = "Noget tekst bla. bla. bla.";
            stmt.executeUpdate("INSERT INTO tt VALUES (" + i + ",'" + s + "')");
            stmt.close();
        }
        long t2 = System.currentTimeMillis();
        System.out.println("Statement ikke genbrugt: " + (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 = "Noget tekst bla. bla. bla.";
            stmt.executeUpdate("INSERT INTO tt VALUES (" + i + ",'" + s + "')");
        }
        stmt.close();
        long t2 = System.currentTimeMillis();
        System.out.println("Statement genbrugt: " + (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, "Noget tekst bla. bla. bla.");
            pstmt.executeUpdate();
            pstmt.close();
        }
        long t2 = System.currentTimeMillis();
        System.out.println("PreparedStatement ikke genbrugt: " + (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, "Noget tekst bla. bla. bla.");
            pstmt.executeUpdate();
        }
        pstmt.close();
        long t2 = System.currentTimeMillis();
        System.out.println("PreparedStatement genbrugt: " + (t2 - t1));
    }
    private static void test(String txt, String url, String un, String pw) throws SQLException {
        System.out.println(txt);
        Connection con = DriverManager.getConnection(url, un, pw);
        testStatSing(con);
        testStatMulti(con);
        testPrepStatSing(con);
        testPrepStatMulti(con);
        con.close();
    }
    public static void main(String[] args) throws Exception {
        Class.forName("oracle.jdbc.OracleDriver");
        Class.forName("com.mysql.jdbc.Driver");
        test("Oracle", "jdbc:oracle:thin:@localhost:1521:arnepc3", "arne", "xxxx");
        test("MySQL", "jdbc:mysql://localhost/Test", "", "");
    }
}
