1 <%@page import="java.sql.*"%> 2 <%@page import="java.sql.DriverManager"%> 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 16 try 17 { 18 // 1. 连接数据库 19 // 1)加载驱动 20 Class.forName("oracle.jdbc.driver.OracleDriver"); 21 22 // 2)得到连接 23 Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", 24 "test1","1234"); 25 // 2. 读取数据库 26 Statement st = conn.createStatement(); 27 28 ResultSet rs = st.executeQuery("select * from T_STUDENTS"); 29 30 // 3. 输出结果集 31 if (rs != null) 32 { 33 while(rs.next()) 34 { 35 out.print("SNO = " + rs.getString("SNO") + " "); 36 out.print("name = " + rs.getString("sname") + " "); 37 out.print("ssex = " + rs.getString("ssex") + " "); 38 out.print("sbirthday = " + rs.getString("sbirthday") + " "); 39 out.print("calss = " + rs.getString("class") + "<br>"); 40 41 } 42 rs.close(); 43 } 44 45 // 4. 释放资源 46 st.close(); 47 conn.close(); 48 } 49 catch (Exception e) 50 51 { 52 53 e.printStackTrace(); 54 System.out.println("连接数据库失败"); 55 } 56 57 %> 58 59 添加学生信息<br> 60 <form action="SaveStudent" method = "post"> 61 学号:<input type = "text" name = "sno"><br> 62 名称:<input type = "text" name = "sname"><br> 63 性别:<input type = "text" name = "ssex"><br> 64 生日:<input type = "text" name = "sbirthday"><br> 65 班级:<input type = "text" name = "class"><br> 66 67 <input type = "submit" value = "保存"> 68 </form> 69 70 </body> 71 </html>
1 package com.hanqi.web; 2 import java.sql.*; 3 import java.io.IOException; 4 import java.sql.Connection; 5 import java.sql.PreparedStatement; 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 13 public class SaveStudent extends HttpServlet { 14 private static final long serialVersionUID = 1L; 15 16 public SaveStudent() { 17 super(); 18 19 } 20 21 22 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 23 24 // 处理保存数据的请求 25 26 // 1.接受参数 27 String sno = request.getParameter("sno"); 28 String sname = request.getParameter("sname"); 29 String ssex = request.getParameter("ssex"); 30 String sbirthday = request.getParameter("sbirthday"); 31 String sclass = request.getParameter("class"); 32 33 // 2.验证参数 34 if (sno != null && sno.trim().length() != 0 35 && sname !=null && sname.trim().length() !=0) 36 { 37 try 38 { 39 // 3.保存参数 40 Class.forName("oracle.jdbc.driver.OracleDriver"); 41 42 // 2)得到连接 43 Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", 44 "test1","1234"); 45 // 2. 读取数据库 46 //Statement st = conn.createStatement(); 47 PreparedStatement pst = conn.prepareStatement( 48 "insert into t_students(sno,sname,ssex,class)"+ 49 "values(?,?,?,?)"); 50 51 pst.setString(1, sno); 52 pst.setString(2, sname); 53 pst.setString(3, ssex); 54 //pst.setDate(4, x); 55 pst.setString(4, sclass); 56 57 pst.executeUpdate(); 58 59 pst.close(); 60 conn.close(); 61 62 63 // 4.跳转页面 64 response.getWriter().print("保存成功");; 65 response.setHeader("refresh", "3;URL=index.jsp"); 66 67 } 68 catch(Exception e) 69 { 70 e.printStackTrace(); 71 response.getWriter().print("保存失败");; 72 response.setHeader("refresh", "3;URL=index.jsp"); 73 } 74 75 76 } 77 else 78 { 79 response.getWriter().print("请正确提交数据");; 80 response.setHeader("refresh", "3;URL=index.jsp"); 81 } 82 83 84 } 85 86 87 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 88 89 doGet(request, response); 90 } 91 92 }
1 package com.hanqi.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 16 } 17 18 19 public void destroy() { 20 21 } 22 23 24 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 25 26 //添加过滤 27 request.setCharacterEncoding("UTF-8"); 28 29 response.setContentType("text/html"); 30 31 response.setCharacterEncoding("UTF-8"); 32 33 34 35 36 chain.doFilter(request, response); 37 } 38 39 40 public void init(FilterConfig fConfig) throws ServletException { 41 42 } 43 44 }
时间: 2024-10-20 03:34:28