jsp+mysql 在 preparedStatement中 中文乱码解决

直接上代码:

[java]  view plain copy
  1. System.out.println(username+password);  
  2. String sql = "select * from user where name =? and password  =?";             
  3. preparedStatement = connection.prepareStatement(sql);  
  4. preparedStatement.setString(1,"中文乱码");  
  5. preparedStatement.setString(2,password);  
  6. System.out.println(preparedStatement.toString());  

这样在后台输出sql语句是:com.mysql.jdbc.JDBC4PreparedStatement@5009ea: select * from user where name ='??' and password  ='123'

中文是??。


解决方法:原因是设置datasource 的driver 时jdbc.url=jdbc:mysql://localhost:3306/shoppiong 没有指定编码

改成:

[java]  view plain copy
  1. private final static String URL = "jdbc:mysql://localhost/shoppingstyle=?characterEncoding=utf8";  

 这样在重新运行程序,后台输出sql语句为:com.mysql.jdbc.JDBC4PreparedStatement@c07125: select * from user where name ='张山' and password  ='123'

解决了乱码问题

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值