日期:2014-05-16  浏览次数:20553 次

很奇怪的问题,跪求大神解决
我用Ajax尝试做实现表单数据验证
遇到的问题是能连接数据库,能查询数据库
但是当新用户注册成功时,数据库没有增加数据。
搞不懂为什么
下面是几个页面的代码
Conn.jsp

Java code
<%!
  Connection con=null;
  Statement stmt=null;
  ResultSet rs=null;
  public void jspInit(){
    try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con=DriverManager.getConnection("jdbc:odbc:"+"wl","sa","wl");
      stmt=con.createStatement();
      }
    catch(Exception ex){
      System.out.println(ex.toString());
      }
    }
    public void Exec(String sql){
      jspInit();
      try{
    stmt.executeUpdate(sql);
    } 
      catch(Exception e){
    System.out.print(e.toString());    
    }
      }
    public ResultSet getRs(String sql) throws SQLException{
      jspInit();
      try{
      rs=stmt.executeQuery(sql);
      return rs;
      }
      catch(Exception e){
       System.out.print(e);
      return null;
    }
      }
    public String getS(String Str){
      try{
    byte b[]=Str.getBytes("ISO-8859-1");
    Str=new String(b,"UTF-8");
    }
      catch(Exception ee){
    ee.printStackTrace();
    }
      return Str;
      }

%>


zhuce1.jsp

Java code
<%@ page contentType="text/html;charset=GB2312" import="java.sql.*" errorPage=""%>
<%@ include file="Conn.jsp"%>
<%
  String name=request.getParameter("name");
  String password=request.getParameter("pass");
  String mailname=request.getParameter("mailname");
  name=getS(name);
  String sql="insert into [uesr-ajax](user_name,user_password,user_email) values ('name','password','mailname')";
  try{
    String sq="select * from [user-ajax] where user_name='"+name+"'";
    ResultSet rs=getRs(sq);
    if(rs.next()){
      out.print("2");
      }
    else{
      Exec(sql);
      out.print("1"); 
      }
    }
  catch(Exception e){
    out.print("0");
    }
%>


zhuce.jsp
HTML code

<%@ page contentType="text/html;cahrset=GB2312" import="java.sql.*" errorPage=""%>
<script type=text/javascript>
  function trim(str){
    var t=str.replace(/(^\s*)|(\s*$)/g,"");  //用正则表达式将前后空格用空字符串替代
    return t.replace(/(^ *)|( *$)/g,"");
    }
  var xmlHttp;
  function createXMLHttpRequest(){
    if(window.ActiveXObject){
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    else if(window.XMLHttpRequest){
           xmlHttp=new XMLHttpRequest();
       }
    }
  function validate(){
    createXMLHttpRequest();
    var name=document.getElementById("username").value;
    var pass=document.getElementById("passname").value;
    var mailname=document.getElementById("mailname").value;
    var str="name="+name+"&pass="+pass+"&mailname="+mailname;
    var url="zhuce1.jsp";
    xmlHttp.open("POST",url,true);
    xmlHttp.onreadystatechange=callback;
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlHttp.send(str);
    }
  function callback(){
    if(xmlHttp.readyState==4){
      var s=xmlHttp.responseText;
      if(trim(s)==1){
    alert("用户注成功,即将转向首页");
       clear();
    }
      if(trim(s)==2){
    var ta="<font color='red'>该名称在数据库中已存在,请重新输入</font>"
        document.getElementById("res").innerHTML=ta;