这个问题在这里已经有答案了:
java preparedstatement utf-8字符问题(4个答案)
11天前关门了。
我使用的数据库有一个表,其中一个字段是用ipa字符编写的,例如一个字段是:/iː/ - /ɪ/
我正在做一个prepared语句,结果集是空的,不应该是空的(我检查了查询),当我打印prepared语句时,字符的编码有问题:
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\u 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
[]
我已经在函数中检查了string/iː/ - /ɪ/ 正确打印,但在执行准备好的语句时显示“/i?/-/?/”
有人知道为什么会这样,我该怎么解决?
事先非常感谢!
1条答案
按热度按时间fhity93d1#
使用marc链接到java preparedstatement utf-8字符问题,我用以下方法解决了这个问题:
事实上,有很多方法会把事情搞砸。如果您使用的是mysql,请尝试在jdbc连接url的末尾添加characterencoding=utf-8参数:
jdbc:mysql://服务器/数据库?characterencoding=utf-8