请教一个JSP连接mysql数据库的乱码问题

icarusliu81 2008-05-14 06:09:21
下面这段代码,如果没有红色部分的话,能正常运行,并打印出数据库中的数据,但当有红色部分的时候却有错,我试了好多的编码方式,都不行,哪位达人帮忙看下。
<%@page contentType="text/html; charset=gb2312" %>
<%@page import="java.sql.*" %>
<%
Connection conn=null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost:3306/plane1?useUnicode=true&characterEncoding=gb2312";
String user = "root";
String password = "1";
conn = DriverManager.getConnection(url,user,password);
Statement st = conn.createStatement();

String sql = "select * from planinfo where from = '北京'";
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
out.print(rs.getString(2));
}
%>
...全文
122 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
kui1015 2008-05-15
  • 打赏
  • 举报
回复
能用from作字段,你也是开天辟地之人了,呵呵
icarusliu81 2008-05-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zuoguodang 的回复:]
是不是from的关系啊,from是关键字啊,编码应该没问题了,如果是编码问题就换utf-8
[/Quote]
汗。。。丢人了。。。from是关键字。。。解决解决。呵呵
icarusliu81 2008-05-14
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zhangxu3739 的回复:]
引用 2 楼 zongjianhui 的回复:
不是中文问题吧,你装数据库的时候,编码是什么啊



安装的时候最好是GB2312或者GBK
[/Quote]

装的时候用的是UTF-8有问题么?那我试一下GBK的看一下
icarusliu81 2008-05-14
  • 打赏
  • 举报
回复
多谢名位的回答,但问题还没有解决,注意如果将北京改成英文单词的话执行成功,而且页面,数据库,等编码我都用的是UTF-8
此电脑 2008-05-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zongjianhui 的回复:]
不是中文问题吧,你装数据库的时候,编码是什么啊
[/Quote]


安装的时候最好是GB2312或者GBK
icarusliu81 2008-05-14
  • 打赏
  • 举报
回复
装的时候是UTF-8的
sheep49 2008-05-14
  • 打赏
  • 举报
回复
试试这个

你的DBURL这么写看看

String DBURL = "jdbc:mysql://localhost:3306/DataBase?useUnicode=true&characterEncoding=GB2312";

应为mysql的JDBC驱动默认都是ISO-8859-1编码 这里如果要正确显示 需要转换
lixq2000 2008-05-14
  • 打赏
  • 举报
回复
select * from planinfo where from = '北京'
from是关键字,怎么可以from=' 北京呢'?
FL1429 2008-05-14
  • 打赏
  • 举报
回复
http://hi.baidu.com/lovekaili/blog/item/f250b2c74fe808d8d000601e.html
canyangruxie 2008-05-14
  • 打赏
  • 举报
回复
把where from = '北京'改成where `from` = '北京'试试!
把from 用反引号包围!
canyangruxie 2008-05-14
  • 打赏
  • 举报
回复
在select * from planinfo where from = '北京'后面加上分号试试!
zuoguodang 2008-05-14
  • 打赏
  • 举报
回复
是不是from的关系啊,from是关键字啊,编码应该没问题了,如果是编码问题就换utf-8
zongjianhui 2008-05-14
  • 打赏
  • 举报
回复
不是中文问题吧,你装数据库的时候,编码是什么啊
icarusliu81 2008-05-14
  • 打赏
  • 举报
回复
报错情况如下:
javax.servlet.ServletException: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from = '±±¾©'' at line 1

81,094

社区成员

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

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