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

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

public class Setup {
    static final int NREC = 100000;
    static final int NCOUNTER = 100;
    public static void main(String[] args) throws SQLException {
        try(Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "")) {
            try(Statement stmt = con.createStatement()) {
                stmt.executeUpdate("DROP TABLE big");
                stmt.executeUpdate("CREATE TABLE big (id INTEGER NOT NULL, data1 VARCHAR(255), data2 VARCHAR(255), PRIMARY KEY(id))");
            }
            try(PreparedStatement pstmt = con.prepareStatement("INSERT INTO big VALUES (?,?,?)")) {
                for(int i = 0; i < NREC; i++) {
                    pstmt.setInt(1,  i + 1);
                    pstmt.setString(2, "Some data for record #" + (i + 1));
                    pstmt.setString(3, "Some other data for record #" + (i + 1));
                    pstmt.executeUpdate();
                }
            }
            try(Statement stmt = con.createStatement()) {
                stmt.executeUpdate("DROP TABLE counter");
                stmt.executeUpdate("CREATE TABLE counter (id VARCHAR(32) NOT NULL, val INTEGER, PRIMARY KEY(id))");
            }
            try(PreparedStatement pstmt = con.prepareStatement("INSERT INTO counter VALUES (?,?)")) {
                for(int i = 0; i < NCOUNTER; i++) {
                    pstmt.setString(1, "c" + (i + 1));
                    pstmt.setInt(2,  0);
                    pstmt.executeUpdate();
                }
            }
        }
    }
}
