说明:数据库:access ,服务器:tomcat 6.0 ,操作系统:windows xp
目标:实现对student数据库进行四大操作:查询,添加,删除,修改
问题:上述四大功能基本上实现,有些细节还未完善,其中有一条:添加数据时怎么判断学号的唯一性,我写的代码如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>添加学生信息</title>
<style type="text/css">
<!--
.style1 {
color: #FF0000;
font-size: 24px;
}
.style3 {font-size: 16px}
-->
</style>
<script language="javascript">
function panduan(){while(1==0)
{
  alert("学号已经存在!");  
}     
document.forms[0].submit();
}
</script>
<script language='javascript'>   
  function   check()
{  }   
</script>
</head>
<body><div align="center" class="style1">
  <p>添加学生信息</p>
  <form name="form1" method="post" action="add_1.jsp">
    <p><span class="style3">学号:</span>      
      <input type="text"  name="id" onchange="check()">
    </p>    <p><span class="style3">姓名:</span>      
      <input type="text" name="name">
    </p>
    <p><span class="style3">性别:</span>      
      <input type="text" name="sex">
    </p>
    <p><span class="style3">年龄:</span>      
      <input type="text" name="age" >
    </p>
    <p><span class="style3">体重:</span>      
      <input type="text" name="weight">
    </p>
    <p>
      &nbsp;
      <input type="button" name="queding" value="提交" onclick="panduan()">
      &nbsp;
      <input type="reset" name="chongtian" value="重置">
</p>
    <p>&nbsp;    </p>
  </form>
  <p>&nbsp;  </p>
</div><script language='javascript'>  
check()
{
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //载入驱动程序类别
  String url = "jdbc:odbc:studentdata";
  String user="";
  String pwd="";
  Connection con = DriverManager.getConnection(url,user, pwd); //建立数据库链接,studentdata为ODBC数据源名称
  //建立Statement对象
  Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
  ResultSet.CONCUR_READ_ONLY); 
  String id=request.getParameter("id");
  String sql= "select * from stu_info where id='"+id+"'";
  ResultSet rs = stmt.executeQuery(sql);
  if(rs.next()){
  alert("学号已经存在!");  
  rs.close();
  }
}  
</script>

</body>
</html>本意是打算利用check()函数在输入完学号之后会自动提示学号是否可用,即满足唯一性条件。PS,本人初学,完全是个菜鸟,我不知道能不能利用Java script实现。在网上搜索了一番,有人说用Ajax,这个我不会,在csdn上搜到一个跟我要求接近的提问,网页地址如下:http://topic.csdn.net/t/20040630/21/3135779.html
还望高手指点一二。谢谢。附件是所有相关源码以及源数据库。

