用下面的代码访问MYSQL时 英文数字时显示正常 中文显示乱码 是什么原因?我的MYSQL默认是GBK
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%
Connection conn;
ResultSet rs;
Statement sql;
try
{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
out.print("aaa");
}
try{
String url="jdbc:mysql://localhost/on?user=root&password=5321620&useUnicode=true&characterEncoding=gb2312";
conn=DriverManager.getConnection(url);
sql=conn.createStatement();
rs=sql.executeQuery("select * from myword");
out.print("<table border=1>");
while(rs.next()){
out.print("<tr>");
out.print("<td >"+rs.getString("id")+"</td>");
out.print("<td >"+rs.getString("name")+"</td>");
out.print("<td >"+rs.getString("bimeng")+"</td>");
out.print("<td >"+rs.getString("zhubei")+"</td>");
out.print("<td >"+rs.getString("dianhua")+"</td>");
out.print("<td >"+rs.getString("qita")+"</td>");
out.print("<td width=30> </td>");
out.print("<td > <a href='del.jsp?name2="+rs.getString("id")+"'>删除</a> </td>");
out.print("</tr>");
}
out.print("</table>");
conn.close();
}
catch(SQLException ie){
out.print("sf");
}
%>
添加数据是
<%@ page contentType="text/html;charSet=gb2312" %>
<%@ page import="java.sql.*" %>
<html>
<body>
<%!
public String codeToString(String str)
{
String s=str;
try{
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
}catch(Exception e){
return s;
}
}
%>
<%
String is1=codeToString(request.getParameter("input1"));
if(is1==null)
is1="";
String is2=codeToString(request.getParameter("input2"));
if(is2==null)
is2="";
String is3=codeToString(request.getParameter("input3"));
if(is3==null)
is3="";
String is4=codeToString(request.getParameter("input4"));
if(is4==null)
is4="";
String is5=codeToString(request.getParameter("input5"));
if(is5==null)
is5="";
String is6=codeToString(request.getParameter("input6"));
if(is6==null)
is6="";
%>
<%
String age2;
age2="insert into myword(id,name,bimeng,zhubei,dianhua,qita)"+"values('"+is1+"','"+is2+"','"+is3+"','"+is4+"','"+is5+"','"+is6+"')";
Connection conn;
Statement sql;
ResultSet rs;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
out.print("not class no");
}
try{
String url="jdbc:mysql://localhost/on?";
conn=DriverManager.getConnection(url,"root","5321620");
sql=conn.createStatement();
sql.executeUpdate(age2);
conn.close();
}catch(SQLException ie){
out.print("no sql ");
}
%>
</body>
</html>