- 浏览: 26983 次
- 性别:
- 来自: 武汉
-
文章分类
最新评论
package com.svse.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//连接数据库
public class DBhelp {
private Connection conn;
private PreparedStatement pstm;
private ResultSet rs;
public Connection getConn()
{
String driver="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String user="scott";
String passwor="tiger";
try {
Class.forName(driver);
conn=DriverManager.getConnection(url, user, passwor);
if(conn!=null)
{
System.err.println("连接数据库成功");
}
else
{
System.err.println("连接数据库失败");
}
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
//关闭连接,释放资源
public void closeConn(Connection conn,PreparedStatement pstm,ResultSet rs) throws Exception
{
if(rs!=null)
{
rs.close();
}
if(pstm!=null)
{
pstm.close();
}
if(conn!=null)
{
conn.close();
}
}
public static void main(String[] args) {
DBhelp db=new DBhelp();
System.err.println(db.getConn());
}
}
---------------------------------------------------
package com.svse.entity;
import java.util.Date;
public class Student {
private int stuid;
private String stuname;
private String stusex;
private String stubirth;
private String stuaddr;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String stuaddr, String stubirth, int stuid, String stuname,
String stusex) {
super();
this.stuaddr = stuaddr;
this.stubirth = stubirth;
this.stuid = stuid;
this.stuname = stuname;
this.stusex = stusex;
}
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStusex() {
return stusex;
}
public void setStusex(String stusex) {
this.stusex = stusex;
}
public String getStubirth() {
return stubirth;
}
public void setStubirth(String stubirth) {
this.stubirth = stubirth;
}
public String getStuaddr() {
return stuaddr;
}
public void setStuaddr(String stuaddr) {
this.stuaddr = stuaddr;
}
}
---------------------------------------------------------
package com.svse.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.svse.entity.Student;
import com.svse.util.DBhelp;
public class StudentDao {
private Connection conn;
private PreparedStatement pstm;
private ResultSet rs;
DBhelp db=new DBhelp();
//添加
public boolean add(Student stu)
{
boolean flag=false;;
String sql="insert into Student values(?,?,?,?,?)";
conn=db.getConn();
if(conn!=null)
{
try {
pstm=conn.prepareStatement(sql);
pstm.setInt(1, stu.getStuid());
pstm.setString(1, stu.getStuname());
pstm.setString(3, stu.getStusex());
pstm.setString(4,stu.getStubirth());
pstm.setString(5, stu.getStuaddr());
int i=pstm.executeUpdate();
if(i>0)
{
flag=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}
//获得总行数(方法一)
public int getTotalCount()
{
String sql="select count(*) from Student";
conn=db.getConn();
try {
pstm=conn.prepareStatement(sql);
rs=pstm.executeQuery();
rs.next();
return rs.getInt(1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
db.closeConn(conn, pstm, rs);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return -1;
}
/*//查询总行数(方法二)
public int getTotalCount()
{
int i=0;
String sql="select * from Student";
conn=db.getConn();
List list=new ArrayList();
try {
pstm=conn.prepareStatement(sql);
rs=pstm.executeQuery();
while(rs.next())
{
Student stu=new Student();
stu.setStuid(rs.getInt(1));
stu.setStuname(rs.getString(2));
stu.setStusex(rs.getString(3));
stu.setStubirth(rs.getString(4));
stu.setStuaddr(rs.getString(5));
list.add(stu);
}
i=list.size();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
db.closeConn(conn, pstm, rs);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return i;
}*/
//分页显示
public List findByPage(int pageindex)
{
System.err.println("111111111111"+pageindex);
String sql="select * from Student where rownum<=3 and stuid not in (select stuid from Student where rownum<="+3*(pageindex-1)+")";
conn=db.getConn();
List list=new ArrayList();
try {
pstm=conn.prepareStatement(sql);
rs=pstm.executeQuery();
while(rs.next())
{
Student stu=new Student();
stu.setStuid(rs.getInt(1));
stu.setStuname(rs.getString(2));
stu.setStusex(rs.getString(3));
stu.setStubirth(rs.getString(4));
stu.setStuaddr(rs.getString(5));
list.add(stu);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
db.closeConn(conn, pstm, rs);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
}
------------------------------------------------
package com.svse.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.svse.dao.StudentDao;
public class StudentServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public StudentServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
StudentDao studao=new StudentDao();
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
String method = request.getParameter("met");
if (method.equals("studentshow")) {
//显示所有并分页
this.findAll(request, response);
}
}
//添加
public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
}
//查询所有
public void findAll(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
//当前页码
int pageIndex = 1;
// 最大页码
int lastPage = 0;
//获得最大行数
int i=studao.getTotalCount();
System.err.println("总行数"+i);
//在dao方法中设定每页显示5条数据
if(i%3==0)
{
lastPage=i/3;
}
else
{
lastPage=i/3+1;
}
//获得传如的pageindex
if(request.getParameter("pindex") == null)
{
//第一次进入pageIndex设为1
pageIndex = 1;
}
else
{
//以后进入,pageIndex从页面上接收
pageIndex = Integer.parseInt(request.getParameter("pindex"));
}
System.err.println("当前页"+pageIndex);
System.err.println("最大页数"+lastPage);
List list=studao.findByPage(pageIndex);
request.setAttribute("lists", list);
request.setAttribute("pageindex", pageIndex + "");
request.setAttribute("lastpage", lastPage + "");
System.err.println("-----------"+list.size());
request.getRequestDispatcher("/showslist.jsp").forward(request,
response);
}
public void init() throws ServletException {
// Put your code here
}
}
----------------------------------------------------------------
<table align="center" >
<tr>
<td>
<table border="0" align="center">
<tr>
<td>学生编号</td>
<td>姓名</td>
<td>性别</td>
<td>生日</td>
<td>地址</td>
</tr>
<c:forEach items="${lists}" var="student">
<tr>
<td align="center">${student.stuid}</td>
<td align="center">${student.stuname}</td>
<td align="center">${student.stusex}</td>
<td align="center">${student.stubirth}</td>
<td align="center">${student.stuaddr}</td>
</tr>
</c:forEach>
</table>
<p align="center">
<a href=studentServlet?met=studentshow&pindex=1>第一页</a>
<%
//获得当前页和最大页数
int pageIndex=Integer.parseInt((request.getAttribute("pageindex")).toString());
int lastPage=Integer.parseInt((request.getAttribute("lastpage")).toString());
//判断当前页是否大于1,如果大于1,说名不是第一页,可以点击上一页
if(pageIndex>1)
{
%>
<a href=studentServlet?met=studentshow&pindex=<%=pageIndex-1 %>>上一页</a>
<%
}
else
{
%>
上一页
<%
}
%>
<%
if(pageIndex<lastPage)
{
//判断当页是否小于最大页,如果当前页小于最大页数,说名不是最后一页,可以点击下一页
%>
<a href=studentServlet?met=studentshow&pindex=<%=pageIndex+1 %>>下一页</a>
<%
}
else
{
%>
下一页
<%
}
%>
<a href="studentServlet?met=studentshow&pindex=<%=lastPage%>">末 页</a>
</td>
</tr>
</table>
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//连接数据库
public class DBhelp {
private Connection conn;
private PreparedStatement pstm;
private ResultSet rs;
public Connection getConn()
{
String driver="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String user="scott";
String passwor="tiger";
try {
Class.forName(driver);
conn=DriverManager.getConnection(url, user, passwor);
if(conn!=null)
{
System.err.println("连接数据库成功");
}
else
{
System.err.println("连接数据库失败");
}
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
//关闭连接,释放资源
public void closeConn(Connection conn,PreparedStatement pstm,ResultSet rs) throws Exception
{
if(rs!=null)
{
rs.close();
}
if(pstm!=null)
{
pstm.close();
}
if(conn!=null)
{
conn.close();
}
}
public static void main(String[] args) {
DBhelp db=new DBhelp();
System.err.println(db.getConn());
}
}
---------------------------------------------------
package com.svse.entity;
import java.util.Date;
public class Student {
private int stuid;
private String stuname;
private String stusex;
private String stubirth;
private String stuaddr;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String stuaddr, String stubirth, int stuid, String stuname,
String stusex) {
super();
this.stuaddr = stuaddr;
this.stubirth = stubirth;
this.stuid = stuid;
this.stuname = stuname;
this.stusex = stusex;
}
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStusex() {
return stusex;
}
public void setStusex(String stusex) {
this.stusex = stusex;
}
public String getStubirth() {
return stubirth;
}
public void setStubirth(String stubirth) {
this.stubirth = stubirth;
}
public String getStuaddr() {
return stuaddr;
}
public void setStuaddr(String stuaddr) {
this.stuaddr = stuaddr;
}
}
---------------------------------------------------------
package com.svse.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.svse.entity.Student;
import com.svse.util.DBhelp;
public class StudentDao {
private Connection conn;
private PreparedStatement pstm;
private ResultSet rs;
DBhelp db=new DBhelp();
//添加
public boolean add(Student stu)
{
boolean flag=false;;
String sql="insert into Student values(?,?,?,?,?)";
conn=db.getConn();
if(conn!=null)
{
try {
pstm=conn.prepareStatement(sql);
pstm.setInt(1, stu.getStuid());
pstm.setString(1, stu.getStuname());
pstm.setString(3, stu.getStusex());
pstm.setString(4,stu.getStubirth());
pstm.setString(5, stu.getStuaddr());
int i=pstm.executeUpdate();
if(i>0)
{
flag=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}
//获得总行数(方法一)
public int getTotalCount()
{
String sql="select count(*) from Student";
conn=db.getConn();
try {
pstm=conn.prepareStatement(sql);
rs=pstm.executeQuery();
rs.next();
return rs.getInt(1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
db.closeConn(conn, pstm, rs);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return -1;
}
/*//查询总行数(方法二)
public int getTotalCount()
{
int i=0;
String sql="select * from Student";
conn=db.getConn();
List list=new ArrayList();
try {
pstm=conn.prepareStatement(sql);
rs=pstm.executeQuery();
while(rs.next())
{
Student stu=new Student();
stu.setStuid(rs.getInt(1));
stu.setStuname(rs.getString(2));
stu.setStusex(rs.getString(3));
stu.setStubirth(rs.getString(4));
stu.setStuaddr(rs.getString(5));
list.add(stu);
}
i=list.size();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
db.closeConn(conn, pstm, rs);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return i;
}*/
//分页显示
public List findByPage(int pageindex)
{
System.err.println("111111111111"+pageindex);
String sql="select * from Student where rownum<=3 and stuid not in (select stuid from Student where rownum<="+3*(pageindex-1)+")";
conn=db.getConn();
List list=new ArrayList();
try {
pstm=conn.prepareStatement(sql);
rs=pstm.executeQuery();
while(rs.next())
{
Student stu=new Student();
stu.setStuid(rs.getInt(1));
stu.setStuname(rs.getString(2));
stu.setStusex(rs.getString(3));
stu.setStubirth(rs.getString(4));
stu.setStuaddr(rs.getString(5));
list.add(stu);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
db.closeConn(conn, pstm, rs);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
}
------------------------------------------------
package com.svse.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.svse.dao.StudentDao;
public class StudentServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public StudentServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
StudentDao studao=new StudentDao();
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
String method = request.getParameter("met");
if (method.equals("studentshow")) {
//显示所有并分页
this.findAll(request, response);
}
}
//添加
public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
}
//查询所有
public void findAll(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
//当前页码
int pageIndex = 1;
// 最大页码
int lastPage = 0;
//获得最大行数
int i=studao.getTotalCount();
System.err.println("总行数"+i);
//在dao方法中设定每页显示5条数据
if(i%3==0)
{
lastPage=i/3;
}
else
{
lastPage=i/3+1;
}
//获得传如的pageindex
if(request.getParameter("pindex") == null)
{
//第一次进入pageIndex设为1
pageIndex = 1;
}
else
{
//以后进入,pageIndex从页面上接收
pageIndex = Integer.parseInt(request.getParameter("pindex"));
}
System.err.println("当前页"+pageIndex);
System.err.println("最大页数"+lastPage);
List list=studao.findByPage(pageIndex);
request.setAttribute("lists", list);
request.setAttribute("pageindex", pageIndex + "");
request.setAttribute("lastpage", lastPage + "");
System.err.println("-----------"+list.size());
request.getRequestDispatcher("/showslist.jsp").forward(request,
response);
}
public void init() throws ServletException {
// Put your code here
}
}
----------------------------------------------------------------
<table align="center" >
<tr>
<td>
<table border="0" align="center">
<tr>
<td>学生编号</td>
<td>姓名</td>
<td>性别</td>
<td>生日</td>
<td>地址</td>
</tr>
<c:forEach items="${lists}" var="student">
<tr>
<td align="center">${student.stuid}</td>
<td align="center">${student.stuname}</td>
<td align="center">${student.stusex}</td>
<td align="center">${student.stubirth}</td>
<td align="center">${student.stuaddr}</td>
</tr>
</c:forEach>
</table>
<p align="center">
<a href=studentServlet?met=studentshow&pindex=1>第一页</a>
<%
//获得当前页和最大页数
int pageIndex=Integer.parseInt((request.getAttribute("pageindex")).toString());
int lastPage=Integer.parseInt((request.getAttribute("lastpage")).toString());
//判断当前页是否大于1,如果大于1,说名不是第一页,可以点击上一页
if(pageIndex>1)
{
%>
<a href=studentServlet?met=studentshow&pindex=<%=pageIndex-1 %>>上一页</a>
<%
}
else
{
%>
上一页
<%
}
%>
<%
if(pageIndex<lastPage)
{
//判断当页是否小于最大页,如果当前页小于最大页数,说名不是最后一页,可以点击下一页
%>
<a href=studentServlet?met=studentshow&pindex=<%=pageIndex+1 %>>下一页</a>
<%
}
else
{
%>
下一页
<%
}
%>
<a href="studentServlet?met=studentshow&pindex=<%=lastPage%>">末 页</a>
</td>
</tr>
</table>
相关推荐
**标题:“jsp+servlet分页”** 在Web开发中,数据分页是一种常见的技术,用于在用户界面上显示大量数据时提高性能和用户体验。这里我们主要讨论如何在Java Web环境中,利用JSP(JavaServer Pages)和Servlet进行...
在Java Web开发中,"jsp+servlet 分页"是一个常见的技术组合,用于处理大量数据的显示问题。在网站或应用程序中,如果一次性加载所有数据,不仅会消耗大量资源,也会影响用户体验。因此,分页是一种优化策略,它将...
项目主体结构是dao+db+filter+pojo+servlet, 使用技术Servlet转发,代码中有注释帮助学者理解,数据库为MySQL资源内含数据库的sql。实现的数据库内容分页,查询分页,对初学者难点是根据get请求的中的url地址进行...
在这个“jsp + servlet分页代码”中,我们很显然关注的是如何实现数据库查询结果的分页展示。分页是一种有效的优化策略,它能帮助用户更方便地浏览大量数据,同时减轻服务器负载。 首先,我们需要理解JSP和Servlet...
【标题】:“jsp+servlet分页程序” 在Web开发中,使用JSP(JavaServer Pages)和Servlet技术处理用户请求并展示动态内容是常见的做法。本分页程序结合了这两种技术,以提供高效的数据库查询结果分页显示。通过集成...
在本文中,我们将深入探讨如何使用JSP、Servlet和MyBatis这三种技术来实现一个分页查询的功能。这是一个常见的需求,在许多Web应用程序中,为了提高用户体验,通常需要将大量数据分批次展示,而不是一次性加载所有...
在JavaWeb开发中,"JavaWeb+JSP+Servlet+JDBC分页查询和查询后分页界面优化"是一个常见的需求,特别是在构建大型的、数据密集型的学生管理系统中。这个主题涵盖了许多关键知识点,让我们逐一深入探讨。 首先,...
在这个"分页jsp+servlet"的项目中,我们将探讨如何使用Java Web技术实现分页功能。 首先,我们需要了解JSP(JavaServer Pages)和Servlet的基本概念。JSP是Java的一个动态网页技术,允许开发者将HTML代码与Java代码...
在IT领域,尤其是在Web开发中,"jsp+servlet+mysql实现简单的增删改查+分页"是一个常见的学习主题,适合初学者理解基础的后端开发流程。这个组合提供了从客户端到服务器端再到数据库的数据交互能力,同时也包含了...
这需要在Servlet中计算总页数,然后根据用户请求的页码发送对应的查询请求,JSP页面则负责展示分页链接。 6. **购物车实现**:购物车的实现涉及多个步骤。首先,用户将商品添加到购物车时,Servlet会创建或更新对应...
本项目“jsp+servlet+javabean 分页”旨在提供一种简单而实用的分页解决方案,帮助开发者高效地处理大量数据的展示问题。下面将详细介绍这三个技术及其在分页实现中的应用。 首先,JSP是一种基于Java的服务器端脚本...
使用Servlet+Jsp+JDBC实现增删改查,前台显示界面使用JavaScript、Ajax与后台交互进行登录验证,并对查询结果实现分页、局部刷新,以及复选框(全选、反选)操作,适合Web开发的初学者
【基于JSP+Servlet图书管理系统】是一个典型的Web应用程序开发实例,它利用了Java技术栈中的核心组件来构建一个用于管理图书信息的系统。系统的主要功能包括图书的添加、删除、修改和查询,以及用户管理等。以下是这...
本文将深入探讨`JSP(JavaServer Pages)、Servlet、JavaBean`以及`Ajax`如何在MVC架构下实现数据的增删改查与分页显示,并涉及`SmartUpload`组件用于文件和图片的上传。 首先,`MVC`模式是一种软件设计模式,它将...
JSP+SERVLET+MYSQL+分页,有详细的增删改查功能,适用于初学者
"JSP+Servlet+JavaBean查询分页"的主题涵盖了如何在处理大量数据时,有效地展示信息并允许用户进行分页浏览。在这个主题中,我们将深入探讨这三个关键组件在分页查询中的应用。 首先,JSP是Java的一个标准视图技术...
在Java Web开发中,"利用servlet+jsp+javabean进行分页查询"是一个常见的技术实践,主要用于处理大量数据的展示,提升用户体验并减轻服务器压力。本篇将详细讲解这个过程中的关键知识点。 首先,我们需要理解...
这是一个jsp+javaBean+servlet分页 主要功能 是通过 jstl标签中的 中的开始 begin和末尾end、list结果集的items属性 操作整个分页。大家有兴趣看看