解决方案 »

  1.   

    首先你那个不是js,是java里面的内容,在保存操作数据库insert之前使用可以达到效果。另外,一定要注意,这样的设计有点小缺陷,本人开始也是深受其害。如果几个人同时录入数据,而且录入的学号一样,这样会导致某个人的录入产生错误(如果学号有唯一性约束),不过使用人数少,而且录入工作很平均倒也不会有什么大问题。用纯JS应该是不可以,用ajax可以。
      

  2.   

    最基本的IE下使用的Ajax代码
    var http_req = false;
    function p_Req()
    {
          if(http_req.readyState==4)
    {
             if(http_req.status==200)
    {
                var ret = http_req.responseText;
                if(ret=="existed")
    {
                           alert("学号已经存在!");
                }
             }
    else
    {
                alert("您所请求的页面有异常。");
             }
          }
    }
    function s_req(url)
    {
        http_req=false;
        try
    {
           http_req = new ActiveXObject("Msxml2.XMLHTTP");
        }
    catch(e)
    {
           try
    {
              http_req = new ActiveXObject("Microsoft.XMLHTTP");
           }
    catch(e)
    {
    }
        }
        if(!http_req)
    {
          alert("不能创建XMLHttpRequest对象实例。");
          return false;
        }
        http_req.onreadystatechange = p_Req;
        http_req.open("GET",url,true);
        http_req.send(null);
    }
    function check()
    {
            s_req("checkId.jsp?id="+form1.id.value);
    }
    checkId.jsp代码
    <%  String id = request.getParameter("id");
      id = id==null?"":id;
      if("".equals(id))
         return;
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //载入驱动程序类别 
      String url = "jdbc:odbc:studentdata"; 
      String user=""; 
      String pwd=""; 
      Connection con = DriverManager.getConnection(url,user, pwd); //建立数据库链接,studentdata为ODBC数据源名称 
      //建立Statement对象 
      Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 
      ResultSet.CONCUR_READ_ONLY); 
      String id=request.getParameter("id"); 
      String sql= "select * from stu_info where id='"+id+"'"; 
      ResultSet rs = stmt.executeQuery(sql); 
      
      if(rs!=null&&rs.next()){ 
         out.clear();
         out.print("existed");
         rs.close(); 
      } 
      else{
         out.print("notexisted");
      }
    %>
    如果要写适用其他浏览器的代码,自己上网搜索一些Ajax相关的就很容易。
      

  3.   

    再次感谢william3033,明天上机试一试。
      

  4.   

    还是第一次在js里面看到jdbc的代码哦,这样貌似不行吧,像jdbc的一些api,比如Connection,Statement,ResultSet类,只有java api才支持
      

  5.   

     我也是第一次在js里面看到jdbc的代码..汗一个···
      

  6.   

    其实Js也可以操作数据库的,只是楼主竟然直接在js函数里面写java代码,第一次见
      

  7.   

    william3033的代码没仔细看,应该能行,还可以在数据库里设置学号为主键,这样学号只能唯一,相同的学号无法入库,捕获异常就可以了
      

  8.   

    william3033 的代码有两个错误的地方,String id=request.getParameter("id"); 重复定义了;第二个错误也不知道算不算错误,out.print("existed"); 传到var ret = http_req.responseText; 变量ret末尾会自动多出一个换行符;如果把existed换成数字就行了,这个bug搞死我了。
    修改过的add.jsp代码如下:
    <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>添加学生信息</title>
    <style type="text/css">
    <!--
    .style1 {
    color: #FF0000;
    font-size: 24px;
    }
    .style3 {font-size: 16px}
    -->
    </style>
    </head>
    <script language='javascript'>  
    var http_req = false; 
    function p_Req() 

        if(http_req.readyState==4) 

            if(http_req.status==200) 

              var ret = http_req.responseText; 
        //alert("s"+ret+"d")
              if(ret==1) 

                          alert("学号已经存在!"); 
              } else{
    alert("test")
    document.forms[0].submit();
    }
            } 
    else 

            window.alert("你请求的页面有问题.");
            } 
        } 

    function s_req(url) 

      http_req=false; 
      try 

          http_req = new ActiveXObject("Msxml2.XMLHTTP"); 
      } 
    catch(e) 

          try 

            http_req = new ActiveXObject("Microsoft.XMLHTTP"); 
          } 
    catch(e) 


      } 
      if(!http_req) 

        alert("不能创建XMLHttpRequest对象实例。"); 
        return false; 
      } 
      http_req.onreadystatechange = p_Req; 
      http_req.open("GET",url,true); 
      http_req.send(null); 

    function check() 

            s_req("checkId.jsp?id="+form1.id.value); 

    }
    </script>
    <body>
    <div align="center" class="style1">
      <p>添加学生信息</p>
      <form name="form1" method="post" action="add_1.jsp">
        <p><span class="style3">学号:</span>      
          <input type="text"  name="id">
        </p>
        <p><span class="style3">姓名:</span>      
          <input type="text" name="name">
        </p>
        <p><span class="style3">性别:</span>      
          <input type="text" name="sex">
        </p>
        <p><span class="style3">年龄:</span>      
          <input type="text" name="age">
        </p>
        <p><span class="style3">体重:</span>      
          <input type="text" name="weight">
        </p>
        <p>
          &nbsp;
          <input type="button" name="queding" value="提交" onClick="check()">
          &nbsp;
          <input type="reset" name="chongtian" value="重置">
    </p>
        <p>&nbsp;    </p>
      </form>
      <p>&nbsp;  </p>
    </div>
    </body>
    </html>
    新加的checkID代码如下:
    <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
    <% 
      String id = request.getParameter("id"); 
      id = id==null?"":id; 
      if("".equals(id)) 
        return; 
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //载入驱动程序类别 
      String url = "jdbc:odbc:studentdata"; 
      String user=""; 
      String pwd=""; 
      Connection con = DriverManager.getConnection(url,user, pwd); //建立数据库链接,studentdata为ODBC数据源名称    
      Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, //建立Statement对象
      ResultSet.CONCUR_READ_ONLY); 
      
      String sql= "select * from stu_info where id='"+id+"'"; 
      ResultSet rs = stmt.executeQuery(sql); 
      
      if(rs!=null&&rs.next()){ 
        out.clear(); 
        out.print("1"); 
        rs.close(); 
      } 
      else{ 
        out.print("notexisted"); 
      } 
    %> 
    再次感谢william3033 !!!
      

  9.   

    汗,第一次看见有人在js中用java。
    感觉用ajax和jquery 好点。