Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Why JDBC need explicit character conversion?

Thread view: 
howachen@gmail.com - 01 Jul 2006 11:06 GMT
Hi,

I use java to connect to MySQL (5.x), using the latest connector.

I have declared the character setting in the connection:
//-----------------------------------------------------------------------------
Properties props = new Properties();
props.put("characterEncoding", "UTF-8");
props.put("useUnicode", "true");
props.put("user", "root");
props.put("password", "password");
Class.forName("com.mysql.jdbc.Driver");

String dbUrl = "jdbc:mysql://" + "127.0.0.1" + "/"
+ "test_db";

this.setConnection(DriverManager.getConnection(dbUrl, props));
//-----------------------------------------------------------------------------

in the query part
//-----------------------------------------------------------------------------

String sqlstr = "SELECT * FROM test_table WHERE id = 8";
try {
        PreparedStatement s = this.getConnection().prepareStatement(sqlstr);
        ResultSet rs = s.executeQuery();

        while (rs.next()) {

            String text1Val = "";
            String text2Val = "";

            text1Val = rs.getString("text1");    // THIS DOES NOT WORK

            try {
                text2Val = new String(rs.getString("text1").getBytes(),
"UTF-8");    // THIS WORK!
            } catch (UnsupportedEncodingException e2) {
                    e2.printStackTrace();
            }
        }
....

//----------------------------------------

the value storing in "text1" field is a UTF-8 character.

Why need this kind of overhead in character conversion when using java?

thanks...
Chris Smith - 01 Jul 2006 19:59 GMT
> I use java to connect to MySQL (5.x), using the latest connector.

As an unrelated side note, have you considered PostgreSQL instead?  It
is free, but also cares about your data integrity, has a better design
and better standards compliance, and most importantly no one from the
PostgreSQL project has ever made any statements to me of the form: "We
think you (and everyone else who has written database-independent JDBC
code that may be usable with MySQL) may owe us money despite possibly
having never used our product, ever; but we can't give you legal advice
on interpreting our own license, so please contact an attorney if you
need advice on this matter."  That last one is important to me. :)

On to your question...

> I have declared the character setting in the connection:
> //-----------------------------------------------------------------------------
> Properties props = new Properties();
> props.put("characterEncoding", "UTF-8");
> props.put("useUnicode", "true");

The reference manual at:

   http://dev.mysql.com/doc/refman/5.0/en/cj-character-sets.html

suggests that you should be specifying "utf8" instead of "UTF-8".  Does
this make any difference?

...

>             text1Val = rs.getString("text1");    // THIS DOES NOT WORK
>
>             try {
>                 text2Val = new String(rs.getString("text1").getBytes(),
> "UTF-8");    // THIS WORK!

Hmm.  That's really bad!  The database appears to be sending the data
back in the system default encoding.  If that data is representable in
the system default encoding, then the code above will work; but
otherwise, it will fail.  Hence, your application will fail non-
deterministically based on things like the operating system, version,
locale settings, environment variables (in UNIX), etc.

If the above suggestion doesn't work, I'll poke around further.

> Why need this kind of overhead in character conversion when using java?

This doesn't have anything to do with Java; it's a JDBC driver problem.

Signature

Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation

howachen@gmail.com - 01 Jul 2006 23:24 GMT
Chris Smith

> > I use java to connect to MySQL (5.x), using the latest connector.
>
[quoted text clipped - 22 lines]
> suggests that you should be specifying "utf8" instead of "UTF-8".  Does
> this make any difference?

this does not work.

in fact, the documentation you quoted above said we should use "UTF-8"

i.e.
When specifying character encodings on the client side, Java-style
names should be used. The following table lists Java-style names for
MySQL character sets...

...or by configuring the JDBC driver to use "UTF-8" through the
characterEncoding property.
Chris Smith - 02 Jul 2006 01:15 GMT
> this does not work.
>
[quoted text clipped - 7 lines]
> ...or by configuring the JDBC driver to use "UTF-8" through the
> characterEncoding property.

Oops.  I misread the page.  Sorry, I don't know why the driver is using
the wrong encoding.

Signature

Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation

Dražen Gemić - 03 Jul 2006 02:40 GMT
>>>I have declared the character setting in the connection:
>>>//-----------------------------------------------------------------------------

AS a matter of fact, I never had that kind of problem with Postgres or
MS SQL, or Hypersonic SQL.....

Check your jdbc driver documentation.....probably there is one.

DG


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.