主题:JSP连Access乱码问题!
最近用NetBeans 5.0写JSP,常常发现一个问题,就是每次输出汉字的时候,编译器总会出现:
D:\TestJsp\Myweb\build\generated\src\org\apache\jsp\liuyan_jsp.java:75: 警告:编码 GBK 的不可映射字符
out.println("<h1 align=center>杩樻病鏈夌暀瑷?淇℃伅鍙互鏌ョ湅</h1><br>");
这种乱码,但是网页上还是正常显示的,所以一直没有在意,今天在连接Access的时候
却发现数据库存的也是乱码,不能正常读出来,来请教各位如何解决呢
文件如下:
[color=FF0000]<%@page contentType="text/html"%><!--设置类型 -->
<%@page pageEncoding="UTF-8"%><!--设置编码-->
<%@page import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<jsp:useBean id="liuyan" class="bean.AccBean" scope="page"/>
<%
request.setCharacterEncoding("GBK");
String sname = request.getParameter("txtname");
String sinfo = request.getParameter("txtinfo");
String sip = request.getLocalAddr();
Statement stat = liuyan.getStatement();
String sql = "insert into liuyaninfo(name,info,ip)values(" + "'"+sname+"'," +
"'"+sinfo+"'," + "'"+sip+"'" + ")";
int i = stat.executeUpdate(sql);
if(i > 0){
response.sendRedirect("liuyan.jsp");
}
liuyan.close();
%>
</body>
</html>[/color]
AccBean中的代码是:
[color=FF0000]package bean;
import java.sql.*;
public class AccBean {
Connection con;
String url;
String id,pwd;
Statement stat;
public AccBean() {
}
public Statement getStatement(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
url = "jdbc:odbc:liuyan";
con = DriverManager.getConnection(url);
stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}catch(Exception e){
e.printStackTrace();
}
return stat;
}
public void close(){
try{
con.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
}</color>
数据库中的内容是:
[img]http://58.60.9.140/cgi-bin/dl/544B38D2796758A06BE4C7D80C87A34DD0C315DAECE733D563062AF6EF2DB010F4C9453B98435925EACF1519003F5B6B4E254B11299F48530FA4AD9E11EAA6C3FB0745BA696C57A400DE97554979A33B0B4D424AE86FBBC45769/123.jpg[/img]
另外还有个问题,在JSP中不能像JAVA一样在连接sqlserver时,在项目中导入含有3个jar包的类库吗? 我每次用jsp连接sqlserver时总出异常,如果不是的话,请高手解答下怎么做!
谢谢 !
D:\TestJsp\Myweb\build\generated\src\org\apache\jsp\liuyan_jsp.java:75: 警告:编码 GBK 的不可映射字符
out.println("<h1 align=center>杩樻病鏈夌暀瑷?淇℃伅鍙互鏌ョ湅</h1><br>");
这种乱码,但是网页上还是正常显示的,所以一直没有在意,今天在连接Access的时候
却发现数据库存的也是乱码,不能正常读出来,来请教各位如何解决呢
文件如下:
[color=FF0000]<%@page contentType="text/html"%><!--设置类型 -->
<%@page pageEncoding="UTF-8"%><!--设置编码-->
<%@page import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<jsp:useBean id="liuyan" class="bean.AccBean" scope="page"/>
<%
request.setCharacterEncoding("GBK");
String sname = request.getParameter("txtname");
String sinfo = request.getParameter("txtinfo");
String sip = request.getLocalAddr();
Statement stat = liuyan.getStatement();
String sql = "insert into liuyaninfo(name,info,ip)values(" + "'"+sname+"'," +
"'"+sinfo+"'," + "'"+sip+"'" + ")";
int i = stat.executeUpdate(sql);
if(i > 0){
response.sendRedirect("liuyan.jsp");
}
liuyan.close();
%>
</body>
</html>[/color]
AccBean中的代码是:
[color=FF0000]package bean;
import java.sql.*;
public class AccBean {
Connection con;
String url;
String id,pwd;
Statement stat;
public AccBean() {
}
public Statement getStatement(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
url = "jdbc:odbc:liuyan";
con = DriverManager.getConnection(url);
stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}catch(Exception e){
e.printStackTrace();
}
return stat;
}
public void close(){
try{
con.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
}</color>
数据库中的内容是:
[img]http://58.60.9.140/cgi-bin/dl/544B38D2796758A06BE4C7D80C87A34DD0C315DAECE733D563062AF6EF2DB010F4C9453B98435925EACF1519003F5B6B4E254B11299F48530FA4AD9E11EAA6C3FB0745BA696C57A400DE97554979A33B0B4D424AE86FBBC45769/123.jpg[/img]
另外还有个问题,在JSP中不能像JAVA一样在连接sqlserver时,在项目中导入含有3个jar包的类库吗? 我每次用jsp连接sqlserver时总出异常,如果不是的话,请高手解答下怎么做!
谢谢 !