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

📄 persondaoimpl.java

📁 本案例重点讲解了如何Struts中实现分页程序
💻 JAVA
字号:
package org.lxh.impl;

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

import org.lxh.dao.PersonDAO;
import org.lxh.dbc.DataBaseConnection;
import org.lxh.vo.Person;

public class PersonDAOImpl implements PersonDAO {
	private DataBaseConnection dbc = null;

	public PersonDAOImpl() {
		this.dbc = new DataBaseConnection();
	}

	public int getAllCount() throws Exception {
		int count = 0;
		String sql = "SELECT COUNT(id) from person";
		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) {
			throw e;
		}
		return count;
	}

	public int getByLikeCount(String cond) throws Exception {
		int count = 0;
		String sql = "SELECT COUNT(id) from person WHERE uid LIKE ? OR name 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) {
			throw e;
		}
		return count;
	}

	public List queryAll(int currentPage, int lineSize) throws Exception {
		List<Person> all = new ArrayList<Person>();
		String sql = "SELECT id,uid,name,password FROM person limit "
				+ (currentPage - 1) * lineSize + "," + lineSize;
		PreparedStatement pstmt = null;
		try {
			pstmt = this.dbc.getConnection().prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				Person p = new Person();
				p.setId(rs.getInt(1));
				p.setUid(rs.getString(2));
				p.setName(rs.getString(3));
				p.setPassword(rs.getString(4));
				all.add(p);
			}
			rs.close();
			pstmt.close();
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return all;
	}

	public List queryByLike(String cond, int currentPage, int lineSize)
			throws Exception {
		List<Person> all = new ArrayList<Person>();
		String sql = "SELECT id,uid,name,password FROM person WHERE uid LIKE ? OR name 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()) {
				Person p = new Person();
				p.setId(rs.getInt(1));
				p.setUid(rs.getString(2));
				p.setName(rs.getString(3));
				p.setPassword(rs.getString(4));
				all.add(p);
			}
			rs.close();
			pstmt.close();
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbc.close();
		}
		return all;
	}
};

⌨️ 快捷键说明

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