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

随心笔谈11个月前发布 admin
90 0

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();
? ? ?}
? ? ? ?
? }
?
}

© 版权声明

相关文章