免费预览已结束,剩余24页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
学生管理信息系统Servlet版(Eclipse)1、 新建Dynamic Web Project,命名StudentServlet 2、 配置项目1)右击项目-Properties-修改Java Builder Path-Browse-在Webcontent下的Web-INF下新建classes文件夹,并选择.2)复制粘贴两个包到/WEB-INF/lib下,如图:3)修改字符集,Properties-Resource-将GBK改为UTF-83、 编写测试类/src下新建包,包名为com.ls.test,在此包下新建Servlet,创建继承与HttpServlet类的HelloWorld测试类。代码如下:package com.ls.test;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/* * Servlet implementation class HelloWorld */WebServlet(/HelloWorld)public class HelloWorld extends HttpServlet private static final long serialVersionUID = 1L; /* * see HttpServlet#HttpServlet() */ public HelloWorld() super(); / TODO Auto-generated constructor stub /* * see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stubresponse.setContentType(text/html;charset=UTF8);PrintWriter out= response.getWriter();out.println(HelloWorld);/* * see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stub运行测试类,结果如图:4、新建名为DataBase的folder,用于存放数据库。将数据库Student脚本文件复制粘贴到/DataBase下:5、各页面代码:1)、com.ls.test下显示所有记录的测试类DisplayServletpackage com.ls.test;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/* * Servlet implementation class addStudentServlet */WebServlet(/disp)public class DisplayServlet extends HttpServlet private static final long serialVersionUID = 1L; /* * see HttpServlet#HttpServlet() */ public DisplayServlet() super(); / TODO Auto-generated constructor stub /* * see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stubdoPost(request,response);/* * see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stubresponse.setContentType(text/html;charset=UTF-8);PrintWriter out=response.getWriter();String connStr = jdbc:mysql:/localhost:3306/Student?characterEncoding=utf8;Connection conn = null ;Statement stmt = null ; ResultSet rs;try Class.forName(com.mysql.jdbc.Driver);conn = DriverManager.getConnection(connStr,root, 123456);stmt = conn.createStatement(); catch (Exception ex) ex.printStackTrace();System.out.println(exception in getConnection);String sql=select * from studentinfo;try rs = stmt.executeQuery(sql);out.println();out.println(编号姓名年龄);while (rs.next()out.println(+rs.getString(id)+rs.getString(name)+rs.getInt(age)+);out.println();stmt.close();conn.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace(); 2)com.ls.student.bean下的实体类Student.javapackage com.ls.student.bean;public class Student private String id,name;private int age;public Student() super();public Student(String id, String name, int age) super();this.id = id; = name;this.age = age;public String getId() return id;public void setId(String id) this.id = id;public String getName() return name;public void setName(String name) = name;public int getAge() return age;public void setAge(int age) this.age = age;Overridepublic String toString() return Student id= + id + , name= + name + , age= + age + ;3)连接数据库的SQLHelper类package com.ls.student.bean;import java.sql.*;public class SQLHelper private String driverStr = com.mysql.jdbc.Driver;private String connStr = jdbc:mysql:/localhost:3306/Student?characterEncoding=utf8;private String dbusername = root;private String dbpassword = 123456;private Connection conn = null;private Statement stmt = null;public SQLHelper() try Class.forName(driverStr);conn = DriverManager.getConnection(connStr, dbusername, dbpassword);stmt = conn.createStatement(); catch (Exception ex) System.out.println(exception in SQLHelper);public int executeUpdate(String sql) int result = 0;try result = stmt.executeUpdate(sql); catch (Exception ex) System.out.println(Exception in executeUpdate);return result;public ResultSet executeQuery(String sql) ResultSet rs = null;try rs = stmt.executeQuery(sql);if(!rs.next() return null;rs.previous(); catch (Exception ex) System.out.println(Exception in executeQuery);return rs;public void close() try stmt.close();conn.close(); catch (Exception e) 4)com.ls.servlet下的addStudentServlet.javapackage com.ls.student.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.ls.student.bean.SQLHelper;/* * Servlet implementation class addStudentServlet */WebServlet(/add)public class addStudentServlet extends HttpServlet private static final long serialVersionUID = 1L; /* * see HttpServlet#HttpServlet() */ public addStudentServlet() super(); / TODO Auto-generated constructor stub /* * see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stubdoPost(request,response);/* * see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stubresponse.setContentType(text/html;charset=UTF-8);request.setCharacterEncoding(UTF-8);/PrintWriter out=response.getWriter();String id=request.getParameter(id);String name=request.getParameter(name);String age= request.getParameter(age);SQLHelper db= new SQLHelper();String sql=insert into studentinfo values(+id+,+name+,+age+);db.executeUpdate(sql);request.getRequestDispatcher(/list2).forward(request, response);db.close(); 5)com.ls.servlet下的deleteStudentServlet.javapackage com.ls.student.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.ls.student.bean.SQLHelper;/* * Servlet implementation class deleteStudentServlet */WebServlet(/delete)public class deleteStudentServlet extends HttpServlet private static final long serialVersionUID = 1L; /* * see HttpServlet#HttpServlet() */ public deleteStudentServlet() super(); / TODO Auto-generated constructor stub /* * see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stubdoPost(request,response);response.setContentType(text/html;charset=UTF-8);request.setCharacterEncoding(UTF-8);/PrintWriter out=response.getWriter();String id=request.getParameter(id);SQLHelper db= new SQLHelper();String sql=delete from studentinfo where id =+id+;db.executeUpdate(sql);request.getRequestDispatcher(/list2).forward(request, response);db.close(); /* * see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stub6)com.ls.servlet下的listStudentServlet.javapackage com.ls.student.servlet;import java.io.IOException;import java.io.PrintWriter;import java.sql.ResultSet;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.ls.student.bean.SQLHelper;/* * Servlet implementation class listStudentServlet */WebServlet(/listall)public class listStudentServlet extends HttpServlet private static final long serialVersionUID = 1L; /* * see HttpServlet#HttpServlet() */ public listStudentServlet() super(); / TODO Auto-generated constructor stub /* * see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stubdoPost(request,response);/* * see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stubresponse.setContentType(text/html;charset=UTF-8);PrintWriter out=response.getWriter();SQLHelper db= new SQLHelper();String sql=select * from studentinfo;ResultSet rs=db.executeQuery(sql);out.println();out.println(编号姓名年龄);try while (rs.next()out.println(+rs.getString(id)+rs.getString(name)+rs.getInt(age)+); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();out.println();db.close(); 7) com.ls.servlet下的listStudentServlet2.javapackage com.ls.student.servlet;import java.io.IOException;import java.io.PrintWriter;import java.sql.ResultSet;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.ls.student.bean.SQLHelper;/* * Servlet implementation class listStudentServlet2 */WebServlet(/list2)public class listStudentServlet2 extends HttpServlet private static final long serialVersionUID = 1L; /* * see HttpServlet#HttpServlet() */ public listStudentServlet2() super(); / TODO Auto-generated constructor stub /* * see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stubdoPost(request,response);/* * see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stubresponse.setContentType(text/html;charset=UTF-8);PrintWriter out=response.getWriter();/String path=request.getContextPath()SQLHelper db= new SQLHelper();String sql=select * from studentinfo;ResultSet rs=db.executeQuery(sql);out.println();out.println(编号姓名年龄操作);try while (rs.next()String id=rs.getString(id);out.println(+rs.getString(id)+rs.getString(name)+rs.getInt(age)+ 修改 + + 删除); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();out.println();db.close(); 8) com.ls.servlet下的updateDoStudentServlet.javapackage com.ls.student.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.ls.student.bean.SQLHelper;/* * Servlet implementation class updateDoStudentServlet */WebServlet(/update.do)public class updateDoStudentServlet extends HttpServlet private static final long serialVersionUID = 1L; /* * see HttpServlet#HttpServlet() */ public updateDoStudentServlet() super(); / TODO Auto-generated constructor stub /* * see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stubdoPost(request,response);/* * see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stubresponse.setContentType(text/html;charset=UTF-8);request.setCharacterEncoding(UTF-8);PrintWriter out=response.getWriter();String id=request.getParameter(id);String name=request.getParameter(name);String age=request.getParameter(age);SQLHelper db= new SQLHelper();String sql=update studentinfo set name=+ name+,age=+age+ where id =+id+;/System.out.println(sql);db.executeUpdate(sql);request.getRequestDispatcher(/list2).forward(request, response);db.close(); 9) com.ls.servlet下的updateStudentServlet.javapackage com.ls.student.servlet;import java.io.IOException;import java.io.PrintWriter;import java.sql.ResultSet;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.ls.student.bean.SQLHelper;/* * Servlet implementation class updateStudentServlet */WebServlet(/update)public class updateStudentServlet extends HttpServlet private static final long serialVersionUID = 1L; /* * see HttpServlet#HttpServlet() */ public updateStudentServlet() super(); / TODO Auto-generated constructor stub /* * see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stubdoPost(request,response);/* * see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / TODO Auto-generated method stubresponse.setContentType(text/html;charset=UTF-8);request.setCharacterEncoding(UTF-8);PrintWriter out=response.getWriter();String id=request.getParameter(id);SQLHel
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 公司治理理论知识考核试题
- 中小学生心理健康测试题
- 公路造价工程师考试模拟试题及答案
- 大学体育理论考试题库与答案
- 临汾市尧都区职业技术学校教师招聘考试真题2022
- 《法律职业伦理》试卷及答案
- 2025年申论丙级试卷真题答案
- 中央电大法学本科国际经济法期末考试题库
- 2025法硕专业课模拟试卷及答案
- 2010年主管药师资格考试基础知识模拟试题
- 2025年陕西有色金属科工贸服务有限公司招聘(24人)考试笔试参考题库附答案解析
- 2025广东深圳市宝安区审计局招聘机关事业单位编外人员1人笔试考试参考题库及答案解析
- 新车托运协议书模板
- 陕西有色校招笔试题及答案
- 医院会计笔试题库及答案
- 厂房施工进度汇报
- 铝合金门窗安装工程专项施工方案正式版
- 物业会所经营管理
- 2025年学校副校级后备干部考试题及答案
- 2025超市蔬菜供应模型合同
- 休闲体育活动管理
评论
0/150
提交评论