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=‘update.jsp?sno="+rs.getString("sno")+"‘>[修改]</a> <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 添加学生信息<br> 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 <%@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 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 10 <title>修改学生信息</title> 11 </head> 12 <body> 13 <% 14 //接收主键,从数据库中读取最新数据,在进行修改 15 String sno=request.getParameter("sno"); 16 String sname=""; 17 String ssex=""; 18 String sbirthday=""; 19 String sclass=""; 20 if(sno!=null&&sno.trim().length()>0) 21 { 22 23 try 24 { 25 //读取数据 26 Class.forName("oracle.jdbc.driver.OracleDriver"); 27 String strUrl = "jdbc:oracle:thin:@localhost:1521:ORCL"; 28 29 Connection conn = DriverManager.getConnection(strUrl, "test", "test"); 30 PreparedStatement pst=conn.prepareStatement("select * from t_student where sno=?"); 31 pst.setString(1,sno); 32 ResultSet rs=pst.executeQuery(); 33 //如果数据库中数据不为空且有下一条数据 34 if(rs!=null&&rs.next()) 35 { 36 37 sname=rs.getString("sname"); 38 ssex=rs.getString("ssex"); 39 try{ 40 SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd"); 41 sbirthday=sdf.format(rs.getDate("sbirthday")); 42 }catch(Exception e) 43 { 44 e.printStackTrace(); 45 } 46 sclass=rs.getString("class"); 47 rs.close(); 48 } 49 else 50 { 51 out.print("未查询到数据"); 52 } 53 pst.close(); 54 conn.close(); 55 }catch(Exception e) 56 { 57 e.printStackTrace(); 58 } 59 } 60 else 61 { 62 out.print("请正确访问"); 63 } 64 %> 65 修改学生信息<br> 66 <form action="savet_student" method="post"> 67 学号<input type="text" name="sno" readonly="readonly" value="<%=sno %>"><br><br> 68 姓名<input type="text" name="sname" value="<%=sname %>"><br><br> 69 性别<input type="text" name="ssex" value="<%=ssex %>"><br><br> 70 生日<input type="text" name="sbirthday" value="<%=sbirthday %>"><br><br> 71 班级<input type="text" name="sclass" value="<%=sclass %>"><br><br> 72 <input type="hidden" name="isupdate" value="1"> 73 <input type="submit" value="保存"> 74 </form> 75 </body> 76 </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("sclass"); 30 //是否更新 31 String isupdate=request.getParameter("isupdate"); 32 // 2)验证 33 34 if (sno != null && sno.trim().length() != 0 && sname != null && sname.trim().length() != 0) 35 36 { 37 try { 38 Class.forName("oracle.jdbc.driver.OracleDriver"); 39 String strUrl = "jdbc:oracle:thin:@localhost:1521:ORCL"; 40 41 Connection conn = DriverManager.getConnection(strUrl, "test", "test"); 42 43 //执行插入或更新 44 //插入 45 PreparedStatement pst = conn.prepareStatement("insert into t_student(sname,ssex,sbirthday,class,sno)" + "values(?,?,?,?,?)"); 46 // 47 if(isupdate!=null&&isupdate.equals("1")) 48 { 49 //更新 50 pst = conn.prepareStatement("update student set sname=?," 51 + "ssex=?,sbirthday=?,class=? where "+"sno=?"); 52 } 53 54 55 //String转成Date 56 //关于日期格式的类 57 //可以进行日期转换 58 SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd"); 59 java.util.Date bir=sdf.parse(sbirthday); 60 //使用long型的时间构造sql.Date 61 Date sqlbir=new Date(bir.getTime()); 62 pst.setString(5, sno); 63 pst.setString(1, sname); 64 pst.setString(2, ssex); 65 pst.setDate(3, sqlbir); 66 pst.setString(4, sclass); 67 pst.executeUpdate(); 68 69 pst.close(); 70 71 conn.close(); 72 // 4)跳转页面 73 response.setHeader("refresh", "3;URL=index.jsp"); 74 response.getWriter().write("数据保存成功"); 75 76 } 77 catch (Exception e) { 78 e.printStackTrace(); 79 } 80 } 81 82 else { 83 response.setHeader("reflush", "3;URL=index.jsp"); 84 response.getWriter().write("请正确提交数据"); 85 } 86 87 //response.getWriter().append("Served at: ").append(request.getContextPath()); 88 } 89 90 protected void doPost(HttpServletRequest request, HttpServletResponse response) 91 throws ServletException, IOException { 92 93 doGet(request, response); 94 } 95 96 }
1 package com.haiqi.web; 2 3 import java.io.IOException; 4 import java.sql.Connection; 5 import java.sql.Date; 6 import java.sql.DriverManager; 7 import java.sql.PreparedStatement; 8 import java.text.SimpleDateFormat; 9 10 import javax.servlet.ServletException; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 15 /** 16 * Servlet implementation class Deletet_student 17 */ 18 public class Deletet_student extends HttpServlet { 19 private static final long serialVersionUID = 1L; 20 21 /** 22 * @see HttpServlet#HttpServlet() 23 */ 24 public Deletet_student() { 25 super(); 26 // TODO Auto-generated constructor stub 27 } 28 29 /** 30 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 31 */ 32 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 33 String sno = request.getParameter("sno"); 34 if (sno != null && sno.trim().length() != 0 ) 35 36 { 37 try { 38 Class.forName("oracle.jdbc.driver.OracleDriver"); 39 String strUrl = "jdbc:oracle:thin:@localhost:1521:ORCL"; 40 41 Connection conn = DriverManager.getConnection(strUrl, "test", "test"); 42 43 // 2)、得到连接 44 // 3)保存 45 // 读取 46 //删除 47 PreparedStatement pst = conn.prepareStatement("delete t_student where sno=?"); 48 //String转成Date 49 //关于日期格式的类 50 //可以进行日期转换 51 SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd"); 52 53 //使用long型的时间构造sql.Date 54 55 pst.setString(1, sno); 56 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 else { 71 response.setHeader("reflush", "3;URL=index.jsp"); 72 response.getWriter().write("请正确提交数据"); 73 } 74 75 } 76 //response.getWriter().append("Served at: ").append(request.getContextPath()); 77 78 79 /** 80 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 81 */ 82 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 83 // TODO Auto-generated method stub 84 doGet(request, response); 85 } 86 87 }
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 }
时间: 2024-11-13 06:38:31