请问在JSP中,向sql2000中插入数据的中文化问题
我现在需要向MS SQLServer2000中,通过JSP提交的形式,提交数据给sqlServer服务器。我是通过jdbcodbc桥形式连接的,但是插入英文毫无问题,中文则为问号。如果在JavaBean测试中,提交中文也没有问题,我把源码贴上,请高手指教,如何修改,最好能有简单的代码示例,多谢。
提交网页:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p>测试向SQL数据库中回填数据</p>
<form name="form1" method="post" action="Result.jsp">
<table width="48%" border="1" align="center">
<tr>
<td width="26%">
<div align="left">用户名字</div>
</td>
<td width="74%">
<div align="left">
<input type="text" name="username">
</div>
</td>
</tr>
<tr>
<td width="26%">用户住址</td>
<td width="74%">
<div align="left">
<input type="text" name="address">
</div>
</td>
</tr>
</table>
<p align="center">
<input type="submit" name="Submit" value="Submit">
</p>
</form>
<p> </p>
</body>
</html>
JavaBean源码:
package mytest;
/**
* Title: ContactSQLServer
* Description: 此项目主要用于测试向SQL Server数据库中回填数据时,产生的中文乱码问题,测试环境为MS SQLServer2000
* Copyright: Copyright (c) 2001
* Company: Tsinghua University
* @author Sunny
* @version 1.0
*/
import java.sql.*;
import java.util.Properties;
public class ContactSQLBean {
/**
* 此Bean通过向SQLServer2000中,测试用数据库TestDB写入
* 两个字段(username, address)写入数据,测试回填的中文
* 化问题
*/
private String username;
private String address;
Connection conn;
PreparedStatement prepStmt;
public void setusername(String username) {
this.username = username;
}
public void setaddress(String address) {
this.address = address;
}
public static void main(String args[]) {
ContactSQLBean test = new ContactSQLBean();
test.insertUserInfo("孙阳", "清华大学");
test.closeDB();
}
/**
* 在构造函数中对conn对象进行初始化
*/
public ContactSQLBean() {
String url = "jdbc:odbc:TestDB";
Properties props = new Properties();
props.setProperty("username", "sa");
props.setProperty("password", "");
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(url, props);
conn.commit();
}
catch(Exception e) {
System.out.println("Connect Error " + e.getMessage());
}
}
/**
* 中断数据库连接
*/
public void closeDB() {
try{
conn.close();
}catch(Exception e) {
e.printStackTrace();
}
}
/**
* 向tab_user_info表中插入记录
*/
public boolean insertUserInfo(String username, String address){
String insertString ="insert into sunny.testdb.dbo.tab_user_info " +
"values(?, ?)";
String gbusername; //编码后的username
String gbaddress; //编码后的address
try {
gbusername = new String(username.getBytes("GB2312"), "ISO8859_1");
prepStmt = conn.prepareStatement(insertString);
//对字符串编码与否,在数据库中都是一样的乱码
prepStmt.setString(1, gbusername);
prepStmt.setString(2, address);
prepStmt.executeUpdate();
conn.commit();
return true;
}catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
return false;
}
}
}
JSP网页为:
<%@ page language = "java" %>
<%@ page contentType = "text/html;charset=gb2312" %>
<jsp:useBean id = "SqlBean" scope = "session" class = "mytest.ContactSQLBean" />
<%! String username, address; %>
<%
username = new String(request.getParameter("username").getBytes("GB2312"),
"ISO8859_1");
address = new String(request.getParameter("address").getBytes("GB2312"),
"ISO8859_1");
%>
<HTML>
<HEAD>
<TITLE>
Result
</TITLE>
</HEAD>
<BODY>
<H1>
<%
if (SqlBean.insertUserInfo(username, address) == true){
out.print("修改数据成功");
}else {
out.print("修改数据不成功");
}
%>
</H1>
</BODY>
</HTML>