package StandaloneTest;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ExampleUnicode {
  public static void main(String[] args) throws ClassNotFoundException, SQLException {
    new ExampleUnicode();
  }

  public ExampleUnicode(){
        String user="test";
        String password="test";
        String host = "servernode";
        String dbname = "testdb";
        String url1 = "jdbc:sapdb://" + host + "/" + dbname + "?unicode=yes";
        final String bspC  = "\u5386\u53F2\u56DE";
        final String bspC1 = "\u043F\u0439\u043E\u0435\u0436\u0434";
        long start, end;

        try{
        /*
         * load driver and connect
         */
        Class.forName("com.sap.dbtech.jdbc.DriverSapDB");
        Connection connection = DriverManager.getConnection(url1, user, password);
        Statement  stmt       = connection.createStatement();

        try {
          stmt.executeUpdate("drop table unicodeTest");
        }
        catch (SQLException ex) {
          //ignore tableNotFound-Exception
        }
        /*create a table*/
        stmt.executeUpdate("create table unicodeTest (a varchar(50) unicode)");
        /*insert a unicode value*/
        PreparedStatement ps  = connection.prepareStatement("insert into unicodeTest values(?)");
        ps.setString(1,bspC);
        ps.executeUpdate();
        /*commit all changes*/
        connection.commit();

        /*
         * select unicode data
         */
        ResultSet rs =  stmt.executeQuery("select a from unicodeTest");
        rs.next();
        /*
         * System.out.println normaly prints '?' for unicode characters
         */
        System.out.println(rs.getString(1));
        /*
         * call helper class that prints unicode string. You must set
         * ....
         */
        DisplayString ss = new DisplayString(rs.getString(1),"UnicodeTest");
        /*release the connection*/
        connection.close();
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }

  }

  public class DisplayString extends Frame {
      FontMetrics fontM;
      String outString;

      public DisplayString (String target, String title) {
          setTitle(title);
          outString = target;

          Font font = new Font("SimSun", Font.PLAIN, 30);
          fontM = getFontMetrics(font);
          setFont(font);

          int size = 0;
          for (int i = 0; i < outString.length(); i++) {
             size += fontM.charWidth(outString.charAt(i));
          }
          size += 24;

          setSize(size, fontM.getHeight() + 80);
          setLocation(getSize().width/2, getSize().height/2);
          show();
      }
      public void paint(Graphics g) {
          Insets insets = getInsets();
          int x = insets.left;
          int y = insets.top;
          g.drawString(outString, x + 6, y + fontM.getAscent() + 14);
      }
  }
}