0

I'm working with a database that has one table with a field that is wrote with IPA characters, for example one field is: /iː/ - /ɪ/

I'm doing a prepared statement and the result set is empty and it shouldn't (I've checked the query) and when I print the prepared statement there is a problem with the encoding of the characters:

com.mysql.cj.jdbc.ClientPreparedStatement: SELECT Id, sound, pair, IPA, writed_word, pair_name FROM minimal_pair WHERE dismised <> 1 AND pair_name = '/i?/ - /?/'

The pair_name value should be: /iː/ - /ɪ/

The method code to query the database is:

public static ArrayList<Word> returnRandomWordsFromPair(String pair_name) {

        ArrayList<Word> pairNameList = new ArrayList<Word>();

        // Connection variables
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        // Calling the connection
        conn = Dataconn.conn();

        String sql = "SELECT Id, sound, pair, IPA, writed_word, pair_name FROM minimal_pair WHERE dismised <> 1 AND pair_name = ?";
        System.out.println(sql);
        try {
            pstmt = conn.prepareStatement(sql);
            // The next line are the parameters
            pstmt.setString(1, pair_name);
            System.out.println(pstmt);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Word word = new Word();
                word.setId(rs.getInt("Id"));
                word.setSound(rs.getString("sound"));
                word.setPair(rs.getString("pair"));
                word.setIpa(rs.getString("IPA"));
                word.setWrited_word(rs.getString("writed_word"));
                word.setPair_name(rs.getString("pair_name"));
                
                pairNameList.add(word);
                System.out.println(word);
            }

            if (rs != null)
                System.out.println("result set is null");
                try {
                    rs.close();
                } catch (SQLException logIgnore) {
                }
            if (pstmt != null)
                System.out.println("prepared statement is null");
                try {
                    rs.close();
                } catch (SQLException logIgnore) {
                }
            if (conn != null)
                System.out.println("connection is null");
                try {
                    rs.close();
                } catch (SQLException logIgnore) {
                }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (rs != null)
                try {
                    rs.close();
                } catch (SQLException logIgnore) {
                }
            if (pstmt != null)
                try {
                    rs.close();
                } catch (SQLException logIgnore) {
                }
            if (conn != null)
                try {
                    rs.close();
                } catch (SQLException logIgnore) {
                }
        }

        Dataconn.closeConn(conn);

        return pairNameList;
    }

And I'm calling it from this code:

public static void main(String[] args) {

        ArrayList<Word> wordList = new ArrayList<Word>();

        wordList = DatabaseMethods.returnRandomWordsFromPair("/iː/ - /ɪ/");

        String name = wordList.getClass().getSimpleName();
        System.out.println(name);

        System.out.println(wordList);
        
        for (Word smallWord : wordList)
          {               
               System.out.println(smallWord.toString());        
          }
    }

The standard output result is:

SELECT Id, sound, pair, IPA, writed_word, pair_name FROM minimal_pair WHERE dismised <> 1 AND pair_name = ?
com.mysql.cj.jdbc.ClientPreparedStatement: SELECT Id, sound, pair, IPA, writed_word, pair_name FROM minimal_pair WHERE dismised <> 1 AND pair_name = '/i?/ - /?/'
result set is null
prepared statement is null
connection is null
Connection closed
ArrayList
[]

I have checked that in the function the String /iː/ - /ɪ/ is correctly printed but when doing the prepared statement there is shown '/i?/ - /?/'

Somebody has any idea why is this happening and how may I resolve it?

Thank you very much in advance!

RandomGuy9
  • 21
  • 3
  • This might help https://stackoverflow.com/questions/3828818/java-preparedstatement-utf-8-character-problem – Marc Dec 28 '20 at 05:24
  • See "question mark" in https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Dec 28 '20 at 06:18

1 Answers1

1

Using Marc's link to Java PreparedStatement UTF-8 character problem I've solve the problem using this:

The number of ways this can get screwed up is actually quite impressive. If you're using MySQL, try adding a characterEncoding=UTF-8 parameter to the end of your JDBC connection URL:

jdbc:mysql://server/database?characterEncoding=UTF-8

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
RandomGuy9
  • 21
  • 3