• 综合习题——数据查询——输出学生列表信息


    一)登录页面

     1 <%@page import="java.sql.*"%>
     2 <%@ page language="java" contentType="text/html; charset=UTF-8"
     3     pageEncoding="UTF-8"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     8 <title>学生信息</title>
     9 </head>
    10 <body>
    11 学生信息列表
    12 <br>
    13 <%
    14 try
    15 {
    16 //连接数据库
    17 
    18   //1)、加载驱动
    19   Class.forName("oracle.jdbc.driver.OracleDriver");
    20   String strUrl="jdbc:oracle:thin:@localhost:1521:ORCL";
    21   
    22   Connection conn=DriverManager.getConnection(strUrl, "test", "test");
    23   //2)、得到连接
    24 
    25 //读取
    26 Statement st=conn.createStatement();
    27   ResultSet rs=st.executeQuery("select t.* from T_STUDENT t");
    28 //输出结果集
    29 if(rs!=null)
    30 {
    31     while(rs.next())
    32     {
    33         out.print("sno="+rs.getString("sno")+" ");
    34         out.print("name="+rs.getString("sname")+" ");
    35         out.print("ssex="+rs.getString("ssex")+"");
    36         out.print("sbirthday="+rs.getDate("sbirthday")+" ");
    37         out.print("class="+rs.getString("class")+"<br>");
    38     }
    39     }
    40 //释放资源
    41 st.close();
    42 conn.close();
    43 rs.close();
    44 }
    45 catch(Exception ex)
    46 {
    47     ex.printStackTrace();
    48     }
    49 %>
    50 添加学生信息
    51 <form action="savet_student" method="post">
    52 学号<input type="text" name="sno"><br>
    53 名称<input type="text" name="sname"><br>
    54 性别<input type="text" name="ssex"><br>
    55 生日<input type="text" name="sbirthday"><br>
    56 班级<input type="text" name="class"><br>
    57 <input type="submit" value="保存">
    58 </form>
    59 
    60 </body>
    61 </html>

    二)转码页面

     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 }

    三)跳转页面

     1 package com.haiqi.web;
     2 
     3 import java.io.IOException;
     4 import java.sql.*;
     5 
     6 import javax.servlet.ServletException;
     7 import javax.servlet.http.HttpServlet;
     8 import javax.servlet.http.HttpServletRequest;
     9 import javax.servlet.http.HttpServletResponse;
    10 
    11 public class savet_student extends HttpServlet {
    12     private static final long serialVersionUID = 1L;
    13 
    14     public savet_student() {
    15         super();
    16         // TODO Auto-generated constructor stub
    17     }
    18 
    19     protected void doGet(HttpServletRequest request, HttpServletResponse response)
    20             throws ServletException, IOException {
    21         // 处理保存数据的请求
    22 
    23         // 1)接收参数
    24         String sno = request.getParameter("sno");
    25         String sname = request.getParameter("sname");
    26         String ssex = request.getParameter("ssex");
    27         String sbirthday = request.getParameter("sbirthday");
    28         String sclass = request.getParameter("class");
    29         // 2)验证
    30 
    31         if (sno != null && sno.trim().length() != 0 && sname != null && sname.trim().length() != 0)
    32 
    33         {
    34             try {
    35                 Class.forName("oracle.jdbc.driver.OracleDriver");
    36                 String strUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    37 
    38                 Connection conn = DriverManager.getConnection(strUrl, "test", "test");
    39 
    40                 // 2)、得到连接
    41                 // 3)保存
    42                 // 读取
    43                 PreparedStatement pst = conn.prepareStatement("insert into t_student(sno,sname,ssex,class)" + "values(?,?,?,?)");
    44                 pst.setString(1, sno);
    45                 pst.setString(2, sname);
    46                 pst.setString(3, ssex);
    47                 // pst.setDate(parameterIndex, x);
    48                 pst.setString(4, sclass);
    49                 pst.executeUpdate();
    50 
    51                 pst.close();
    52                 conn.close();
    53                 // 4)跳转页面
    54                 response.setHeader("refresh", "3;URL=index.jsp");
    55                 response.getWriter().write("请正确提交数据");
    56                 
    57             }
    58             catch (Exception e) {
    59                 e.printStackTrace();
    60             }
    61         }
    62 
    63         else {
    64             response.setHeader("reflush", "3;URL=index.jsp");
    65             response.getWriter().write("请正确提交数据");
    66         }
    67 
    68         //response.getWriter().append("Served at: ").append(request.getContextPath());
    69     }
    70 
    71     protected void doPost(HttpServletRequest request, HttpServletResponse response)
    72             throws ServletException, IOException {
    73 
    74         doGet(request, response);
    75     }
    76 
    77 }
  • 相关阅读:
    深入理解Kafka-学习笔记04-部分生产者客户端参数
    深入理解Kafka-学习笔记03-消息有序性
    IDEA包不存在找不到包解决方案
    深入理解Kafka-学习笔记02-生产者整体架构
    深入理解Kafka-学习笔记01-初识Kafka
    ClickHouse与MySQL数据类型对应
    MySQL查看版本
    从apache phoenix表提取数据为CSV
    Kafka查看版本
    HBASE查看版本
  • 原文地址:https://www.cnblogs.com/yg6405816/p/5640795.html
Copyright © 2020-2023  润新知