JDBC在mysql中查询中文返回结果为空

hbxtght 2012-04-26 06:55:31
RT
我的mysql设置的编码为utf8, 在java里边连接数据库的链接写的是:
String DBURL = "jdbc:mysql://localhost:3306/store_and_coupon_info?useUnicode=true&characterEncoding=utf8";
但是就是查询不出结果来,如果sql语句里边没中文就可以查询出来,救命啊- -
...全文
487 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
应该是字符串就应该用单引号引起来 '%小刘%'
hbxtght 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

我又测试了一下,发现直接用jdbc来查是没问题的,这个字符串是从android客户端发来的就有问题了
[/Quote]
没说清楚,我的意思是如果直接定义一个字符串去查询,是没有问题的,但是我这个storeName字符串是从android客户端接收来的,这样子就不行了,我尝试把这个字符串转码成utf8的:
new String(storeName.getBytes(), "utf8");

还是没成功....
  • 打赏
  • 举报
回复
中文字符要加 单引号引起来吧
hbxtght 2012-04-26
  • 打赏
  • 举报
回复
我又测试了一下,发现直接用jdbc来查是没问题的,这个字符串是从android客户端发来的就有问题了
sdojqy1122 2012-04-26
  • 打赏
  • 举报
回复
useUnicode=true&characterEncoding=utf-8
是不是少个 - 。
hbxtght 2012-04-26
  • 打赏
  • 举报
回复
哦,刚刚发的代码有些错误,ResultSet关闭之后,我又把它返回了,不过这个应该不影响查询
hbxtght 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

看看你的代码!
[/Quote]
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;

public class DBOperator {
private static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
private static final String DBURL = "jdbc:mysql://localhost:3306/store_and_coupon_info?useUnicode=true&characterEncoding=utf8";
private static final String USERNAME = "root";
private static final String PASSWORD = "ght";

public static ResultSet queryByStoreName(String storeName) {
//System.out.println(storeName);
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select storeName, storeType, storeAddress, longitude, latitude from store where storeName like ?";
try {
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL, USERNAME, PASSWORD);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "%" + storeName + "%");
rs = pstmt.executeQuery();

while (rs.next()) {
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
System.out.println(rs.getDouble(4));
System.out.println(rs.getDouble(5));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(-1);
} catch (SQLException e) {
e.printStackTrace();
System.exit(-1);
} finally {
try {
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return rs;
}
}
kankan231 2012-04-26
  • 打赏
  • 举报
回复
要不改成utf-8试试
古市轩 2012-04-26
  • 打赏
  • 举报
回复
看看你的代码!

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