import java.io.StringReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class MyMain {
    public static void main( String[] args ) throws Exception {
        // this can fix the mismatch!
        // System.setProperty( "file.encoding", "latin1" );
        System.setProperty( "file.encoding", "utf8" );
        String dbname = args[0];
        String username = args[1];
        String password = args[2];
        System.out.println( "file.encoding=" + System.getProperty( "file.encoding" ) );
        String value = "ä";
        String options = "";
        // this can fix the mismatch, too!
        // String options = "?clobCharacterEncoding=latin1";
        Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/" + dbname + options, username, password );
        Statement stmt = con.createStatement();
        stmt.execute( "DROP TABLE IF EXISTS TestTable" );
        stmt.execute( "CREATE TABLE TestTable ( Testfield LONGTEXT COLLATE latin1_general_cs )" );

        try( PreparedStatement insert = con.prepareStatement( "INSERT INTO TestTable ( Testfield ) VALUES (?)" ) ) {
            insert.setCharacterStream( 1, new StringReader( value ), value.length() );
            insert.executeUpdate();
        }

        ResultSet rs = stmt.executeQuery( "select * from TestTable" );
        rs.next();
        String result = rs.getString( 1 );
        System.out.println( value.equals( result ) ? "match" : "mismatch" );
        System.out.println( result );
        con.close();
    }
}