daily pastebin goal
51%
SHARE
TWEET

Untitled

a guest Sep 3rd, 2018 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. JDBC MySQL UTF-8 string writing problem
  2. public DBSource(ConnectionInfo ci) throws
  3.         ClassNotFoundException, InstantiationException,
  4.         IllegalAccessException, SQLException
  5. {
  6.     Class.forName("com.mysql.jdbc.Driver").newInstance();
  7.     String dbPath = String.format(
  8.         "jdbc:mysql://%s:%d/%s?user=%s&password=%s&characterEncoding=utf-8&" +
  9.         "useUnicode=true", ci.host, ci.port, ci.dbName, ci.user, ci.password);
  10.     conn = java.sql.DriverManager.getConnection(dbPath);
  11.     prepareTables();
  12. }
  13.    
  14. private void prepareTables() throws SQLException
  15. {
  16.     java.sql.Statement stat = conn.createStatement();
  17.  
  18.     String query = "set names utf8";
  19.     stat.execute(query);
  20.  
  21.     query = "set character set utf8";
  22.     stat.execute(query);
  23.  
  24.     query = "show variables like '%char%'";
  25.     stat.execute(query);
  26.     java.sql.ResultSet rs = stat.getResultSet();
  27.     while (rs.next())
  28.     {
  29.         String k = rs.getString(1);
  30.         String v = rs.getString(2);
  31.         System.out.println(k + " - " + v);
  32.     }
  33.  
  34.     query = "drop table if exists clt";
  35.     stat.execute(query);
  36.     query = "create table clt"
  37.             + "("
  38.             + "  id bigint not null"
  39.             + ", text varchar(50) not null"
  40.             + ") default character set utf8";
  41.     stat.execute(query);
  42. }
  43.    
  44. public void visit(Insert i) throws SQLException
  45. {
  46.     String query = "insert into clt"
  47.             + " (id, text) values (?, ?)";
  48.     java.sql.PreparedStatement stmt = conn.prepareStatement(query);
  49.     if (i.rowData.id == 12656697)
  50.     {
  51.         String toOut = "<<< " + Long.toString(i.rowData.id) + " - " + i.rowData.text;
  52.         System.out.println(toOut);
  53.     }
  54.     int it = 0;
  55.     stmt.setLong(++it, i.rowData.id);
  56.     stmt.setString(++it, i.rowData.text);
  57.     stmt.execute();
  58.     stmt.close();
  59. }
  60.    
  61. public void checkText() throws SQLException
  62. {
  63.     java.sql.Statement stmt = conn.createStatement();
  64.     String query = "select id, text from clt where id = '12656697'";
  65.     stmt.execute(query);
  66.     java.sql.ResultSet rs = stmt.getResultSet();
  67.     while (rs.next())
  68.     {
  69.         String k = rs.getString(1);
  70.         String v = rs.getString(2);
  71.         String toOut = ">>> " + k + " - " + v;
  72.         System.out.println(toOut);
  73.     }
  74. }
  75.    
  76. character_set_client - utf8
  77. character_set_connection - latin1
  78. character_set_database - latin1
  79. character_set_filesystem - binary
  80. character_set_results - utf8
  81. character_set_server - utf8
  82. character_set_system - utf8
  83. character_sets_dir - /usr/share/mysql/charsets/
  84.  
  85. <<< 12656697 - Апарати
  86. >>> 12656697 - ???????
  87.    
  88. [client]
  89. default-character-set=utf8
  90.    
  91. [mysql]
  92. default-character-set=utf8
  93.    
  94. [mysqld]
  95. default-character-set=utf8
  96. character-set-server=utf8
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top