使用技术:
1)mysql数据库基础
2)C3P0连接池(开源框架)
3)dbUtils(第三方框架)
4)jsp基础
用到的第三方jar包:
C3P0连接池核心jar包:C3P0-0.9.1.0.jar
注意:C3P0使用配置文件方式实现,需在src目录下存放配置未见(c3p0-config.xml)
项目src包下 C3P0 配置问价格式如下(文件名:c3p0-config.xml):
---------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
连接mysql数据的驱动jar包:mysql-connector-java-5.1.12-bin.jar
dbutils第三方jar包:commons-dbutils-1.6.jar
Jstl标签库的jar包:jstl-1.2.jar(若新建项目中没有带,则自行导入)
注意:使用javaee5.0的项目自动导入jstl支持jar包
-----------------------------JdbcUntils类代码如下(操作数据库的类)--------------------------------
package com.wxw.people.untils;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JdbcUntils {
/*
* 1:初始化 C3P0 连接池
* */
private static DataSource dataSource;
static {
dataSource = new ComboPooledDataSource();
}
/*
* 2:创建DbUntils核心工具类对象
* */
public static QueryRunner getQueryRunner(){
// 创建QueryRunner对象,传入连接池对象
// 在创建QueryRunner对象的时候,如果传入了数据源对象;
// 那么在使用QueryRunner对象方法的时候,就不需要传入连接对象;
// 会自动从数据源中获取连接(不用关闭连接)
QueryRunner queryRunner = new QueryRunner(dataSource);
return queryRunner;
}
@Test
public void test(){
String sql = "insert into student(id,name,age,gender) values(?,?,?,?)";
for(int i = 8 ; i <= 100; i++){
if(i%2==0){
try {
getQueryRunner().update(sql, i,"Lucy" + i,i,"女" );
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try {
getQueryRunner().update(sql, i,"Lucy" + i,i,"男" );
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
-----------------------------PageBean类代码如下(存储返回页数,查询到的分页数据等)--------------------------------
package com.wxw.people.untils;
import java.util.List;
public class PageBean
private int currentPage = 1; //当前页,默认显示第一页
private int pageCount = 8; //每页显示的行数(查询返回的行数),默认每页显示3行
private int totalCount ; //数据库表中的总记录数
private int totalPage; //总页数 = 总记录数 / 每页显示的行数(+1)
private List
//返回总页数
public int getTotalPage() {
//若总页数是奇数是页数增加的逻辑判断
if(totalCount % pageCount == 0){
totalPage = totalCount / pageCount;
}else{
totalPage = totalCount / pageCount +1;
}
return totalPage;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List
return list;
}
public void setList(List
this.list = list;
}
}
package com.wxw.people.entity;
public class Students {
private int id;
private String name;
private int age;
private String gender;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Students [id=" + id + ", name=" + name + ", age=" + age
+ ", gender=" + gender + "]";
}
}
package com.wxw.people.dao;
import com.wxw.people.entity.Students;
import com.wxw.people.untils.PageBean;
/*
*
* 数据访问层,接口设计
* */
public interface IEmployeeDao {
/*
* 分页查询数据
* */
public void getAll(PageBean
/*
* 查询总记录数
* */
public int getTotalCount();
}
-----------------------------操作数据库 EmployeeDao 实现类代码如下--------------------------------
package com.wxw.people.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.wxw.people.dao.IEmployeeDao;
import com.wxw.people.entity.Students;
import com.wxw.people.untils.JdbcUntils;
import com.wxw.people.untils.PageBean;
public class EmployeeDao implements IEmployeeDao {
@Override
public void getAll(PageBean
//查询总记录数; 并设置保存到pageBean对象中
int totalCount = getTotalCount();
pageBean.setTotalCount(totalCount);
/*
* 问题: jsp页面,如果当前页为首页,再点击上一页报错!
* 如果当前页为末页,再点下一页显示有问题!
* 解决:
* 1. 如果当前页 <= 0; 当前页设置当前页为1;
* 2. 如果当前页 > 最大页数; 当前页设置为最大页数
*/
if(pageBean.getCurrentPage() <= 0){
pageBean.setCurrentPage(1);
}else if(pageBean.getCurrentPage() > pageBean.getTotalPage()){
pageBean.setCurrentPage(pageBean.getTotalPage());
}
//获取当前页:计算查询的起始行、返回行数
int currentPage = pageBean.getCurrentPage();
int index = (currentPage - 1)*pageBean.getPageCount();
int count = pageBean.getPageCount();
String sql = "select * from student limit ?,?";
QueryRunner queryRunner = JdbcUntils.getQueryRunner();
try {
List
pageBean.setList(list);
} catch (SQLException e) {
e.printStackTrace();
}
}
public int getTotalCount() {
String sql = "select count(*) from student";
try {
Long count = JdbcUntils.getQueryRunner().query(sql, new ScalarHandler
return count.intValue();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
-----------------------------操作业务逻辑 IEmployeeService 接口代码如下--------------------------------
package com.wxw.people.service;
import com.wxw.people.entity.Students;
import com.wxw.people.untils.PageBean;
public interface IEmployeeService {
public void getAll(PageBean
}
-----------------------------操作业务逻辑 EmployeeService 实现类代码如下--------------------------------
package cn.itcast.service.impl;
import cn.itcast.dao.IEmployeeDao;
import cn.itcast.dao.impl.EmployeeDao;
import cn.itcast.entity.Employee;
import cn.itcast.service.IEmployeeService;
import cn.itcast.utils.PageBean;
/**
* 3. 业务逻辑层,实现
* @author Jie.Yuan
*
*/
public class EmployeeService implements IEmployeeService {
// 创建Dao实例
private IEmployeeDao employeeDao = new EmployeeDao();
@Override
public void getAll(PageBean
try {
employeeDao.getAll(pb);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
-----------------------------接收请求 参数 IndexServlet 类代码如下--------------------------------
package com.wxw.people.servlet;-----------------------------JSP页面代码如下--------------------------------
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
编号 | 姓名 | 年龄 | 性别 | ||||
${stu.id } | ${stu.name } | ${stu.age } | ${stu.gender } | ||||
对不起,没有你要找的数据 | |||||||
首页 上一页 下一页 末页 |