How to compare utf8 text in mysql query using java How to compare utf8 text in mysql query using java database database

How to compare utf8 text in mysql query using java


Use parameters. The driver should then encode them correctly according to the connection properties.

PreparedStatement statement = connection.prepareStatement(    "select age from student where name = ?");      statement.setString(1, stringVariable);

In addition, this also correctly escapes special SQL characters (like single quotes).


You shouldn't have to transform anything. The driver takes care of everything. Try using a prepared statement rather than string concatenation. This will have the additional advantage of avoiding SQL injection attacks, and make sure your statement works even if the string variable contains a quote.


I solved this problem it was not only related with the database query but I get the string from ajax request so I had to decode it from utf8 first and then get the parameter value using

URLDecoder.decode(request.getQueryString(), "UTF-8")

and then pass it to the query and I used prepared statement which encodes it according to the connection properties as you mentioned

Thank you