| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1321 人关注过本帖
标题:[求助]jsp+sql server2000中文乱码问题
只看楼主 加入收藏
kingwen8174
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2005-12-21
收藏
 问题点数:0 回复次数:1 
[求助]jsp+sql server2000中文乱码问题

 小弟最近换了2000数据库,连接一切正常,但是从jsp页面向数据库中插入中文出现了???的乱码,看了网上很多文章,解释的都不好,问题依旧没有解决,希望坛子里的高手不吝赐教,先附上全部源代码.我的系统:winXP(sp2)+tomcat5.028+sql server2000(sp4)+jdk1.4.2+jdbc for sql server2000.

程序源代码如下:DataBaseConnection.java

 package com.jspdev.util;
import java.sql.*;
public class DataBaseConnection
{
public static Connection getConnection()
{
Connection con=null;
String CLASSFORNAME="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String SERVANDDB="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev";
String USER="kingwen";
String PWD="664711";
try
{

Class.forName(CLASSFORNAME);
con = DriverManager.getConnection(SERVANDDB,USER,PWD);
}
catch(Exception e)
{
e.printStackTrace();
}
return con;
}
}

MessageBean.java

package com.jspdev.ch9;
import com.microsoft.jdbc.sqlserver.SQLServerDriver;
import java.sql.*;
import java.util.*;

