views:

170

answers:

4

Hi there. My question would be what's wrong with the next code? I'm trying with j2ee to read some unicode from a database and some characters are returned as the famous question mark.

try { Class.forName("com.mysql.jdbc.Driver"); String connectionUrl = "jdbc:mysql://localhost/hortimart?" + "user=webservices&password=stipjeservers"; Connection con = DriverManager.getConnection(connectionUrl);

        Statement stmt = null;
        ResultSet rs = null;
        String SQL = "SELECT FirstName,LastName FROM users";

        stmt = con.createStatement();
        rs = stmt.executeQuery(SQL);

        while (rs.next())
        {

            byte[] firstNameBytes = rs.getBytes(1);
            String FirstName =new String(firstNameBytes,"UTF-8");
            byte [] lastNameBytes = rs.getBytes(2);
            String LastName =new String(lastNameBytes,"UTF-8");
            System.out.println(FirstName+" "+LastName);

        }

    }
    catch (SQLException e)
    {
        System.out.println("SQL Exception: "+ e.toString());
    }
    catch (ClassNotFoundException cE)
    {
        System.out.println("Class Not Found Exception: "+ cE.toString());
    }

Now i've tried this code with j2Se as well and it works. So is it j2EE or i missed something in my code?

Thanks

A: 

try to append characterEncoding=utf-8 to your connection url. ie:

String connectionUrl = "jdbc:mysql://localhost/hortimart?" + "user=webservices&password=stipjeservers&characterEncoding=utf-8
Omry
unfortunately the result is the same
Alexandru SARARU
A: 

Do you know if the strings have been encoded in UTF-8 (Unicode encompasses many encodings, UTF-8 being only one of them). Have you tried using a simple rs.getString(1) ?

Brian Agnew
yes i tried and it gets the question marks.
Alexandru SARARU
A: 

It could be that either i) you have data in the database that is not unicode (how are your tables defined?) or ii) that your driver is not unicode-enabled.

EDIT: Have you tried setting both useUnicode and characterEncoding in your connection string?

...useUnicode=true&characterEncoding=utf8
davek
the type of the fields is varchar and the Collation is set to utf8_unicode_ci(unicode multilangual, case insensitive)
Alexandru SARARU
Alexandru SARARU
+2  A: 

It's most likely your console that's misconfigured. Have you examined the bytes of the result to see if it's UTF-8?

Jonathan Feinberg
i'm not getting UTF-8. all i get are numbers like this -72 -48 -67 -48 -77 -48 which appear to be correct since i get the same numbers in j2me as in j2se and in j2se the characters are displayed correctly
Alexandru SARARU
If they are the same numbers in two places, and those numbers display as expected in one place, then it is the **display** that is broken, as I suggested, and not the encoding. UTF-8 **is** a series of "numbers", which have to be decoded and interpreted as Unicode code points to be displayed as human-readable glyphs.
Jonathan Feinberg
i'm using NetBeans and the only difference between those two places is this: in a simple java project it works (j2se) but in a web project it doesn't (j2ee).
Alexandru SARARU
the display is the NetBeans console and it's the same for both projects
Alexandru SARARU
Your problems are: not understanding what Unicode is, what UTF-8 is, how System.out works, and how to recognize the right answer when it's staring you in the face. Good luck!http://www.joelonsoftware.com/articles/Unicode.html
Jonathan Feinberg
+1 since it encapsulates everything I thought but couldn't face writing out. I'd give it another +1 for the Joel article if I could...
Brian Agnew