2

I got this program that interacts with MySQL. It works but acts strangely when non-ASCII occur in the statement. I'm using prepared statement:

public ResultSet executeQuery(Connection _conn, int _val1, String _val2) throws SQLException {
    PreparedStatement stmt = _conn.prepareStatement("SELECT c.name  FROM categories c,languages l WHERE c.language = l.id AND c.user = ? AND l.name = ?;");
    stmt.setInt(1, _val1);
    stmt.setString(2, _val2);
    return stmt.executeQuery();
}

It works fine unless I use something like "čččč" in _val2. Problem is somewhere in Java because when I prepare the statement a print it to stdout those characters are just "????". Any suggestions?

Michal Artazov
  • 4,368
  • 8
  • 25
  • 38
  • What happens when it don't work? And don't be fooled by the console, you may need to configure it to display other charsets than ANSI. – Pragmateek Jun 15 '13 at 14:22

1 Answers1

5

Try adding characterEncoding=UTF-8 parameter to the end of your JDBC connection URL. Even set the table and column character set to UTF-8. This article explains how to do that.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • Yes. Or whatever the appropriate encoding in MySQL is I suppose? For the czech language it could also be ISO-8859-2. – tbsalling Jun 15 '13 at 14:37
  • @tbsalling Yes absolutely. – AllTooSir Jun 15 '13 at 14:38
  • Actually, I think that the encoding of the source file containing the "čččč" might also matter. This is configured in the IDE/editor and must match the encoding stated/selected when preparing the JDBC connection. – tbsalling Jun 15 '13 at 14:44
  • Thanks, `characterEncoding=utf8` fixed it. I already had all the tables and fields set to UTF-8 so probably it must be done altogether. – Michal Artazov Jun 15 '13 at 14:52
  • Any ideas on WHERE to set this JDCB-connection url? Can't find it anywhere (in my netbeans project).. :p – Tor Claesson Aug 28 '14 at 09:39
  • @TorClaesson It can be as a hard coded String, in properties file, as a JNDI object .... – AllTooSir Aug 28 '14 at 09:42