<%@page import="java.text.SimpleDateFormat"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.sql.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>教师信息</title> </head> <body> <div align="center"><font size="20px">教师信息表</font></div> <table border="1" cellspacing="0" width="80%" align="center" style="text-align: center"> <tr> <td height="40">职工编号</td> <td >姓名</td> <td >性别</td> <td >生日</td> <td >职称</td> <td >专业</td> <td>数据处理</td> </tr> <% try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "test", "5211314"); Statement st=conn.createStatement(); ResultSet rs=st.executeQuery("select * from TEACHER"); if(rs!=null) { SimpleDateFormat sdf= new SimpleDateFormat("MM-dd-yyyy"); while(rs.next()) { String tno = rs.getString(1); String tname = rs.getString(2); String tsex = rs.getString(3); String tbirthday=null; if(rs.getDate(4)!=null) { tbirthday=sdf.format(rs.getDate(4)); } String prof = rs.getString(5); String depart = rs.getString(6); %> <tr> <td height="40"><%=tno %></td> <td><%=tname%></td> <td><%=tsex%></td> <td><%=tbirthday==null?"":tbirthday %></td> <td><%=prof==null?"":prof %></td> <td><%=depart%></td> <td><a href=‘updateT.jsp?tno=<%=tno%>‘><input type="button" value="修改"></a> <a href=‘DeleteT?tno=<%=tno%>‘><input type="button" value="删除"></a></td> </tr> <% } rs.close(); } st.close(); conn.close(); } catch(Exception e) { e.printStackTrace(); } %> <tr><td colspan="7" height="40"><a href="teacherInsert.jsp"><input type="button" value="添加教师信息"></a></td></tr> </table> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>添加信息</title> </head> <body> <form action="SaveT" method="post"> 编号:<input type="text" name="tno"><br> 姓名:<input type="text" name="tname"><br> 性别:<input type="text" name="tsex"><br> 生日:<input type="text" name="tbirthday"><br> 职称:<input type="text" name="prof"><br> 专业:<input type="text" name="depart"><br> <input type="submit" value="保存"> </form> </body> </html>
<%@page import="java.text.SimpleDateFormat"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>修改</title> </head> <body> <% try{ String tno=request.getParameter("tno"); String tname=null; String tsex=null; String tbirthday=null; String prof=null; String depart=null; SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); if(tno!=null && tno.trim().length()>0) { try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "test", "5211314"); PreparedStatement p=conn.prepareStatement( "select * from TEACHER where tno=?"); p.setString(1, tno); ResultSet rs=p.executeQuery(); if(rs!=null&&rs.next()) { tname=rs.getString("tname"); tsex=rs.getString("tsex"); prof=rs.getString("prof"); depart=rs.getString("depart"); %> <form action="SaveT" method="post"> 编号:<input type="text" name="tno" readonly="readonly" value="<%=tno %>"><br> 姓名:<input type="text" name="tname" value="<%=tname %>"><br> 性别:<input type="text" name="tsex" value="<%=tsex%>"><br> 生日:<input type="text" name="tbirthday" value="<%=rs.getDate("tbirthday")==null?"":sdf.format(rs.getDate("tbirthday")) %>"><br> 职称:<input type="text" name="prof" value="<%=prof==null?"":prof%>"><br> 专业:<input type="text" name="depart" value="<%=depart %>"><br> <input type="hidden" name="isupdate" value="1"> <input type="submit" value="保存"> </form> <% rs.close(); } else { out.print("未查询到数据"); } p.close(); conn.close(); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } else { out.print("请正确访问"); } }catch(Exception e) { } %> </body> </html>
package teacher; import java.sql.*; import java.text.SimpleDateFormat; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import oracle.net.aso.e; public class SaveT extends HttpServlet { private static final long serialVersionUID = 1L; public SaveT() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String tno=request.getParameter("tno"); String tname=request.getParameter("tname"); String tsex=request.getParameter("tsex"); String tbirthday=request.getParameter("tbirthday"); String prof=request.getParameter("prof"); String depart=request.getParameter("depart"); String isupdate=request.getParameter("isupdate"); if(tno!=null&& tno.trim().length()!=0) { try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "test", "5211314"); if(tbirthday.length()!=0) { PreparedStatement pst=conn.prepareStatement( "insert into TEACHER (tname,tsex,tbirthday,prof,depart,tno)"+ "values(?,?,?,?,?,?)"); if(isupdate!=null&& isupdate.equals("1")) { pst = conn.prepareStatement( "update TEACHER set tname=?,tsex=?,tbirthday=?,prof=?,depart=? where tno=?"); } SimpleDateFormat adf = new SimpleDateFormat("yyyy-MM-dd"); java.util.Date bir= adf.parse(tbirthday); Date sqlBir = new Date(bir.getTime()); pst.setString(6, tno); pst.setString(1, tname); pst.setString(2, tsex); pst.setDate(3, sqlBir); pst.setString(4, prof); pst.setString(5, depart); pst.executeUpdate(); pst.close(); } else { PreparedStatement pst=conn.prepareStatement( "insert into TEACHER (tname,tsex,prof,depart,tno)"+ "values(?,?,?,?,?)"); if(isupdate!=null&& isupdate.equals("1")) { pst = conn.prepareStatement( "update TEACHER set tname=?,tsex=?,prof=?,depart=? where tno=?"); } pst.setString(5, tno); pst.setString(1, tname); pst.setString(2, tsex); pst.setString(3, prof); pst.setString(4, depart); pst.executeUpdate(); pst.close(); } conn.close(); response.getWriter().write("保存成功"); response.setHeader("refresh", "3;URL=teacherChart.jsp"); } catch (Exception e) { e.printStackTrace(); response.getWriter().write("保存失败"); response.setHeader("refresh", "3;URL=teacherChart.jsp"); } } else { response.getWriter().write("请正确提交数据"); response.setHeader("refresh", "3;URL=teacherChart.jsp"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package teacher; import java.sql.*; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DeleteT extends HttpServlet { private static final long serialVersionUID = 1L; public DeleteT() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String tno=request.getParameter("tno"); if(tno!=null&& tno.trim().length()!=0) { try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "test", "5211314"); PreparedStatement pst=conn.prepareStatement( "delete TEACHER where "+"tno=?"); pst.setString(1, tno); pst.executeUpdate(); pst.close(); conn.close(); response.getWriter().write("删除成功"); response.setHeader("refresh", "3;URL=teacherChart.jsp"); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); response.getWriter().write("删除失败"); response.setHeader("refresh", "3;URL=teacherChart.jsp"); } } else { response.getWriter().write("请正确提交数据"); response.setHeader("refresh", "3;URL=teacherChart.jsp"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
时间: 2024-09-29 08:45:27