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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值