/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author olav
 */
public class LoadData {
    static Connection conn = null;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Hello world");
        
        loadDriver();
        
        connect();
        
        createTable();
        
        try {
            insert();
        } catch (SQLException ex) {
            Logger.getLogger(LoadData.class.getName()).log(Level.SEVERE, null, ex);
            System.exit(1);
        }
    }

    private static void connect() {
        System.out.println("Connecting to: ");
        try {
            conn = DriverManager.getConnection("jdbc:mysql://trym:33065/perftestdb?" +
                    "user=perftest&password=perftest");
            conn.setAutoCommit(false);
        } catch (SQLException ex) {
            Logger.getLogger(LoadData.class.getName()).log(Level.SEVERE, null, ex);
            System.exit(1);
        }
    }

    private static void createTable() {
        
        // Delete the old table if it exits
        deleteTable();
            
        // Create the new table
        System.out.println("Creates new table");
        try {    
            Statement stmt = conn.createStatement();
            stmt.executeUpdate("CREATE TABLE " + "TESTDATA" + " (priKey int NOT NULL, " +
                    "data VARCHAR(1000), PRIMARY KEY(priKey))");
            conn.commit();
        } catch (SQLException ex) {
            Logger.getLogger(LoadData.class.getName()).log(Level.SEVERE, null, ex);
            System.exit(1);
        }
        
    }

    private static void deleteTable() {
        System.out.println("Deleting old table");
        try {
            Statement stmt = conn.createStatement();
            stmt.executeUpdate("DROP TABLE " + "TESTDATA");
            conn.commit();
        } catch (SQLException ex) {
        }
    }

    private static void insert() throws SQLException {
        PreparedStatement pStmt = null;
        String insertString = "INSERT INTO " + "TESTDATA" + " VALUES(?, ?)";
        try {
            pStmt = conn.prepareStatement(insertString);
        } catch (SQLException ex) {
            Logger.getLogger(LoadData.class.getName()).log(Level.SEVERE, null, ex);
            System.exit(1);
        }
        
        for (int i=1; i<=100000000; ++i) {
            if (i % 100000 == 0) {
                System.out.println(getTime() + ": Inserting record " + i);
            }
            insertOne(pStmt, i);
        }
        
        conn.commit();
    }

    private static void insertOne(PreparedStatement pStmt, int value) throws SQLException {

        String text = new String("Olav was here Olav was here Olav was here Olav was here Olav was here Olav was here Olav was here");
        
        String inText = text + text +text + text +text + text +text + text +text + text  ;

        //System.out.println(inText);

        pStmt.setInt(1, value);
        pStmt.setString(2, inText);
        pStmt.executeUpdate(); 
    }
       
    private static void loadDriver() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LoadData.class.getName()).log(Level.SEVERE, null, ex);
            System.exit(1);
        }
    }


    /** 
     * Get the current time in the a HH:mm:ss format.
     */
    private static String getTime() {
        // Create a time formatter (TODO: make this static)
        SimpleDateFormat timeFormatter =  new SimpleDateFormat("HH:mm:ss");

        // Read and format the current time
        java.util.Date currentTime = new java.util.Date();
        String ret = timeFormatter.format(currentTime);
        return ret;
    }

}
