1、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)
2、 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 setPageS
3、ize(int pageSize) this.pageSize=pageSize; /获得和设置总页数 public int getTotalPage() return totalPage; public void setTotalPage(int totalRecord,int pageSize) if(totalRecord%pageSize=0) totalPage=totalRecord/pageSize; totalPage=totalRecord/pageSize+1; /获得和设置总记录 public int getTotalRecord() return totalRecord
4、; public void setTotalRecord(int totalRecord) this.totalRecord=totalRecord; 8,用相同的方法建一个StudentInfo类完整的StudentInfo.java代码如下public class StudentInfo private int id; /学号 private String name; /姓名 private int age; /年龄 private String gender; /性别 private String major; /专业 public StudentInfo() public Studen
5、tInfo(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) public String getName() return name; public void setName(String name) public int getAge() return age; p
6、ublic void setAge(int age) public String getGender() return gender; public void setGender(String gender) public String getMajor() return major; public void setMajor(String major)9,在src目录下添加另一个包dbservlet在该包中建立一个AllServlet类完整的AllServlet.java代码如下package dbservlet;import java.io.IOException;import java.
7、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;impo
8、rt 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, IOEx
9、ception request.setCharacterEncoding(UTF-8); response.setCharacterEncoding( String methodName=request.getParameter(methodName int method=Integer.parseInt(methodName); try switch(method) case 0: insert(request,response); case 1: difpage(request,response); break; case 2: delete(request,response); brea
10、k; case 3: update(request,response); break; case 4: update1(request,response); case 5: dispatch(request,response); catch (ClassNotFoundException e) / TODO Auto-generated catch block e.printStackTrace(); catch (SQLException e) /doGet方法 public void doGet(HttpServletRequest request, HttpServletResponse
11、 response) throws ServletException, IOException doPost(request,response); /数据库连接方法 public Connection connect() throws ClassNotFoundException, SQLException Connection conn=null; Class.forName(oracle.jdbc.driver.OracleDriver String url=jdbc:oracle:thin:localhost:1521:orcl; String user=scott String pas
12、sword=tiger conn=DriverManager.getConnection(url,user,password); return conn; /关闭数据库资源 public void close(Statement stat,Connection conn) throws SQLException if(stat!=null) stat.close(); if(conn! conn.close(); /插入方法 public void insert(HttpServletRequest request, HttpServletResponse response) throws ClassNotFou
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1