问题: prepareStatement LIKE在MySQL中可以输出结果,但是用java写就查找不到结果。
public void getYunEventListByKw(String content) { PreparedStatement preStmt = null; ResultSet rs = null; Connection conn = null; try { conn = dbHelper.getYunshanConnection(); //**重点内容** String sql = "SELECT * FROM yunshan_event WHERE event_summary LIKE ?"; preStmt = conn.prepareStatement(sql); preStmt.setString(1, "%"+content+"%"); System.out.println(content); rs = preStmt.executeQuery(); System.out.println(preStmt.toString()); //*** while (rs.next()) { int eventId = rs.getInt("id"); } } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { dbHelper.closeAll(conn, preStmt, rs); } return null; }我一直搜索不出结果,看了最后我打印出来那个最后的sql语句。 竟然是这个样子:
com.mysql.jdbc.JDBC4PreparedStatement@5dbe6544: SELECT * FROM yunshan_event WHERE event_summary LIKE '%??%'原来是因为jdbc连接的时候没有设置utf8的编码,所以会乱码,一直搜索不到内容.
String DB_URL = "jdbc:mysql://192.168.235.36:3306/fig?zeroDateTimeBehavior=round&characterEncoding=utf8";搞定。
新闻热点
疑难解答
图片精选