综合习题——删除数据

Posted 曦~妍

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了综合习题——删除数据相关的知识,希望对你有一定的参考价值。

   

 1 <%@page import="java.text.SimpleDateFormat"%>
 2 <%@page import="java.sql.*"%>
 3 <%@ page language="java" contentType="text/html; charset=UTF-8"
 4     pageEncoding="UTF-8"%>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>学生信息</title>
10 </head>
11 <body>
12 学生信息列表
13 <br>
14 <%
15 try
16 {
17 //连接数据库
18 
19   //1)、加载驱动
20     Class.forName("oracle.jdbc.driver.OracleDriver");
21     String strUrl="jdbc:oracle:thin:@localhost:1521:ORCL";
22     Connection conn=DriverManager.getConnection(strUrl, "test", "test");
23   //2)、得到连接
24 //Connection 连接     DriverManager是一个结果集
25 // Connection conn=DriverManager.getConnection("strURL", "test", "test");
26 //读取
27 //Statement  声明  是java的一个重要接口
28  Statement st=conn.createStatement();
29 //RescultSet  遍历    executeQuery()  是数据库查询Select的一个方法
30   ResultSet rs=st.executeQuery("select t.*, t.rowid from T_STUDENT t");
31 //输出结果集
32 if(rs!=null)//如果遍历的认识这个数不为空
33 {
34     SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
35     while(rs.next())//当有下一个数时,输出从rs里得到的“sno”的数据
36     {
37         
38         
39         
40         out.print("sno"+rs.getString("sno")+" ");//从rs结果集中得到"sno"的数据
41         out.print("sname"+rs.getString("sname")+" ");
42         out.print("ssex"+rs.getString("ssex")+" ");
43         try
44         {
45         out.print("sbirthday"+(rs.getDate("sbirthday")==null?"":sdf.format(rs.getDate("sbirthday")))+" ");
46         }catch(Exception e)
47         {
48             e.printStackTrace();
49         }
50         out.print("sclass"+rs.getString("class")+"[修改]<a href=‘Deletet_student?sno="+rs.getString("sno")+"‘>[删除]</a><br>");
51     }
52 }
53 //释放资源
54 conn.close();
55 st.close();
56 rs.close();
57 }
58 catch(Exception e)
59 {
60     e.printStackTrace();
61     }
62 
63  %>
64 添加学生信息
65 <form action="savet_student" method="post">
66 学号<input type="text" name="sno"><br>
67 姓名<input type="text" name="sname"><br>
68 性别<input type="text" name="ssex"><br>
69 生日<input type="text" name="sbirthday"><br>
70 班级<input type="text" name="sclass"><br>
71 <input type="submit" value="保存">
72 </form>
73 </body>
74 </html>

 

 1 package com.haiqi.web;
 2 
 3 import java.io.IOException;
 4 import java.sql.*;
 5 import java.text.SimpleDateFormat;
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 public class savet_student extends HttpServlet {
13     private static final long serialVersionUID = 1L;
14 
15     public savet_student() {
16         super();
17         // TODO Auto-generated constructor stub
18     }
19 
20     protected void doGet(HttpServletRequest request, HttpServletResponse response)
21             throws ServletException, IOException {
22         // 处理保存数据的请求
23 
24         // 1)接收参数
25         String sno = request.getParameter("sno");
26         String sname = request.getParameter("sname");
27         String ssex = request.getParameter("ssex");
28         String sbirthday = request.getParameter("sbirthday");
29         String sclass = request.getParameter("class");
30         // 2)验证
31 
32         if (sno != null && sno.trim().length() != 0 && sname != null && sname.trim().length() != 0)
33 
34         {
35             try {
36                 Class.forName("oracle.jdbc.driver.OracleDriver");
37                 String strUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
38 
39                 Connection conn = DriverManager.getConnection(strUrl, "test", "test");
40 
41                 // 2)、得到连接
42                 // 3)保存
43                 // 读取
44                 PreparedStatement pst = conn.prepareStatement("insert into t_student(sno,sname,ssex,sbirthday,class)" + "values(?,?,?,?,?)");
45                 //String转成Date
46                 //关于日期格式的类
47                 //可以进行日期转换
48                 SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
49                 java.util.Date bir=sdf.parse(sbirthday);
50                 //使用long型的时间构造sql.Date
51                 Date sqlbir=new Date(bir.getTime());
52                 pst.setString(1, sno);
53                 pst.setString(2, sname);
54                 pst.setString(3, ssex);
55                 pst.setDate(4, sqlbir);
56                 pst.setString(5, sclass);
57                 pst.executeUpdate();
58 
59                 pst.close();
60                 conn.close();
61                 // 4)跳转页面
62                 response.setHeader("refresh", "3;URL=index.jsp");
63                 response.getWriter().write("请正确提交数据");
64                 
65             }
66             catch (Exception e) {
67                 e.printStackTrace();
68             }
69         }
70 
71         else {
72             response.setHeader("reflush", "3;URL=index.jsp");
73             response.getWriter().write("请正确提交数据");
74         }
75 
76         //response.getWriter().append("Served at: ").append(request.getContextPath());
77     }
78 
79     protected void doPost(HttpServletRequest request, HttpServletResponse response)
80             throws ServletException, IOException {
81 
82         doGet(request, response);
83     }
84 
85 }

 

 1 package com.haiqi.web;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.Filter;
 5 import javax.servlet.FilterChain;
 6 import javax.servlet.FilterConfig;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.ServletRequest;
 9 import javax.servlet.ServletResponse;
10 
11 
12 public class myfilter implements Filter {
13 
14     public myfilter() {
15         // TODO Auto-generated constructor stub
16     }
17 
18     
19     public void destroy() {
20         // TODO Auto-generated method stub
21     }
22 
23     
24     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
25         request.setCharacterEncoding("UTF-8");
26         response.setContentType("text/html");
27         response.setCharacterEncoding("UTF-8");
28         chain.doFilter(request, response);
29     }
30 
31     
32     public void init(FilterConfig fConfig) throws ServletException {
33         // TODO Auto-generated method stub
34     }
35 
36 }

 

                                                                                                                                                                                                                                                                                                                                                 

以上是关于综合习题——删除数据的主要内容,如果未能解决你的问题,请参考以下文章

ADO.net 综合练习题

shell综合练习题(图文并茂代码清单)

综合习题——修改数据

22年最新Java笔记:day6-Java基础知识综合练习题

第2章 Python基础-字符编码&数据类型 综合 练习题

关系数据库标准语言SQL(上)+前端基础习题分享