奇怪的乱码问题,急!
郁闷我都用了,把他转化为中文了,怎么插到数据库中还是乱代码??
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="conn" class="opendb.opendb" scope="page"/>
<jsp:useBean id="str" class="mybean.str" scope="page"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<link href="Style.css" rel="stylesheet" type="text/css">
</head>
<%
String name=(String)request.getParameter("name");
name=str.getstr(name);
String password=(String)request.getParameter("password");
password=str.getstr(password);
session.putValue("name",name);
String sql="insert tb_admin(name,password) values('"+name+"','"+password+"')";
int col=conn.executeUpdate(sql);
out.println("col:"+col);
%>
我的两个bean文件:
opendb.java
package opendb;
import java.sql.*;
public class opendb
{
String sdbdirver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=web_jsp";
String user="sa";
String password="xishui";
Connection conn=null;
ResultSet rs=null;
Statement stmt=null;
int col;
int del;
public opendb()
{
try{
Class.forName(sdbdirver);
}
catch(java.lang.ClassNotFoundException e){
System.err.println("opendb():"+e.getMessage());
}
}
public ResultSet executeQuery(String sql) throws Exception
{
try{
sql= new String(sql.getBytes("GBK"),"ISO8859_1");
conn=DriverManager.getConnection(url,user,password);
Statement
stmt=conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.out.println("executeQuery(sql)"+ex.getMessage());
}
return rs;
}
public int executeUpdate(String sql) throws Exception
{
sql= new String(sql.getBytes("GBK"),"ISO8859_1");
try{
conn=DriverManager.getConnection(url,user,password);
Statement
stmt=conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_SENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE);
col=stmt.executeUpdate(sql);
}
catch(SQLException ex){
System.out.println("executeUpdate(sql)"+ex.getMessage());
}
return col;
}
}
str.java 如下:
package mybean;
public class str
{
public static String getstr(String s)
{
try
{
byte[] a = s.getBytes("ISO-8859-1");
return new String(a, "gb2312");
}
catch(Exception exception)
{
return s;
}
}
}
我可以插入成功了,就是遇到中文的时候怎么还是出现乱码?????
大侠们帮助小弟下!!!!