1

I have MySQL database, where I have saved data and some words have diacritics.

This is my function how to get data from database.

public List<RowType> getData(String query){

    List<RowType> list = new ArrayList<>();
    try{
        connect();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(query);

        while(resultSet.next()){
            StringBuilder str = new StringBuilder();
            for(int i = 1; i <= getCountColumns(resultSet); i++){
                if(i==1) str.append(resultSet.getString(i));
                else str.append("," + resultSet.getString(i));
            }
            list.add(new RowType(str.toString()));
        }
    }
    catch(Exception e){
        System.out.println("Chyba při získavání údajů z databáze.");
        System.out.println(e);
        Console.print("Chyba při získavání údajů z databáze.");
        Console.print(e.toString());
    }
    finally{
        disconnect();
    }
    return list;
}

As parameter i send this query.

List<RowType> list = connection.getData("Select id from countries where name = 'Česko'");

But it doesn´t find anything, because i have diacritic in the query ("Česko"). I try it without diacritic and it works. So don´t you know how to fix it to work with accents too?

Martin
  • 27
  • 4
  • What is your current mysql connection String? – Elliott Frisch Mar 11 '19 at 23:01
  • 1
    Do not concatenate user entered strings into a SQL statement. Use `PreparedStatement` to prevent SQL syntax errors and [SQL Injection](https://en.wikipedia.org/wiki/SQL_injection) attacks. – Andreas Mar 11 '19 at 23:03
  • I used PreparedStatement and it works when i send to query "Cesko" so it find id of "Česko", but when i send to query "Česko" so it find nothing. – Martin Mar 11 '19 at 23:28
  • Elliot, what do you mean current mysql connection string? – Martin Mar 11 '19 at 23:29
  • @Martin It was [`"jdbc:mysql://localhost/sportdata"`](https://stackoverflow.com/questions/55111501/problem-with-diacritic-getting-data-from-database-java#comment96983828_55113078) – Elliott Frisch Mar 12 '19 at 16:05

1 Answers1

0

Can you try to add a few more queries before executing your main query? so it will look something like:

connect();
Statement statement = connection.createStatement();

String query2 = "SET NAMES 'utf8'";
statement.execute(query2);

query2 = "SET CHARACTER SET 'utf8'";
statement.execute(query2);

ResultSet resultSet = statement.executeQuery(query);

if the above does not work for you, then maybe there is an issue with your database settings; if that's the case, you can refer to the answer here

Kevin fu
  • 222
  • 1
  • 4
  • 16
  • Thank you, the link, which you provided, was helpful. I used following syntax and it works. `connection = DriverManager.getConnection("jdbc:mysql://localhost/sportdata?characterEncoding=utf-8","root","");` – Martin Mar 12 '19 at 12:12
  • I added to my connection "characterEncoding=utf-8" and it. – Martin Mar 12 '19 at 12:15