// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class CollateFun {
    private static void executeUpdate(Connection con, String sqlstr) throws SQLException {
        System.out.println(sqlstr);
        try(Statement stmt = con.createStatement()) {
            stmt.executeUpdate(sqlstr);
        }
    }
    private static void executeQuery(Connection con, String sqlstr) throws SQLException {
        System.out.println(sqlstr);
        try(Statement stmt = con.createStatement()) {
            try(ResultSet rs = stmt.executeQuery(sqlstr)) {
                while(rs.next()) {
                    System.out.printf("%d,%s\n", rs.getInt(1), rs.getString(2));
                }
            }
        }
    }
    public static void test(String charset, String collation) throws SQLException {
        try(Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Test", "root", "")) {
            executeUpdate(con, "CREATE TABLE colfun (id INTEGER NOT NULL, s VARCHAR(50), PRIMARY KEY(id)) CHARACTER SET " + charset + " COLLATE " + collation);
            executeUpdate(con, "SET NAMES utf8");
            executeUpdate(con, "INSERT INTO colfun VALUES(1, 'A')");
            executeUpdate(con, "INSERT INTO colfun VALUES(2, 'a')");
            executeUpdate(con, "INSERT INTO colfun VALUES(3, 'Ã?')");
            executeUpdate(con, "INSERT INTO colfun VALUES(4, 'ø')");
            executeQuery(con, "SELECT * FROM colfun WHERE s = 'a'");
            executeQuery(con, "SELECT * FROM colfun WHERE s = 'ø'");
            executeUpdate(con, "DROP TABLE colfun");
        }
    }
    public static void main(String[] args) throws SQLException {    
        test("utf8", "utf8_bin");
        test("utf8", "utf8_general_ci");
        test("utf8", "utf8_danish_ci");
        test("utf8", "utf8_spanish_ci");
    }
}
