import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MysqlTest {
	public static void main (String [] args) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
	{
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		
		
		//create table tbl (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, value INTEGER);
		
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/bugtest", "root", "");
		
		PreparedStatement stmt = conn.prepareStatement("INSERT INTO tbl (value) VALUES (?)", Statement.RETURN_GENERATED_KEYS),
				stmt2 = conn.prepareStatement("SELECT * FROM tbl LIMIT 6");
		for (int i = 0; i < 100000000; ++i) {
		    stmt.setString(1, "48");
		    stmt.executeUpdate();
		    
		    ResultSet result = stmt.getGeneratedKeys();
		    result.next();
		    result.getInt(1);
		    result.next();
		    
		    result = stmt2.executeQuery();
		    while (result.next());
		    
		    
		    if (i % 500 == 0) {
		    	//System.out.printf("free-mem: %d, id: %d\n", Runtime.getRuntime().freeMemory()/1024/1024, id);
		    	conn.createStatement().execute("TRUNCATE TABLE tbl");
		    }
		}
		
	}
}
