⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookdaoimpl.java

📁 基于Struts的分页功能
💻 JAVA
字号:
package com.zhku.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.zhku.dao.BookDao;
import com.zhku.dbc.DBConnection;
import com.zhku.vo.Book;

public class BookDaoImpl implements BookDao {
       private DBConnection dbc=null;
       
       public BookDaoImpl(){
    	   this.dbc=new DBConnection();
       }
       
	public int getAllCount() throws Exception {
		int count=0;
		String sql="select count(id) from book";
	
		PreparedStatement pstmt=null;
		
		try {
			pstmt = this.dbc.getConnection().prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()) {
				count = rs.getInt(1);
			}
			pstmt.close();
			rs.close();
		} catch (Exception e) {
			e.getStackTrace();
		}		
		System.out.println("count"+count);
		return count;
	}

	public int getLikeCount(String cond) throws Exception {
		int count=0;
		String sql="select count(id) from book where id like ? or ok like ?";
		PreparedStatement pstmt=null;
		
		try {
			pstmt = this.dbc.getConnection().prepareStatement(sql);
			pstmt.setString(1, "%" + cond + "%");
			pstmt.setString(2, "%" + cond + "%");
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()) {
				count=rs.getInt(1);
			}
			pstmt.close();
			rs.close();
		} catch (Exception e) {
			e.getStackTrace();
		}		
	System.out.println("c1"+count);
		return count;
	}

	public List queryAll(int currentPage, int lineSize) throws Exception {
	    List all=new ArrayList();
	    String sql="select * from book limit "+(currentPage-1)*lineSize+","+lineSize;
		PreparedStatement pstmt=null; 
		System.out.println("sssssssssssss="+sql);
		try {
			pstmt = this.dbc.getConnection().prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()) {
				Book book = new Book();
				book.setId(rs.getInt(1));
				book.setOk(rs.getString(2));
				book.setAuthor(rs.getString(3));
				book.setPrice(rs.getString(4));
				all.add(book);
			}
			rs.close();
			pstmt.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return all;
	}

	public List queryLikeAll(String cond, int currentPage, int lineSize)
			throws Exception {
		List all=new ArrayList();
		String sql="select id,ok,author,price from book where id like ? or ok like ? limit "+(currentPage-1)*lineSize+","+lineSize;
        PreparedStatement pstmt=null;
        try {
			pstmt = this.dbc.getConnection().prepareStatement(sql);
			pstmt.setString(1,"%" +cond+"%");
			pstmt.setString(2,"%"+ cond+"%");
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				Book book = new Book();
				book.setId(rs.getInt(1));
				book.setOk(rs.getString(2));
				book.setAuthor(rs.getString(3));
				book.setPrice(rs.getString(4));
				System.out.println("rs"+rs.getString(4));
				all.add(book);
			}
			rs.close();
			pstmt.close();
		} catch (Exception e) {
			e.getStackTrace();
		}        
		System.out.println("size"+all.size());
		return all;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -