我正在使用一个数据库,该数据库有一个表,其中一个字段是用 IPA 字符编写的,例如一个字段是:/iː/ - /ɪ/
我正在做一个准备好的语句,结果集是空的,它不应该(我检查了查询),当我打印准备好的语句时,字符的编码有问题:
com.mysql.cj.jdbc.ClientPreparedStatement: SELECT Id, sound, pair, IPA, writed_word, pair_name FROM minimal_pair WHERE dismised <> 1 AND pair_name = '/i?/ - /?/'
pair_name 值应该是:/iː/ - /ɪ/
查询数据库的方法代码为:
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;
}
我从这段代码中调用它:
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());
}
}
标准输出结果为:
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ː/ - /ɪ/ 是否正确打印,但是在执行准备好的语句时,会显示 '/i?/ - /?/'
有人知道为什么会发生这种情况,我该如何解决?
非常感谢您!