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?

5
  • What is your current mysql connection String? Commented Mar 11, 2019 at 23:01
  • 1
    Do not concatenate user entered strings into a SQL statement. Use PreparedStatement to prevent SQL syntax errors and SQL Injection attacks. Commented Mar 11, 2019 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. Commented Mar 11, 2019 at 23:28
  • Elliot, what do you mean current mysql connection string? Commented Mar 11, 2019 at 23:29
  • @Martin It was "jdbc:mysql://localhost/sportdata" Commented Mar 12, 2019 at 16:05

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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","");
I added to my connection "characterEncoding=utf-8" and it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.