一个纯粹在JSP 页面实现对数据增删改的例子

  1. <%@ page contentType="text/html; charset=GB2312"%>  
  2. <%@ page language="java" import="java.sql.*"%>  
  3.   
  4. <HTML>  
  5.     <HEAD>  
  6.         <SCRIPT LANGUAGE="JavaScript">  
  7.     function btnClick(type){   
  8.    var fm=document.form1   
  9.   if(type=='add'){   
  10.        
  11.   fm.c.value=1;    
  12.   return  true;   
  13.   }else if(type=='edt'){   
  14.    fm.c.value=2;   
  15.   return true;    
  16.  }else{   
  17.    fm.c.value=3;   
  18.    return true;   
  19.   
  20. }   
  21. }   
  22.   
  23. SCRIPT>  
  24.   
  25.         <TITLE>增加记录TITLE>  
  26.   
  27.     HEAD>  
  28.   
  29.     <BODY>  
  30.         <%   
  31.             Connection con = null;   
  32.             Statement sql = null;   
  33.             ResultSet rs = null;   
  34.             try {   
  35.                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");   
  36.             } catch (ClassNotFoundException e) {   
  37.                 out.println("连接没成功");   
  38.             }   
  39.                
  40.             String Std_id=null;   
  41.             String Std_name=null;   
  42.             String Std_sex=null;   
  43.             String Std_cj=null;   
  44.             try {   
  45.   
  46.                 con = DriverManager.getConnection("jdbc:odbc:LocalServer",   
  47.                 "exmple", "exmple");   
  48.                 sql = con.createStatement();   
  49.                    
  50.                    
  51.                 String parm = request.getParameter("c");   
  52.                    
  53.                 if(parm==null)   
  54.                 {   
  55.                 parm="";   
  56.                    
  57.                 }   
  58.                     String std_id=request.getParameter("std_id");   
  59.                     String std_name=request.getParameter("std_name");   
  60.                     String std_sex=request.getParameter("std_sex");   
  61.                     String std_cj=request.getParameter("std_cj");   
  62.               if (parm.equals("1")) {   
  63.                        
  64.                     String condition = "insert into student values" + "(" + "'"   
  65.                     + std_id + "','" + std_name + "','" + std_sex   
  66.                     + "','" + std_cj + "')";   
  67.                     sql.executeUpdate(condition);   
  68.                   
  69.                }else if (parm.equals("2")) {   
  70.                     String condition = "update student set std_name='"  
  71.                     + std_name + "',std_sex='" + std_sex + "',std_cj='"   
  72.                     + std_cj + "' where std_id='" + std_id + "'";   
  73.                     sql.executeUpdate(condition);   
  74.   
  75.                 } else if(parm.equals("3")){   
  76.   
  77.                     String condition = "delete from student where std_id='"  
  78.                     + std_id + "'";   
  79.                     sql.executeUpdate(condition);   
  80.                 }else{}   
  81.                    
  82.                 rs=sql.executeQuery("select * from student order by std_id");   
  83.                // rs.afterLast();   
  84.                //rs.last();   
  85.              while(rs.next()){   
  86.                    
  87.                 Std_id = rs.getString(1);   
  88.                 Std_name = rs.getString(2);   
  89.                 Std_sex =rs.getString(3);   
  90.                 Std_cj = rs.getString(4);   
  91.                 out.print(Std_id);   
  92.                 }   
  93.                 con.close();   
  94.                    
  95.             } catch (SQLException e) {   
  96.             }   
  97.         %>  
  98.            
  99.         <center>  
  100.             <form id="form1" name="form1" method="post">  
  101.                 <br>  
  102.                 学号:   
  103.                 <input type="text" name="std_id" value="" >  
  104.                 <br>  
  105.                 名字:   
  106.                 <input type="text" name="std_name" value="" >  
  107.                 <br>  
  108.                 性别:   
  109.                 <input type="text" name="std_sex" value="" >  
  110.                 <br>  
  111.                 成绩:   
  112.                 <input type="text" name="std_cj" value="">  
  113.   
  114.                 <br>  
  115. <input type="hidden" name="c" id="c">  
  116. <input type="button" name="add" value="新增" onclick="btnClick('add')">  
  117. <input type="button" name="edt" value="修改" onclick="btnClick('edt')">  
  118. <input type="button" name="del" value="删除" onclick="btnClick('del')">  
  119. <input type="submit" name="save" value="保存">  
  120.           </form>  
  121.         </center>  
  122.   
  123.    </BODY>  
  124. </HTML>  

posted on 2009-01-06 15:52  欣路历程  阅读(808)  评论(0)    收藏  举报

导航