public class MessageBean
{
private Connection con;
MessageVO msg;

//获得数据库连接。
public MessageBean()
{
String CLASSFORNAME="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String SERVANDDB="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev";
String USER="kingwen";
String PWD="664711";
try
{

Class.forName(CLASSFORNAME);
con = DriverManager.getConnection(SERVANDDB,USER,PWD);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void setMessage(MessageVO msg)
{
this.msg=msg;
}
public void addMessage()throws Exception
{
try
{
PreparedStatement stm=con.prepareStatement("insert into message values(?,?,?,?,?)");
stm.setString(1,msg.getTitle());
stm.setString(2,msg.getName());
stm.setDate(3,new java.sql.Date(new java.util.Date().getTime()));
if((msg.getEmail()).length()==0)
stm.setString(5,null);
else stm.setString(5,msg.getEmail());
stm.setString(4,msg.getContent());
try
{
stm.executeQuery();
}
catch(Exception e)
{
}

con.close();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
public Collection getMessage()throws Exception
{
Collection ret=new ArrayList();
try
{
Statement stm=con.createStatement();
ResultSet result=stm.executeQuery("select count(*) from message");
int message_count=0;
if(result.next())
{
message_count=result.getInt(1);
result.close();
}
if(message_count>0)
{
result=stm.executeQuery("select * from message order by time desc");

while(result.next())
{
String title=result.getString("title");
String name=result.getString("name");
String mail=result.getString("mail");
String content=result.getString("content");
java.sql.Date date=result.getDate("time");
MessageVO message=new MessageVO();
message.setName(name);
message.setTitle(title);
message.setContent(content);
message.setDate(date);
message.setEmail(mail);
ret.add(message);
}
result.close();
stm.close();
}

con.close();

}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
return ret;
}
}

MessageVO.java

package com.jspdev.ch9;
public class MessageVO implements java.io.Serializable
{
private String name,email,title,content;
private java.sql.Date date;
public void setName(String name)
{
this.name=name;
}
public void setEmail(String email)
{
this.email=email;
}
public void setTitle(String title)
{
this.title=title;
}
public void setContent(String content)
{
this.content=content;
}
public String getName()
{
return this.name;
}

public String getContent()
{
return this.content;
}
public String getTitle()
{
return this.title;
}
public String getEmail()
{
return this.email;
}
public java.sql.Date getDate()
{
return this.date;
}
public void setDate(java.sql.Date date)
{
this.date=date;
}
}

jsp页面:index.html

<!-- index.html -->
<HTML>
<HEAD>
<TITLE> message board </TITLE>
</HEAD>
<LINK href="..\..\hellking.css" type=text/css rel=stylesheet>
<BODY>
<TABLE width="90%" align="center" border=1 cellspacing="0" cellpadding="0" bordercolordark="#CCCC99" bordercolorlight="#CCCC99">
<TR>
<TD align="center" bgcolor="#CCCC99">访客留言板</TD>
</TR>
<TR>
<TD><TABLE align="center" border=1 cellspacing="0" cellpadding="0" bordercolordark="#CCCC99" bordercolorlight="#CCCC99">
<FORM ACTION="addMessage.jsp">
<TR>
<TD>姓名:</TD>
<TD bgcolor="#CCCC99"><input type="text" name="name" size=25></TD>
</TR>
<TR>
<TD bgcolor="#CCCC99">E-mail:</TD>
<TD bgcolor="#CCCC99"><input type="text" name="email" size=25></TD>
</TR>
<TR>
<TD>主题:</TD>
<TD bgcolor="#CCCC99"><input type="text" name="title" size=25></TD>
</TR>
<TR>
<TD valign="top" bgcolor="#CCCC99">留言:</TD>
<TD><textarea name="content" rows=7 cols=25></textarea></TD>
</TR>
<TR>
<TD colspan=3><TABLE align="center" width="100%" cellspacing="0" cellpadding="0" bordercolordark="#CCCC99" bordercolorlight="#CCCC99">
<TR>
<TD align="center" bgcolor="#CCCC99"><input type="submit" value="提交留言"></TD>
<TD align="center"><a href="viewMessages.jsp"><font size=2>查看留言</font></a></TD>
<TD align="center" bgcolor="#CCCC99"><input type="reset" value="重新填写"></TD>
</TR>
</TABLE></TD>
</TR>
</FORM>
</TABLE></TD>
</TR>
</TABLE>
</BODY>
</HTML>

addMessage.jsp

<%@ page pageEncoding="gb2312" %>
<%@ page import="java.sql.*,com.jspdev.ch9.*,java.util.*,java.sql.*" errorPage="../error.jsp"%>
<jsp:useBean id="messageVo" class="com.jspdev.ch9.MessageVO" scope="page">
<jsp:setProperty name="messageVo" property="*"/>
</jsp:useBean>
<jsp:useBean id="messageBean" class="com.jspdev.ch9.MessageBean" scope="page"/>
<HTML>
<HEAD>
<TITLE>add message into table </TITLE>
</HEAD>
<BODY>
<%
try
{

messageBean.setMessage(messageVo);
messageBean.addMessage();
}
catch(Exception e)
{
e.printStackTrace();
}
%>
<jsp:forward page="viewMessages.jsp" />
</body>
</html>

viewMessages.jsp

<%@ page import="java.sql.*,com.jspdev.ch9.*,java.util.*,java.sql.*"%>
<jsp:useBean id="messageBean" class="com.jspdev.ch9.MessageBean" scope="page"/>
<HTML>
<HEAD>
<TITLE> show the message in the table </TITLE>
</HEAD>
<LINK href="..\..\hellking.css" type=text/css rel=stylesheet>
<BODY>
<p align="center">所有访客留言</p>
<hr>
<%
int message_count=0;
Collection messages=messageBean.getMessage();
Iterator it=messages.iterator();
while(it.hasNext())
{
MessageVO message=(MessageVO)it.next();
%>
<TABLE width="100%" align="center" border=1 brcolor=99ccff cellspacing="0" cellpadding="0" bordercolordark="#000000" bordercolorlight="#CCCC99">
<tr><td bgcolor="#CCCC99"><font size=2>主题:</font></td>
<td colspan=3><%=message.getTitle()%></td></tr>
<tr><td bgcolor="#CCCC99"><font size=2>留言人:</font></td>
<td><%=message.getName()%></td><td bgcolor="#CCCC99"><font size=2>E-mail:</font></td>
<td>
<%
out.println("<a href=mailto:"+message.getEmail()+">"+message.getEmail()+"</a>");
%>
</td></tr>
<tr><td bgcolor="#CCCC99"><font size=2>留言时间:</font></td><td colspan=3>
<%
out.println("<font size=2>"+message.getDate().toLocaleString()+"</font>");
%>
</td></tr>
<tr><td align="center">
<%
out.println("("+message_count+")");
%>
</td>
<td colspan=3><%=message.getContent()%>
</td></tr>
</table>
<%
out.println("<hr>");
message_count++;
}
%>
<p align="center"><a href="index.html">我要留言</a></p>
</body>
</html>


搜索更多相关主题的帖子: jsp sql 乱码 中文 
2005-12-21 03:18
jeremydomett
Rank: 2
等 级:新手上路
威 望:3
帖 子:312
专家分:0
注 册:2004-6-10
收藏
得分:0 
stm.setString(1,msg.getTitle());
在这里你可以先对msg.getTitle()的值进行编码(ISO-8859-1)
应该可以的.

热爱编程........永不放弃.........
2005-12-21 08:27
快速回复:[求助]jsp+sql server2000中文乱码问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017738 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved