这个项目是别人的结晶,有下载,希望不要找我麻烦。我只是觉得很好 servlet package com.test.servlet; import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.test.bean.Student; import com.test.dao.StudentDAO; public class ServletAction extends HttpServlet { StudentDAO s = new StudentDAO(); public void init() throws ServletException { } public ServletAction() { super(); } public void destroy() { super.destroy(); // Just puts "destroy" string in log } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); request.setCharacterEncoding("gbk"); response.setCharacterEncoding("gbk"); String type=request.getParameter("type"); if(type.equals("login")){ this.show(request, response); } if(type.equals("delete")){ this.delete(request, response); } if(type.equals("insert")){ this.insert(request, response); } if(type.equals("modify")){ this.modify(request, response); } if(type.equals("select")){ this.select(request, response); } } public void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { ResultSet rs = s.selectAllStudent(); request.setAttribute("rs", rs); } catch (SQLException e) { e.printStackTrace(); } RequestDispatcher rsd = request.getRequestDispatcher("display.jsp"); rsd.forward(request,response); } public void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id = Integer.parseInt(request.getParameter("id")); try { Student stu=s.selectStudentById(id); request.setAttribute("stu", stu); request.setAttribute("id", id); } catch (SQLException e) { e.printStackTrace(); } // response.sendRedirect("modify.jsp");//����д������ RequestDispatcher rsd = request.getRequestDispatcher("modify.jsp"); rsd.forward(request,response); } public void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id = Integer.parseInt(request.getParameter("id")); String name = request.getParameter("name"); int age = Integer.parseInt(request.getParameter("age")); String sex = request.getParameter("sex"); String address = request.getParameter("address"); Student stu=new Student(); stu.setId(id); stu.setName(name); stu.setAge(age); stu.setSex(sex); stu.setAddress(address); try { s.insertStudent(stu); } catch (SQLException e) { e.printStackTrace(); } response.sendRedirect("ServletAction?type=login"); } public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id = Integer.parseInt(request.getParameter("id")); try { s.deleteStudent(id); } catch (SQLException e) { e.printStackTrace(); } response.sendRedirect("ServletAction?type=login"); } public void modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id = Integer.parseInt(request.getParameter("id")); int num = Integer.parseInt(request.getParameter("num")); String name = request.getParameter("name"); int age = Integer.parseInt(request.getParameter("age")); String sex = request.getParameter("sex"); String address = request.getParameter("address"); Student stu=new Student(); stu.setId(num); stu.setName(name); stu.setAge(age); stu.setSex(sex); stu.setAddress(address); try { s.updateStudent(stu, id); } catch (SQLException e) { e.printStackTrace(); } response.sendRedirect("ServletAction?type=login"); } } dao package com.test.dao; import java.sql.ResultSet; import java.sql.SQLException; import com.test.bean.Student; import com.test.db.DBOperator; public class StudentDAO { private int n; private ResultSet rs; /** * ����ݿ�����һ����¼�ĵ��÷��� */ public int insertStudent(Student cust) throws SQLException{ String sql = "insert into Student values(?,?,?,?,?)"; Object [] obj = new Object[]{cust.getId(),cust.getName(),cust.getAge(),cust.getSex(),cust.getAddress()}; DBOperator db = new DBOperator(); try { n = db.executeUpdate(sql, obj); } catch (SQLException e) { e.printStackTrace(); } return n; } /** * ɾ����ݿ�һ����¼���÷��� */ public int deleteStudent(int id) throws SQLException{ String sql = "delete from Student where id=?"; Object[] obj = new Object[]{id}; DBOperator db = new DBOperator(); try { n = db.executeUpdate(sql, obj); } catch (SQLException e) { e.printStackTrace(); } return n; } /** * ����ݿ�һ����¼���÷��� */ public int updateStudent(Student cust,int Id) throws SQLException{ String sql = "update Student set id=?,name=? ,age=?,sex=?,address=? where id=?"; Object[] obj = new Object[]{cust.getId(),cust.getName(),cust.getAge(),cust.getSex(),cust.getAddress(),Id}; DBOperator db = new DBOperator(); try { n = db.executeUpdate(sql, obj); } catch (SQLException e) { e.printStackTrace(); } return n; } /** * ������ݿ������м�¼ */ public ResultSet selectAllStudent() throws SQLException { String sql = "select * from Student"; DBOperator db = new DBOperator(); try { rs = db.executeQuery(sql,new Object[]{}); } catch (SQLException e) { e.printStackTrace(); } return rs; } /** * ������ݿ���ָ���ļ�¼ */ public Student selectStudentById(int id) throws SQLException { String sql = "select * from Student where id=?"; DBOperator db = new DBOperator(); Student cut = new Student(); try { rs = db.executeQuery(sql,new Object[]{id}); while(rs.next()){ cut.setId(rs.getInt("id")); cut.setName(rs.getString("name")); cut.setAge(rs.getInt("age")); cut.setSex(rs.getString("sex")); cut.setAddress(rs.getString("address")); } } catch (SQLException e) { e.printStackTrace(); } return cut; } /** * ģ���ѯ��ݿ��еļ�¼ */ public ResultSet findStudent(String name) throws SQLException { String sql = "select * from Student where name like ?"; Object[] obj = new Object[]{name}; DBOperator db = new DBOperator(); try { rs = db.executeQuery(sql,obj); } catch (SQLException e) { e.printStackTrace(); } return rs; } // public static void main(String[]args){ // StudentDAO s=new StudentDAO(); // Student stu=new Student(); // try { int n=s.deleteStudent(2); if(n!=0) System.out.println("�����ɹ���"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // try { Student student= s.selectStudentById(1); System.out.println(student.getName()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // stu.setId(5); stu.setAge(20); stu.setName("bb"); stu.setSex("��"); stu.setAddress("bbbbb"); try { int n=s.updateStudent(stu,3); if(n!=0) System.out.println("�����ɹ���"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // stu.setId(3); stu.setAge(20); stu.setName("aa"); stu.setSex("Ů"); stu.setAddress("aabbaa"); try { int n=s.insertStudent(stu); if(n!=0) System.out.println("�����ɹ���"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // stu.setId(2); try { int n=s.deleteStudent(stu); if(n!=0) System.out.println("�����ɹ���"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // try { ResultSet r=s.findStudent("%b"); while(r.next()){ System.out.println(r.getString("name")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // // } } util package com.test.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.PreparedStatement; /** * ������ݿ��� */ public class DBOperator { Connection conn; PreparedStatement stmt; ResultSet rs; /** * ���췽����ʼ����ݿ����� */ public DBOperator(){ try { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@58.116.170.91:1521:oracle"; conn = DriverManager.getConnection(url,"scott","tiger"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } /** * ����ݿ�����ɾ���IJ����ĵ��÷��� */ public int executeUpdate(String sql,Object[]obj) throws SQLException { stmt = conn.prepareStatement(sql); for (int i=0;i<obj.length;i++) { stmt.setObject(i+1,obj[i]); } int ri = stmt.executeUpdate(); return ri; } /** * ����ݿ���Ҳ����ĵ��÷��� */ public ResultSet executeQuery(String sql,Object[] obj) throws SQLException { stmt = conn.prepareStatement(sql); for (int i=0;i<obj.length;i++) { stmt.setObject(i+1,obj[i]); } rs = stmt.executeQuery(); return rs; } /** * �ر���ݿⷽ�����ͷ���Դ */ public void close() throws SQLException { if (conn != null){ conn.close(); } if (stmt != null){ stmt.close(); } if (rs != null){ rs.close(); } } } 主要显示页面 <%@ page contentType="text/html;charset=gbk" %> <%@page import="java.sql.ResultSet"%> <html> <head> <title>show</title> </head> <body> <div align="center"> <h3>学生信息表</h3> <table width="640" border="0" cellpadding="1" cellspacing="1" bgcolor="#003399"> <tr bgcolor="#FFFFFF"> <td width="90">编号</td> <td width="90">姓名 </td> <td width="90">年龄</td> <td width="90">性别</td> <td width="90">住址</td> <td width="90">修改 </td> <td width="90">删除</td> </tr> <% ResultSet rs= (ResultSet)request.getAttribute("rs");//做了强制转换 while(rs.next()){ %> <tr bgcolor="#FFFFFF"> <td><%=rs.getInt("id")%></td> <td><%=rs.getString("name")%></td> <td><%=rs.getInt("age")%></td> <td><%=rs.getString("sex")%></td> <td><%=rs.getString("address")%></td> <td><a href="ServletAction?type=select&id=<%=rs.getInt(" mce_href="ServletAction?type=select&id=<%=rs.getInt("id")%>">修改</a> </td> <td><a href="ServletAction?type=delete&id=<%=rs.getInt(" mce_href="ServletAction?type=delete&id=<%=rs.getInt("id")%>">删除</a> </td> </tr> <% } rs.close(); %> </table> <br><a href="insert.jsp" mce_href="insert.jsp">新增记录</a> </div> </body> </html> insert <%@ page contentType="text/html;charset=gbk" %> <html> <head> <title>show</title> <mce:script language="javascript"><!-- function add(){ var r1 = /^[1-9]/d*$/; var r2 = /(^[a-zA-Z_][^/W]{1,10}$)|(^[/u4e00-/u9fa5]{1,10}$)/; var r3=/^[1-9][0-9]{0,2}$/ var r4 = //男|/女|/male|/female/; var r5 = /^[/w]{2,}@[/w]+/.[/w]+$/; if(!r1.test(document.form.id.value)) { alert("输入的编号不合法!"); document.form.id.focus(); return false; } if(!r2.test(document.form.name.value)){ alert("输入的用户名不合法!"); document.form.name.focus(); return false; } if(!r3.test(document.form.age.value)){ alert("输入的年龄不合法!"); document.form.age.focus(); return false; } if(!r4.test(document.form.sex.value)){ alert("输入的性别不合法!"); document.form.sex.focus(); return false; } if(!r5.test(document.form.address.value)){ alert("请按照邮箱的格式输入地址!"); document.form.address.focus(); return false; } } // --></mce:script> </head> <body bgcolor="#ffffff"> <div align="center"> <h2>请填写下列相关信息</h2> <form name="form" action="ServletAction?type=insert" method="post" onSubmit="return add()"> <p>编号: <input type="text" name="id" > </p> <p>姓名: <input type="text" name="name"> </p> <p>年龄: <input type="text" name="age"> </p> <p>性别: <input type="text" name="sex"> </p> <p>住址: <input type="text" name="address"> </p> <p> <input type="submit" value="提交"> <input type="reset" value="重置"> </p> </form> </div> </body> </html> modify <%@ page contentType="text/html;charset=gbk" %> <%@ page import="com.test.bean.Student"%> <html> <head> <title>show</title> </head> <body bgcolor="#ffffff"> <div align="center"> <h2> 请填写下列相关信息</h2> <form action="ServletAction?type=modify" method="post"> <% Student stu= (Student)request.getAttribute("stu"); %> <input type="hidden" name="id" value="<%=request.getAttribute("id")%>"> <p>编号: <input type="text" name="num" value=<%=stu.getId()%>> </p> <p>姓名: <input type="text" name="name" value=<%=stu.getName()%>> </p> <p>年龄: <input type="text" name="age" value=<%=stu.getAge()%>> </p> <p>性别: <input type="text" name="sex" value=<%=stu.getSex()%>> </p> <p>住址: <input type="text" name="address" value=<%=stu.getAddress()%>> </p> <p> <input type="submit" value="提交"> <input type="reset" value="重置"> </p> </form> </div> </body> </html>