I have set my MariaDB to Default Character Set UTF-8. According to Blue Box Blog Character Set Hell, setting that will save me the problem from unrecognized accented character like 'é'.
However, I can't get some of the results with
SELECT title FROM tablename WHERE title = 'sometext'
where sometext need not necessary have accented characters.
Is there a unique approach to handle utf8 character set? Because it looks like it doesn't recognize string in java.
In Java Code
String query = "SELECT title FROM tablename WHERE title=?";
PreparedStatement pState = conn.getPreparedStatement(query);
ResultSet result;
String textResult = null;
pState.setString(1, "sométéxt");
result = pState.executeQuery();
if(result.next())
textResult= result.getString(1);
System.out.println(textResult);
Output:
null
Other approach
1)
String query = "SELECT title FROM tablename WHERE CONVERT(title USING utf8)=?";
2)
String query = "SELECT title FROM tablename WHERE title=?";
byte[] req = "sométéxt".getBytes("UTF-8");
pState.setBytes(1,req);
output
null
Problem faced before Char Set=utf8
Prior to the Character Set=utf8, my first setup of the database was with the default Character Set, latin1_swedish_ci. With my field 'tablename' datatype=VARBINARY(), the unrecognized accented can be solved with
SELECT * FROM tablename WHERE CONVERT(title USING utf8) = 'sometext'
However, because my database has more than 10 million rows. Using CONVERT function on the whole 'title' field makes the SQL query and Java programme run very slowly.