Insert to a database using java: problem with greek characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stelios andread
    New Member
    • Jan 2011
    • 2

    Insert to a database using java: problem with greek characters

    I'm writing a java programme, which connects to a database in my computer(using jdbc). The database is allowed to use greek characters and I can check that, because when I use a "select" query, the text appears as it should. But when I try to insert new data, instead of greek characters, the result in the database is only some questionmarks. Where is the problem? (I've already defined the collation as utf-8)
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Can you please post the code here. I think it has to do something with the character encoding.

    Regards
    Dheeraj Joshi

    Comment

    • stelios andread
      New Member
      • Jan 2011
      • 2

      #3
      Thank you for the reply.

      the part of the code is the following:

      try
      {
      Statement stmt;
      ResultSet rs;
      Class.forName(" com.mysql.jdbc. Driver");
      String url ="jdbc:mysql ://localhost:3306/users?useUnicod e=true&char acterEncoding=U TF-8&autoRecon nect=true";
      Connection con =(Connection) DriverManager.g etConnection(ur l,"root", "");
      con.setCharacte rEncoding("utf-8");
      stmt = (Statement) con.createState ment();
      stmt.executeQue ry("SET NAMES 'UTF8'");
      stmt.executeQue ry("SET CHARACTER SET 'UTF8'");
      String greekname = "κωνσταντίν α";
      stmt.executeUpd ate("INSERT INTO user2(userid,us ername) VALUES ('" + "1" + "','" + greekname +"')");
      con.close();
      }catch( Exception e ) {System.out.pri ntln("problem during the connection with the database!");}



      The result in the table user2 is userid:1 and username:?????? ?????
      By the way, when i try to insert the same data using php, everything runs ok. However, i need to do this using java.

      Comment

      Working...