0

Basically I have Column row with the value of:

   ID            Title
 ----- ----------------------------------
|  1  |ماهر زين - عليك صلى الله (Official) |
 ----- ----------------------------------

-

String Title = null;
DBConnect Database = new DBConnect();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
    con = Database.getcon();
    String query2="SELECT TITLE FROM table WHERE ID=?";
    ps = con.prepareStatement(query2);
    ps.setInt(1, 1);
    rs=ps.executeQuery();
    if(rs.next()){
        Title=rs.getString(1);
        System.out.println(Title);
    }
} finally {
    if(ps != null)
        ps.close();
    if(rs != null)
        rs.close();
    if(con != null)
        con.close();    
}

To connect to the DB I Do:

public DBConnect(){
    try{
        Class.forName("com.mysql.jdbc.Driver");

        String unicode="useSSL=false&useUnicode=yes&characterEncoding=UTF-8";
        con = DriverManager.getConnection("jdbc:mysql://localhost:15501/db?"+unicode, "root", "pwd");
        st = con.createStatement();
    }catch(Exception ex){
        System.out.println(ex.getMessage());
        System.out.println("couldn't connect!");
    }
}

But the Output I get is: ????? ??? ????? ? ??? ?? ????? (Official)

I also have tried:

ALTER DATABASE <name> CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE <table_name> CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

- I also tried:

select 
c.character_set_name 
from information_schema.tables as t,
 information_schema.collation_character_set_applicability as c
where c.collation_name = t.table_collation
and t.table_schema = "duckdb"
and t.table_name = "stackscontents";

The output I get is `utf8mb4

FYI: My my.ini file says default-character-set=utf8

Yet still i'm getting the output as: ????? ??? ????? ? ??? ?? ????? (Official)

All replies are much appreciated

Ahmad Nabi
  • 43
  • 3
  • 11
  • What device are you outputting to? – Maarten Bodewes Jun 18 '17 at 22:16
  • where did you print this output? – Youcef LAIDANI Jun 18 '17 at 22:18
  • @MaartenBodewes if you mean OS, I am using windows (8.1) x64 PC, MySql 57, and the output i get is from Eclipse IDE, at `if(rs.next())` – Ahmad Nabi Jun 18 '17 at 22:19
  • This link could be of assistance: https://stackoverflow.com/questions/5405236/jdbc-mysql-utf-8-string-writing-problem/5405448#5405448 – Costis Aivalis Jun 18 '17 at 22:23
  • In order to see whether the retrieved from the database is wrong, or the output to the console, dump the string like `System.out.println(s.codePoints().collect(Collectors.toList()));` – Joop Eggen Jun 18 '17 at 22:25
  • @JoopEggen What is `s`? – Ahmad Nabi Jun 18 '17 at 22:34
  • Could you check [this article](https://decoding.wordpress.com/2010/03/18/eclipse-how-to-change-the-console-output-encoding/) about how to set the output encoding? Maybe it is not set correctly? Try UTF-16 & UTF-16LE as well. – Maarten Bodewes Jun 18 '17 at 22:38
  • @AhmadNabi `s` is the string of course, I presume `Title` in your case, what else to get code points from? – Maarten Bodewes Jun 18 '17 at 22:50
  • @MaartenBodewes it says `The method collect(Supplier, ObjIntConsumer, BiConsumer) in the type IntStream is not applicable for the arguments (Collector>)` – Ahmad Nabi Jun 18 '17 at 23:02
  • Did you check the encoding of the console I asked you? That's at least easy to do. That other question above is probably best asked at to @JoopEggen again, I don't know that one out of the top of my head. – Maarten Bodewes Jun 18 '17 at 23:10
  • @MaartenBodewes First so sorry for my late reply, Second I said that it pops the above error which makes it not compilable, see the error in above comment plz, and the link you provide above, it's images are broken – Ahmad Nabi Jun 19 '17 at 00:27
  • @AhmadNabi with `s` I meant the String read from the database. Dumping the content as int codepoints would probably show that the content is fine, without `?`. Then it is probably a short-coming of the charset of the console as Maarten Bodewes mentions. Dumping to file would be an alternative to the console (Logger). – Joop Eggen Jun 19 '17 at 20:08

0 Answers0