Faced a problem on data output from MySQL. Most likely the problem is in the encoding. I did something similar before, but already forgot. Can you remind what you need to do here?

Here is the class:

public void check() { try { InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup("jdbc/sample"); Connection conn = ds.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM peoples"); while (rs.next()) { System.out.println(rs.getString("name")); } } catch (NamingException | SQLException ex) { Logger.getLogger(TestConnection.class.getName()).log(Level.SEVERE, null, ex); } } } 

Here is the output log in the glaze:

 Info: RAR7114: Unable to get ClientInfo for connection Info: ???¤¦?? ?® § ???? ¬ Info: ‚?¤???© ????? «??? ????????®?®  ¤§®? ?® ??????¬ ?¦????¬ ?????¬ ¬ Info: ???¤¦?? Info: ???¤¦?? ?® § ???? ¬ Info: '???? «??? ?«?¦?? ????????®© ?®¤¤??¦?? Info: “?? ?«????© ¬ ? §?®¬ Info: “?? ?«????© ¬ ? §?®¬ Info: ????® «??© ????? ?® ?????? Info: ???¤¦?? ?® ®?? ?§ ??? ¬??®??????© Info: '???? «??? ?® ¬ ??????? Info: visiting unvisited references Info: visiting unvisited references Info: visiting unvisited references Info: Loading application [SampleServer] at [/SampleServer] Info: ChessmanWorkServer was successfully deployed in 1 379 milliseconds. 

Here is the output log if instead of name specify rating (user rating, column in the table).

 Info: Loading application [SampleServer] at [/SampleServer] Info: ChessmanWorkServer was successfully deployed in 323 milliseconds. Info: RAR7114: Unable to get ClientInfo for connection Info: 82 Info: 84 Info: 78 Info: 63 Info: 62 Info: 58 Info: 56 Info: 52 Info: 48 Info: 44 

I sit here, sadly watching these kozyabry. Gentlemen, options?

A day later: Made a servlet here is a screen. The problem is not in the coding netbinsa. enter image description here

Here is the script for creating the table (peoples replaced with workers, the table created more accurately nothing happened) enter image description here

    3 answers 3

    Yes, it is possible in the database that data is stored in one encoding, and the data is read into java in another. Try to explicitly set the encoding. sort of

     properties.setProperty("characterEncoding","cp1251"); Connection con = DriverManager.getConnection("jdbc/sample",properties); 

    Or maybe you have not localized the environment in which the output goes, so check it too.

    Upd: This is how the whole is set up

     Properties properties=new Properties(); properties.setProperty("user","логин"); properties.setProperty("password","пароль"); properties.setProperty("useUnicode","true"); properties.setProperty("characterEncoding","cp1251"); Connection connection=DriverManager.getConnection( "jdbc:mysql://localhost:3306/имяБазы", properties); 

    And in the Mysql server settings, change the standard latin1 encoding to cp1251 - should help

    • As soon as I check it I will write it off. - Alexey Zemtsov
    • Made the encoding in netbut yutf 8 did not help - Alexey Zemtsov
    • and in MySL? Did you ask for the coding (at the java level) through the property? - Alex
    • Yes. stmt.executeQuery ("SET NAMES cp1251"); stmt.executeQuery ("SET CHARACTER SET cp1251"); - Alexey Zemtsov
    • Throw off the script to create a table of peoples - Alex

    Check the encoding in the database. Change if necessary. ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;

    • Today just did. Did not help. - Alexey Zemtsov

    The problem was solved by reinstalling MySQL from 5.5 to 8.0. Also replaced the connector version in the glassfish server. Thanks @Alex for your efforts.