jsp页面中访问数据库(ACCESS)的乱码问题,有待您的解决!!!

pipilu1978 2003-08-19 11:54:15
我做了一个留言板,在留言板中的留言要是用中文的话就回出现数据库中的数据乱码的问题,如果用英文问题就没有,我想知道这应该怎么解决?请高手不吝赐教!!!最好讲解清楚点,兄弟是初学!!!谢谢!!!
...全文
95 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
pipilu1978 2003-08-21
  • 打赏
  • 举报
回复
谢谢大家的关注!我的问题是用在留言本中留言的时间,往数据库里添加的时间出的问题,数据库中显示为乱码,提交以后的页面显示也为乱码!我把处理数
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>向表中添加记录</title>
</head>
<body>
<%
String name=(request.getParameter("name");
//name = new String(name.getBytes("gb2312"),"GBK");
String mail=request.getParameter("mail");
//mail = new String(mail.getBytes("gb2312"),"GBK");
String title=(request.getParameter("title");
//title = new String(title.getBytes("gb2312"),"GBK");
String content=(request.getParameter("content");
//content = new String(content.getBytes("gb2312"),"GBK");
if(name==null)
name="";
if(title==null)
title="";
if(content==null)
content="";
if(mail==null)
mail="";
if(name.length()==0)
out.println("留言人姓名不能为空!");
else
if(title.length()==0)
out.println("留言主题不能为空!");
else
if(content.length()==0)
out.println("留言内容不能为空!");
else
{
java.util.Date date=new java.util.Date();
String datetime=new Timestamp(date.getTime()).toString();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:user_db";
Connection con=DriverManager.getConnection(url,"","");
PreparedStatement stm=con.prepareStatement("insert into message values(?,?,?,?,?)");
stm.setString(1,name);
if(mail.length()==0)
stm.setString(2,null);
else
stm.setString(2,mail);
stm.setString(3,title);
stm.setString(4,datetime);
stm.setString(5,content);
try
{
stm.executeUpdate();
}catch(Exception e)
{}
con.close();
}
catch(Exception e)
{}
%>
<jsp:forward page="view_message.jsp"/>
<%
}
%>
</body>
</html>据库的页面贴出来,希望大家帮忙!
djlzxzy 2003-08-20
  • 打赏
  • 举报
回复
加上这一句<%@ page language="java" contentType="text/html;charset=gb2312" %>应该就没问题了!
betterjonah 2003-08-20
  • 打赏
  • 举报
回复
str =new String(request.getParameter("showword").getBytes("iso8859-1"),"gb2312");

如果还不行,将你的sql语句页转换一下编码形式
JustBT 2003-08-20
  • 打赏
  • 举报
回复
你的情况是不是说网页里可以正常显示,而数据库里的不行?
对于中文处理的常见方法,常见的有2种:
在文档的首行加入
代码:
<%@ page contentType=”text/html;charset=gb2312” %>

或者处理单个的中文输出
代码:
<%
String test=”测试”;
Byte [] tmbyte=test.getBytes(“ISO8859_1”);
Test=new String(tmbyte);
Out.print(test);
%>


大部分文档主要用第一种,这样可以避免大量的代码加在网页中。
在URL附带中文参数,采用第二种方法处理,例如:
代码:
String show1=new String(request.getParameter("showword").getBytes("iso8859-1"));


另外你可以到数据库里看一下能不能正常显示
最好把情况说清楚也好找原因啊
一大梨 2003-08-20
  • 打赏
  • 举报
回复
关注,我也出现了同样的问题!
zxdhaha 2003-08-20
  • 打赏
  • 举报
回复
public String getStr(String str)
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e)
{

}
return "null";
}
向数据库写的时候用这个函数转换一下
wellsoon 2003-08-20
  • 打赏
  • 举报
回复
request.setCharacterEncoding("GB2312");//设置编码
YuLimin 2003-08-20
  • 打赏
  • 举报
回复
有需要JDBC访问Access数据库的示例的话:
请到我的主页上面去下载:http://61.131.30.37/123/Develop/Java/JDBCAccess.zip
pipilu1978 2003-08-20
  • 打赏
  • 举报
回复
首先谢谢朋友的解答,但这个代码中已经包含了这句了,还是有这样的问题?

81,092

社区成员

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

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