Link to home
Start Free TrialLog in
Avatar of teagish
teagish

asked on

Read "special" characters (é,°) from MySQL database

Hi guys,

  My database contains some special characters like "é" or "°" and when I'm reading my resultset (from a select query) these characters are not "translated" correctly when I'm using resultset.getString("name_of_column").  How can I get them right?
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

do you have some example code?

and results?  and expected results?
Avatar of GumB
GumB

You could try getCharacterStream() or getAsciiStream() (treat it as a CLOB), which seems to handle special character conversions better than getString().  However, I put this up in the spirit of collaboration and with the hope that someone else has a better (simpler) solution.

However, this is something I did in the past when I ran into this problem and it produced good results.
Avatar of teagish

ASKER

Here is the function :

  private boolean set_titres(int id_table_choisie)
  {
    java.sql.ResultSet resultat;
    String titre_graphique = null,
        nom[] = new String[2];

    for(int i=1;i>=0;i--)
    {
      try
      {
        if ( (resultat = requete("SELECT * FROM tinfo_on_tables WHERE id=" + id_table_choisie + " AND nom_colonne='" + colonne[i] + "'")) == null)
        {
          return false;
        }
        else
        {
          if (!resultat.next())
          {
            return false;
          }
          else
          {
            <b>nom[i] = resultat.getString("nom_tableau");</b>
            System.out.println(nom[i]);
            if(i==1)
            {
              titre_graphique = "Valeur "+resultat.getString("article")+" "+nom[i]+" en fonction ";
            }
            if(i==0)
            {
              titre_graphique += resultat.getString("article")+" "+nom[i];
            }
            if (resultat.getString("unites") != null)
            {
              nom[i] += " (" + resultat.getString("unites") + ")";
            }
          }
        }
      }
      catch (java.sql.SQLException e)
      {        
        return false;
      }
    }
    //here is some instantiation of a private variable
    toutes_donnees.set_titre_axe_x(nom[0]);
    toutes_donnees.set_titre_axe_y(nom[1]);
    toutes_donnees.set_titre_graphique(titre_graphique);

    return true;
  }

I get "d?formation" from System.out.println(nom[i]) when it is "déformation" in the database.
Avatar of teagish

ASKER

private java.sql.ResultSet requete(String nom_commande)
  {
    java.sql.Statement rapport;
    java.sql.ResultSet resultat;

    try
    {
      rapport = conn_mysql.createStatement();
    }
    catch(java.sql.SQLException e)
    {
      //sortie.append("Erreur lors de la création d'un Statement : \""+e.getMessage()+"\" code n° "+e.getErrorCode()+"\n");
      return null;
    }

    try
    {
      resultat = rapport.executeQuery(nom_commande);
    }
    catch(java.sql.SQLException e)
    {
      //sortie.append("Erreur lors de la requête \""+nom_commande+"\" à la base de données : \""+e.getMessage()+"\" code n° "+e.getErrorCode()+"\n");
      return null; //DEBUGRESTE -> tenir compte du null
    }


    return resultat;
  }
Avatar of teagish

ASKER

I forgot to paste the requete method:

  private java.sql.Connection conn_mysql;

  private java.sql.ResultSet requete(String nom_commande)
  {
    java.sql.Statement rapport;
    java.sql.ResultSet resultat;

    try
    {
      rapport = conn_mysql.createStatement();
      resultat = rapport.executeQuery(nom_commande);
    }
    catch(java.sql.SQLException e)
    {
      return null;
    }

    return resultat;
  }
Avatar of teagish

ASKER

I forgot to paste the requete method:

  private java.sql.Connection conn_mysql;

  private java.sql.ResultSet requete(String nom_commande)
  {
    java.sql.Statement rapport;
    java.sql.ResultSet resultat;

    try
    {
      rapport = conn_mysql.createStatement();
      resultat = rapport.executeQuery(nom_commande);
    }
    catch(java.sql.SQLException e)
    {
      return null;
    }

    return resultat;
  }
This seems to me like a problem with your driver. You could of course do a character encoding translation by manipulating bytes/string/streams, but that would not be tackling the problem at source. What encoding is used in the database?
You may have to reconfigure your system to view the special characters on your server console.

I recommend trying to re-insert the special characters you pull from the database that appear as ?s in the console back into the database.  This will help you to determine where the corruption is occurring, if it in fact is.

Please see my post on https://www.experts-exchange.com/questions/20538200/Saving-Euro-Symbol-into-Oracle-database.html for information about your NLS_LANG setting in the registry.  Modifying this may allow your console to display non-standard ASCII characters -- it did for me on OC4J.

Let me know if this helps.
ASKER CERTIFIED SOLUTION
Avatar of GumB
GumB

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of teagish

ASKER

I've test my program drawing my text using Graphics2D and i'm able to print the letter '&eacute;' but the values given by the method getString("nom_tableau") shows a little rectangle instead of that letter.
Avatar of teagish

ASKER

How do I do to see / change the value of NLS_LANG??  I looked in the registry and it wasn't there...
>>I've test my program drawing my text using Graphics2D and i'm able to print the letter '&eacute;' but the values given by the method getString("nom_tableau") shows a little rectangle instead of that letter.
>>

In the first part of the above, you must of course use a Font that can display the characters you want. In the second part, i'm not clear what you're actually doing.
Avatar of teagish

ASKER

My program is drawing a graphic using input from a MySQL database.
My font can draw '&eacute;' because i tried it manually.  But when i take the String that i took from the database it shows a rectangle instead of a '&eacute;'.  And, when i'm accessing the data from DOS using the mysql program I can see the '&eacute;' in my values.
>>But when i take the String that i took from the database it shows a rectangle instead of a '&eacute;'.  


I take it that it's the String in the code below?

>><b>nom[i] = resultat.getString("nom_tableau");</b>

Can you print out the following

char[] chars = resultat.getString("nom_tableau").toCharArray();
for(int i = 0;i < chars.length ;i++) {
  System.out.println("0x" + Integer.toHexString(chars[i]));
}

Avatar of teagish

ASKER

it gives me :
"0x64, 0x82, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x69, 0x6f, 0x6e" and the unicode letter i'm trying to do (supposed to be the second) is E9
Avatar of teagish

ASKER

Is it possible that the characters returned are in ASCII code (that stops to 0x7F)??
Not sure yet.

Does the number of characters in your string equal the number returned in the above loop?

What is the unicode encoding for the character you're trying to use - \u00e9?
Avatar of teagish

ASKER

yes the unicode is \u00E9
and yes the number of character equals the number in the string...
It's this 0x82 character that's the problem. I don't know what encoding it is - do you?
Avatar of teagish

ASKER

No I don't, and I'm wondering which it is...
Avatar of teagish

ASKER

I decided to use a code in the database that specified the Unicode code for the letter as \U00E9...  When I meet this kind of code, I "translate" the code to its real value as a letter...
teagish:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split between GumB and CEHJ.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TimYates
EE Cleanup Volunteer