Content

jsp+mysql实现网页的分页查询(jsp数据库分页)硬核推荐

文章摘要

这篇文章介绍了名为 `AuthorDao` 的 Java 类,用于对作者数据进行操作。该类包括以下主要方法及其功能: 1. `check(String username, int password)`:根据用户名和密码查询并返回对应的 `Author` 对象。 2. `queryAuthorList()`:从数据库中获取所有 `Author` 对象并返回一个 `List`。 3. `queryUserListCount()`:查询数据库中 `Author` 对象的数量。 4. `queryUserListPage(int pageIndex, int pageSize)`:对 `Author` 对象进行分页查询,返回每页的结果。 5. `add(Author obj)`:向数据库中新增一个 `Author` 对象。 6. `del(int id)`:删除指定 `id` 的 `Author` 对象。 文章还提到了对 JDBC 数据库操作的常用方法,如使用 `ResultSet` 和 `PreparedStatement` 来执行查询和更新操作,并对可能出现的 `SQLException` 进行了处理。整体来看,该类实现了数据库访问接口(如 DAO 接口),用于管理 `Author` 数据的增删查改操作。

package com.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.entity.Author; public class AuthorDao { ? ? public ?Author check(String username ,int ?password ) { ? ? ? Author obj=null ; ? ? try { ? ? ?DBConnection db=new DBConnection(); ? ? ?//获取数据库连接 ? ? ?Connection conn=db.getConn(); ? ? ? ? ? ?String sql=”select *from furnitures where name=? and id=?”; ? ? ? ? ? ?PreparedStatement ps=conn.prepareStatement(sql); ? ? ?//设置用户名和密码作为参数放入sql语句 ? ? ?ps.setString(1,username); ? ? ?ps.setInt(2,password); ? ? ?//执行查询语句 ? ? ?ResultSet rs=ps.executeQuery(); ? ? ?//用户名和密码正确,查到数据 ?欧式风格 ?茶几 ? ? ?if(rs.next()) { ? ? ? ?obj=new Author(); ? ? ? ?obj.setId(rs.getInt(1)); ? ? ? ?obj.setName(rs.getString(2)); ? ? ? ?obj.setPrice(rs.getInt(3)); ? ? ? ?obj.setNum(rs.getInt(4)); ? ? ? ?obj.setDates(rs.getString(5)); ? ? ? ?obj.setStyle(rs.getString(6)); ? ? ?} ? ?} catch (SQLException e) { ? ? ?// TODO Auto-generated catch block ? ? ?e.printStackTrace(); ? ?} ? ? return obj; ? } ? ? public List<Author> queryAuthorList(){ ? ? Author obj=null ; ? ? List<Author> list=new ArrayList<Author>(); ? ? try { ? ? ?DBConnection db=new DBConnection(); ? ? ?//获取数据库连接 ? ? ?Connection conn=db.getConn(); ? ? ? ? ? ?String sql=”select *from furnitures”; ? ? ? ? ? ?PreparedStatement ps=conn.prepareStatement(sql); ? ? ? ?//执行查询语句 ? ? ?ResultSet rs=ps.executeQuery(); ? ? ?//用户名和密码正确,查到数据 ?欧式风格 ?茶几 ? ? ?//循环遍历获取用户信息 ? ? ?while(rs.next()) { ? ? ? ? ? ? ? ?obj=new Author(); ? ? ? ?obj.setId(rs.getInt(1)); ? ? ? ?obj.setName(rs.getString(2)); ? ? ? ?obj.setPrice(rs.getInt(3)); ? ? ? ?obj.setNum(rs.getInt(4)); ? ? ? ?obj.setDates(rs.getString(5)); ? ? ? ?obj.setStyle(rs.getString(6)); ? ? ? ?//将对象加入list里边 ? ? ? ?list.add(obj); ? ? ?} ? ?} catch (SQLException e) { ? ? ?// TODO Auto-generated catch block ? ? ?e.printStackTrace(); ? ?} ? ? return list; ? } ? ? public int queryUserListCount() { ? ? DBConnection db; ? ?try { ? ? ? ? db=new DBConnection(); ? ? ? Connection conn=db.getConn(); ? ? ? String sql=”select count(*) from furnitures”; ? ? ? ? ? PreparedStatement ps=conn.prepareStatement(sql); ? ? ? ResultSet rs=ps.executeQuery(); ? ? ? ? ? ? ? if(rs.next()) { ? ? ? ? return rs.getInt(1); ? ? ? } ? ? ? ? ? ?} catch (SQLException e) { ? ? ?// TODO Auto-generated catch block ? ? ?e.printStackTrace(); ? ?} ? ? ? ? return 0; ? } ? ? public List<Author>queryUserListPage(int pageIndex,int pageSize){ ? ? ? Author obj=null; ? ? List<Author> list=new ArrayList<Author>(); ? ? ? try { ? ? ?Connection conn=new DBConnection().getConn(); ? ? ?String sql=”select * from furnitures limit ?,?;”; ? ? ?PreparedStatement ps=conn.prepareStatement(sql); ? ? ?ps.setObject(1, pageIndex); ? ? ?ps.setObject(2,pageSize); ? ? ? ? ? ?ResultSet rs=ps.executeQuery(); ? ? ?//遍历结果集获取用户列表数据 ? ? ? ? ? ?while(rs.next()) { ? ? ? ?obj=new Author(); ? ? ? ? ? ? ? ?obj.setId(rs.getInt(1)); ? ? ? ?obj.setName(rs.getString(2)); ? ? ? ?obj.setPrice(rs.getInt(3)); ? ? ? ?obj.setNum(rs.getInt(4)); ? ? ? ?obj.setDates(rs.getString(5)); ? ? ? ?obj.setStyle(rs.getString(6)); ? ? ? ? ? ? ? ?list.add(obj); ? ? ?} ? ?} catch (SQLException e) { ? ? ?// TODO Auto-generated catch block ? ? ?e.printStackTrace(); ? ?} ? ? return list; ? } ? ? public void add(Author obj) { ? ? ? ?try { ? ? ? ? ? ?DBConnection db=new DBConnection(); ? ? ?//获取数据库连接 ? ? ?Connection conn=db.getConn(); ? ? ? ? ? ?String sql=”insert into furnitures values(id,?,?,?,?,?)”; ? ? ? ? ? ?PreparedStatement ps=conn.prepareStatement(sql); ? ? ?ps.setObject(1, obj.getName()); ? ? ?ps.setObject(2, obj.getPrice()); ? ? ?ps.setObject(3, obj.getNum()); ? ? ?ps.setObject(4,obj.getDates()); ? ? ?ps.setObject(5, obj.getStyle()); ? ? ? ? ? ?//执行sql语句 ? ? ? ps.execute(); ? ? ? ? ? ? ?} catch (SQLException e) { ? ? ?// TODO Auto-generated catch block ? ? ?e.printStackTrace(); ? ?} ? ? ? ? } ? //删除用户 ? public void del(int id) { ? ? try { ? ? ? ? ? ? ? ?DBConnection db=new DBConnection(); ? ? ? ?//获取数据库连接 ? ? ? ?Connection conn=db.getConn(); ? ? ? ? ? ? ? ?String sql=”delete from furnitures where id=?”; ? ? ? ? ? ? ? ?PreparedStatement ps=conn.prepareStatement(sql); ? ? ? ? ? ? ? ?ps.setObject(1, id); ? ? ? ? ? ? ? ?//执行sql语句 ? ? ? ? ps.execute(); ? ? ? ? ? ? ? ? ? ?} catch (SQLException e) { ? ? ? ?// TODO Auto-generated catch block ? ? ? ?e.printStackTrace(); ? ? ?} ? ? ? ? ? } ? }