不想麻烦大家..只是真的搞到头大了..查了帖子也解决不了,为什么代码1(某书光盘中的)往SQL插入中文数据就完全正常,而代码2(我改了数据库名,数据类型全是CHAR,还改了些变量)...插入中文时就有些会乱码了,比如产品的"产"变成?号..
还有就是代码1中的String SQL = "insert Single_tab (SName,SSex,SIDCred,SAge,Snationality,SUnit,SPhone,SMemo) "+
"values('"+name+"',"+sex+","+idcred+","+age+",'"+nationality+"','"+unit+"','"
+phone+"','"+memo+"')";
为什么sex,idcred,age不用单引号呢?代码2要是不用的话就插入不成功了..代码1:
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="GB2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%request.setCharacterEncoding("GB2312");%>
<%!
public Connection Con() {
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection Con = DriverManager.getConnection("jdbc:odbc:db_database05","sa","sa");
return Con;
}
catch(Exception e)
{
return null;
}
}

public int InsertSQL(String name,String sex,String idcred,String age,
String nationality,String unit,String phone,String memo){ try
{
String SQL = "insert Single_tab (SName,SSex,SIDCred,SAge,Snationality,SUnit,SPhone,SMemo) "+
"values('"+name+"',"+sex+","+idcred+","+age+",'"+nationality+"','"+unit+"','"
+phone+"','"+memo+"')";
//VALUES('A',B,C,D,'E','F','G','H');
Connection Con = Con();
Statement Smt = Con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
int UpdateCount = Smt.executeUpdate(SQL);
return UpdateCount;
}
catch(SQLException e)
{
System.out.println("记录插入失败!");
return 0;
}
}
%>
...
...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <base href="<%=basePath%>">
    
    <title>单条数据录入</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->    <style type="text/css">
<!--
.style1 {
font-size: 14px;
font-weight: bold;
color: #33FF00;
}
.style2 {
font-size: 12px;
color: #663399;
}
.style4 {font-size: 12}
.style5 {color: #FF0000}
-->
    </style>
  </head>
   <body> 
    <form name="form" method="post" action="/myjsp/WebRoot/index.jsp" onsubmit="if (checkEmpty(form)) { <%
    InsertSQL(request.getParameter("name"),request.getParameter("sex"),request.getParameter("idcred"),
     request.getParameter("age"),request.getParameter("nationality"),
     request.getParameter("unit"),request.getParameter("phone"),
     request.getParameter("memo"));
    %>}">
......
  </form>
    <br>
  </body>
</html>
代码2:
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="GB2312"%>
<%request.setCharacterEncoding("GB2312");%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%!
public Connection Con() {
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection Con = DriverManager.getConnection("jdbc:odbc:ylxlam","sa","sa");
return Con;
}
catch(Exception e)
{
return null;
}
}

public int InsertSQL(String id,String name,String abc,String spec,
String sort,String unit,String price,String stock){ try
{
String SQL = "insert product (pID,pName,pShort,pSpec,pSort,pUnit,pPrice,pStock)"+ 
"values('"+id+"','"+name+"','"+abc+"','"+spec+"','"+sort+"','"+unit+"','"+price+"','"+stock+"')";
Connection Con = Con();
Statement Smt = Con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
int UpdateCount = Smt.executeUpdate(SQL);
return UpdateCount;
}
catch(SQLException e)
{
System.out.println("记录插入失败!");
return 0;
}
}
%>
...
...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <base href="<%=basePath%>">
    
    <title>单条数据录入</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->    <style type="text/css">
<!--
.style1 {
font-size: 14px;
font-weight: bold;
color: #33FF00;
}
.style2 {
font-size: 12px;
color: #663399;
}
.style4 {font-size: 12}
.style5 {color: #FF0000}
-->
    </style>
  </head>
   <body> 
    <form name="form" method="post" action="/myjsp/WebRoot/insert1.jsp" onsubmit="<%
    InsertSQL(request.getParameter("id"),request.getParameter("name"),request.getParameter("abc"),
     request.getParameter("spec"),request.getParameter("sort"),
     request.getParameter("unit"),request.getParameter("price"),
     request.getParameter("stock"));
    %>">
...
...
  </form>
    <br>
  </body>
</html>

解决方案 »

  1.   

    不用单引号是因为不是CHAR类型吧,你看一下他定义的可能是INT或其他数字类型。乱码是因为你重新建立的数据库可能没有指定编码,所以不支持中文,在创建数据库的时候指定如gbk或gb2312就可以了。如create database db_name character set gb2312;
      

  2.   

    一般情况下是数据库默认编码与页面编码不一致造成的,如果不方便动数据库的话,就页面getByte转个码看看
      

  3.   

    <%@page contentType="text/html;chsetset=GBK"我怎么没看到这个东西呢?为什么?
    好奇怪的书?
      

  4.   

    应该是4号和YNYN说的数据库默认编码不同导致的..但要怎么更改数据库里的默认编码呢?还有我用的是SQL2000..默认编码是什么?4号说的create database db_name character set gb2312这个应该是MYSQL里的..我SQL2000的语法是什么呢?<<JSP程序开发范例宝典>>什么明日科技出的..真汗颜..发邮件他们拒收,论坛一个鬼都没..就算是设置成GBK也是一样乱码啊..只不过不是全是?号..而多了一些其它符号..
      

  5.   

    代码2的insert后边好象没跟着into
    是什么写法
    落下了`?
      

  6.   

    使用<%@ page language="java" pageEncoding="GBK"%>就可以了。
      

  7.   

    不用INTO可以的<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="GBK"%> 
    <%request.setCharacterEncoding("GBK");%> 
    我同时加了这两个也还是不行的