洞察在数字化转型过程中,信创推动企业有效整合资源,实现低成本、高效率的跨平台小程序运营。
656
2022-10-28
Servlet框架(servlet+jsp)+Mysql实现的增删改查+分页(功能包学生信息录入、学生信息增删改查、分页等)
@TOC
Servlet框架(servlet+jsp)+Mysql实现的增删改查+分页
本系统实现了一个java的servlet框架+jsp的学生信息增删改查+分页的实例,简单易懂,特别适合刚学servlet的伙伴。
实现功能截图
系统功能
本会议管理系统实现了以下功能:1、学生信息录入2、录入成功3、学生信息列表4、分页5、学生信息编辑6、学生信息删除
使用技术
数据库:mysql开发工具:Eclipse(Myeclispe、Idea也可以)知识点:servlet+jsp
代码
实体类Page.java
package bean; public class Page { private int totalPage; private int currentPage; private int totalRecord; private int currentRecord; private int pageSize=8; //获得和设置当前页 public int getCurrentPage(){ return currentPage; } public void setCurrentPage(int currentRecord,int pageSize){ if(currentRecord%pageSize==0){ currentPage=currentRecord/pageSize; } else{ currentPage=currentRecord/pageSize+1; } } //获得和设置当前记录 public int getCurrentRecord(){ return currentRecord; } public void setCurrentRecord(int currentRecord){ this.currentRecord=currentRecord; } //获得和设置每页记录数量 public int getPageSize(){ return pageSize; } public void setPageSize(int pageSize){ this.pageSize=pageSize; } //获得和设置总页数 public int getTotalPage(){ return totalPage; } public void setTotalPage(int totalRecord,int pageSize){ if(totalRecord%pageSize==0){ totalPage=totalRecord/pageSize; } else{ totalPage=totalRecord/pageSize+1; } } //获得和设置总记录 public int getTotalRecord(){ return totalRecord; } public void setTotalRecord(int totalRecord){ this.totalRecord=totalRecord; } }
StudentInfo.java
package bean; public class StudentInfo { private int id; //学号 private String name; //姓名 private int age; //年龄 private String gender; //性别 private String major; //专业 public StudentInfo(){ } public StudentInfo(int id,String name,int age,String gender,String major){ this.id=id; this.name=name; this.age=age; this.gender=gender; this.major=major; } public int getId(){ return id; } public void setId(int id){ this.id=id; } public String getName(){ return name; } public void setName(String name){ this.name=name; } public int getAge(){ return age; } public void setAge(int age){ this.age=age; } public String getGender(){ return gender; } public void setGender(String gender){ this.gender=gender; } public String getMajor(){ return major; } public void setMajor(String major){ this.major=major; } }
servlet 类AllServlet.java:
package dbservlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.Page;
import bean.StudentInfo;
public class AllServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
//doPost方法
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String methodName=request.getParameter("methodName");
int method=Integer.parseInt(methodName); //将字符串解析为数字
try {
switch(method)
{
case 0:
insert(request,response);
break;
case 1:
difpage(request,response);
break;
case 2:
delete(request,response);
break;
case 3:
update(request,response);
break;
case 4:
update1(request,response);
break;
case 5:
dispatch(request,response);
break;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
//doGet方法
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
//数据库连接方法
public Connection connect() throws ClassNotFoundException, SQLException{
Connection conn=null;
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/springside";
String user="root";
String password="xxxxx";
conn=DriverManager.getConnection(url,user,password);
return conn;
}
//关闭数据库资源
public void close(Statement stat,Connection conn) throws SQLException{
if(stat!=null){
stat.close();
}
if(conn!=null){
conn.close();
}
}
//插入方法
public void insert(HttpServletRequest request, HttpServletResponse response)
throws ClassNotFoundException, SQLException , IOException,ServletException{
Connection conn=null;
Statement stat=null;
String id=request.getParameter("id");
String name=request.getParameter("name");
String age=request.getParameter("age");
String gender=request.getParameter("gender");
String major=request.getParameter("major");
conn=connect();
stat=conn.createStatement();
stat.execute("insert into student(id,name,age,gender,major) values("+id+",'"+name+"',"+age+",'"+gender+"','"+major+"')");
close(stat,conn);
request.getRequestDispatcher("InsertSuccess.jsp").forward(request, response);
}
//查询方法
public ArrayList
写在最后
如果运行代码中遇到问题,可以加博主个人主页交流
觉得有用,记得一键三连哦!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~