package us.fatehi.utf8db2;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class UTF8DB2
{
  public static void main(final String[] args)
    throws Exception
  {

    /*
     * final String url = "jdbc:db2://192.168.56.101:50000/SAMPLE";
     * final String username = "db2inst1"; final String password =
     * "sualeh-db2-password";
     */
    final String url = "jdbc:db2://sfatehi-vm1:50000/bevap:securityMechanism=4;currentSchema=SFATEHI;";
    final String username = "sfatehi";
    final String password = null;

    try (final Connection connection = getConnection(url,
                                                     username,
                                                     password,
                                                     true))
    {
      // 1. Clear table for this test
      try (final Statement statement = connection.createStatement();)
      {
        statement.execute("DROP TABLE TAB1");
        statement.execute("CREATE TABLE TAB1 (COL1 VARCHAR(3))");
      }
      System.out.println("Test table dropped and re-created");

      // 2. Insert bad UTF-8 bytes
      final String insertTableSQL = "INSERT INTO TAB1 (COL1) VALUES (?)";
      try (final PreparedStatement preparedStatement = connection
        .prepareStatement(insertTableSQL);)
      {
        preparedStatement.setBytes(1,
                                   new byte[] {
                                                (byte) 0xE2,
                                                (byte) 0x82,
                                                (byte) 0xAC
                                   });
        preparedStatement.executeUpdate();

        preparedStatement.setBytes(1,
                                   new byte[] {
                                                (byte) 0xBF,
                                                (byte) 0x20,
                                                (byte) 0x52
                                   });
        preparedStatement.executeUpdate();
      }
      System.out.println("Data inserted into table");

      // 3. Read them back out
      // http://www-01.ibm.com/support/docview.wss?uid=swg21684365
      try (final ResultSet results = connection.createStatement()
        .executeQuery("SELECT * FROM TAB1");)
      {
        while (results.next())
        {
          System.out.println(results.getString(1));
        }
      }
    }
  }

  private static Connection getConnection(final String url,
                                          final String username,
                                          final String password,
                                          final boolean handleBrokenUF8)
    throws SQLException
  {
    final Properties connectionProps = new Properties();
    connectionProps.put("user", username);
    if (password != null)
    {
      connectionProps.put("password", password);
    }

    if (handleBrokenUF8)
    {
      System.setProperty("db2.jcc.charsetDecoderEncoder", "3");
    }

    final Connection conn = DriverManager.getConnection(url, connectionProps);

    System.out.println("Connected to database, " + conn.toString());
    return conn;
  }

}

@@ -46,24 +46,58 @@
 Connection;%0A
+import java.sql.DatabaseMetaData;%0A
 import java.
@@ -1126,24 +1126,202 @@
 BLE TAB1%22);%0A
+      %7D%0A      catch (final Exception e)%0A      %7B%0A        System.err.println(e.getMessage());%0A      %7D%0A      try (final Statement statement = connection.createStatement();)%0A      %7B%0A
         stat
@@ -3250,24 +3250,99 @@
 word);%0A    %7D
+%0A    connectionProps.put(%22retrieveMessagesFromServerOnGetMessage%22, %22true%22);
 %0A%0A    if (ha
@@ -3509,24 +3509,82 @@
 ionProps);%0A%0A
+    final DatabaseMetaData metaData = conn.getMetaData();%0A
     System.o
@@ -3586,23 +3586,22 @@
 tem.out.
-println
+format
 (%22Connec
@@ -3621,25 +3621,131 @@
 se, 
-%22 + conn.toString
+%25s %25s%25n%22,%0A                      metaData.getDatabaseProductName(),%0A                      metaData.getDatabaseProductVersion
 ());

Drop and recreate working