servlet mysql 乱码_Servlet+JDBC+MySQL乱码,该怎么解决

Servlet+JDBC+mysql乱码

通过Get方式提交参数,url:

http://localhost:8080/Minisite/light?nick="aaa"&mobile=""&prov="江西"&city="景德镇"&game="对对碰"

Servlet里面的doGet代码:

java code

String nick = new String(req.getParameter("nick").getBytes("ISO-8859-1"),"UTF-8"); String mobile = new String(req.getParameter("mobile").getBytes("ISO-8859-1"),"UTF-8"); String prov = new String(req.getParameter("prov").getBytes("ISO-8859-1"),"UTF-8"); String city = new String(req.getParameter("city").getBytes("ISO-8859-1"),"UTF-8"); String game = new String(req.getParameter("game").getBytes("ISO-8859-1"),"UTF-8"); LightMap lm = new LightMap(); lm.insertLightRecord(nick, prov, city, game, mobile);

欢迎大家阅读《Servlet+JDBC+MySQL乱码,该怎么解决》,跪求各位点评,by 搞代码

数据库采用的是utf8编码。

如果将Servlet的doGet代码改成

Java code

String mobile = new String(req.getParameter("mobile").getBytes("ISO-8859-1"),"GBK");

那么这个字段中文就不会乱码

请问哪位能帮帮我。在线等。谢谢!(新手,如果可以的话能不能讲的稍微详细一点)

------解决方案--------------------

第一:调试查看从url得到的中文数据是否是乱码。注意tomcat的request提取方法,并不是所有servlet容器都这样子处理!

第二:查看数据库jdbc的url字符串是否指定了正确的编码

------解决方案--------------------

http://blog.csdn.net/ACMAIN_CHM/archive/2009/05/12/4174186.aspx

MySQL 中文显示乱码

------解决方案--------------------

你用的是tomcat吗?在conf目录下的server.xml文件中的connector节点加上URIEncoding="gbk"

------解决方案--------------------

String mobile = new String(req.getParameter("mobile").getBytes("ISO-8859-1"),"GBK");

你这里不是转换成GBK就不乱码了么

e7ce419cf2d6ad34d01da2ceb8829eed.png

微信 赏一包辣条吧~

023a57327877fb4402bcc76911ec18ea.png

支付宝 赏一听可乐吧~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值