Chinese characters are not printed when connecting to sql from java

3

I'm doing a function that all it does is print all the values of a field in a table, however I have Chinese letters saved in the database, and from sql server they look good, but when printed in java it shows only question marks, this is my function:

public void imprimir() {
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection connection = DriverManager.getConnection("jdbc:sqlserver://xxx.xxx.xxx.xxx;databaseName=foo;characterEncoding=UTF‑8",
                "usuario", "password");
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("SELECT * FROM TABLA");

        while (resultSet.next())
            System.out.println(resultSet.getString("VALOR"));
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
    }

}
    
asked by gibran alexis moreno zuñiga 30.01.2018 в 21:32
source

2 answers

4

One possibility is that you are not saving the character as unicode in the database. For this you need to use the data type nvarchar instead of varchar .

    
answered by 30.01.2018 / 21:59
source
1

You must specify the encoding to UTF-8

Try the following

PrintStream out = new PrintStream(System.out, true, "UTF-8");
out.println(resultSet.getString("VALOR"));

Or set in the ide the text file encoding in UTF-8

    
answered by 30.01.2018 в 21:56