public class UserDao implements DAO<User> {
private static final long serialVersionUID = 1L;
private Connection connection = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public boolean doAdd(User entity) {
// TODO Auto-generated catch block
return doInsertOrUpdate(entity, "add");
}
public boolean doDeleteById(Serializable id) {
boolean flag = false;
String sql = "delete from users where id =?";
try {
connection = DBHandle.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, Integer.parseInt(id.toString()));
int rows = preparedStatement.executeUpdate();
if (rows > 0)
flag = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
flag = false;
e.printStackTrace();
} finally {
DBHandle.close(preparedStatement, connection);
}
return flag;
}
public List<User> doSelect() {
List<User> list = new ArrayList<User>();
String sql = "select id,name,age,tel,address from users";
try {
connection = DBHandle.getConnection();
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setAge(resultSet.getInt("age"));
user.setTel(resultSet.getString("tel"));
user.setAddress(resultSet.getString("address"));
list.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
list = null;
e.printStackTrace();
} finally {
DBHandle.close(resultSet, preparedStatement, connection);
}
if (list.size() > 0)
return list;
return null;
}
public User doSelectById(Serializable id) {
User user = null;
String sql = "select * from users where id=?";
try {
connection = DBHandle.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, Integer.parseInt(id.toString()));
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setAge(resultSet.getInt("age"));
user.setTel(resultSet.getString("tel"));
user.setAddress(resultSet.getString("address"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
user = null;
e.printStackTrace();
} finally {
DBHandle.close(resultSet, preparedStatement, connection);
}
return user;
}
public boolean doUpdate(User entity) {
// TODO Auto-generated method stub
return doInsertOrUpdate(entity, "update");
}
private boolean doInsertOrUpdate(User entity, String identity) {
boolean flag = false;
StringBuilder sql = new StringBuilder("");
try {
connection = DBHandle.getConnection();
if ("add" == identity) {
sql
.append("insert into users (name, age, tel, address) values(?,?,?,?);");
} else if ("update" == identity) {
sql
.append("update users set name=?,age=?,tel=?,address=? where id=?;");
}
preparedStatement = connection.prepareStatement(sql.toString());
preparedStatement.setString(1, entity.getName());
preparedStatement.setInt(2, entity.getAge());
preparedStatement.setString(3, entity.getTel());
preparedStatement.setString(4, entity.getAddress());
if ("update" == identity) {
preparedStatement.setInt(5, entity.getId());
}
int rows = preparedStatement.executeUpdate();
if (rows > 0)
flag = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
flag = false;
e.printStackTrace();
} finally {
DBHandle.close(preparedStatement, connection);
}
return flag;
}
public List<User> doList(Page<User> page) {
List<User> list = new ArrayList<User>();
connection = DBHandle.getConnection();
StringBuilder sql = new StringBuilder("");
if (page.getNowPage() >= 1)
sql.append("select * from users order by id desc limit "
+ (page.getNowPage() - 1) * page.getPageSize() + ","
+ page.getPageSize() + ";");
else
sql
.append("select * from users order by id desc limit "
+ (0 * page.getPageSize()) + ","
+ page.getPageSize() + ";");
// String sql = "select * from users order by id desc limit "
// + (nowPage - 1) * pageRow + "," + pageRow;
try {
preparedStatement = connection.prepareStatement(sql.toString());
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setAge(resultSet.getInt("age"));
user.setTel(resultSet.getString("tel"));
user.setAddress(resultSet.getString("address"));
list.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBHandle.close(resultSet, preparedStatement, connection);
}
return list;
}
public Page<User> doTotalPage(Page<User> page) {
int totalPage = 0;
connection = DBHandle.getConnection();
try {
preparedStatement = connection
.prepareStatement("select count(id) from users");
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
// 获得总页数;总页数=上取整(总记录/每页大小)
// System.out.println("总记录:"+rs.getInt(1)+"每页大小:"+dopage.getPageSize()+"页数:"+rs.getInt(1)/Double.parseDouble(dopage.getPageSize()+""));
totalPage = (int) Math.ceil(resultSet.getInt(1)
/ Double.parseDouble(page.getPageSize() + ""));
page.setTotalPage(totalPage);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBHandle.close(resultSet, preparedStatement, connection);
}
return page;
}
}
增删改查和分页,学习、练习,附件为源码
private static final long serialVersionUID = 1L;
private Connection connection = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public boolean doAdd(User entity) {
// TODO Auto-generated catch block
return doInsertOrUpdate(entity, "add");
}
public boolean doDeleteById(Serializable id) {
boolean flag = false;
String sql = "delete from users where id =?";
try {
connection = DBHandle.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, Integer.parseInt(id.toString()));
int rows = preparedStatement.executeUpdate();
if (rows > 0)
flag = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
flag = false;
e.printStackTrace();
} finally {
DBHandle.close(preparedStatement, connection);
}
return flag;
}
public List<User> doSelect() {
List<User> list = new ArrayList<User>();
String sql = "select id,name,age,tel,address from users";
try {
connection = DBHandle.getConnection();
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setAge(resultSet.getInt("age"));
user.setTel(resultSet.getString("tel"));
user.setAddress(resultSet.getString("address"));
list.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
list = null;
e.printStackTrace();
} finally {
DBHandle.close(resultSet, preparedStatement, connection);
}
if (list.size() > 0)
return list;
return null;
}
public User doSelectById(Serializable id) {
User user = null;
String sql = "select * from users where id=?";
try {
connection = DBHandle.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, Integer.parseInt(id.toString()));
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setAge(resultSet.getInt("age"));
user.setTel(resultSet.getString("tel"));
user.setAddress(resultSet.getString("address"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
user = null;
e.printStackTrace();
} finally {
DBHandle.close(resultSet, preparedStatement, connection);
}
return user;
}
public boolean doUpdate(User entity) {
// TODO Auto-generated method stub
return doInsertOrUpdate(entity, "update");
}
private boolean doInsertOrUpdate(User entity, String identity) {
boolean flag = false;
StringBuilder sql = new StringBuilder("");
try {
connection = DBHandle.getConnection();
if ("add" == identity) {
sql
.append("insert into users (name, age, tel, address) values(?,?,?,?);");
} else if ("update" == identity) {
sql
.append("update users set name=?,age=?,tel=?,address=? where id=?;");
}
preparedStatement = connection.prepareStatement(sql.toString());
preparedStatement.setString(1, entity.getName());
preparedStatement.setInt(2, entity.getAge());
preparedStatement.setString(3, entity.getTel());
preparedStatement.setString(4, entity.getAddress());
if ("update" == identity) {
preparedStatement.setInt(5, entity.getId());
}
int rows = preparedStatement.executeUpdate();
if (rows > 0)
flag = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
flag = false;
e.printStackTrace();
} finally {
DBHandle.close(preparedStatement, connection);
}
return flag;
}
public List<User> doList(Page<User> page) {
List<User> list = new ArrayList<User>();
connection = DBHandle.getConnection();
StringBuilder sql = new StringBuilder("");
if (page.getNowPage() >= 1)
sql.append("select * from users order by id desc limit "
+ (page.getNowPage() - 1) * page.getPageSize() + ","
+ page.getPageSize() + ";");
else
sql
.append("select * from users order by id desc limit "
+ (0 * page.getPageSize()) + ","
+ page.getPageSize() + ";");
// String sql = "select * from users order by id desc limit "
// + (nowPage - 1) * pageRow + "," + pageRow;
try {
preparedStatement = connection.prepareStatement(sql.toString());
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setAge(resultSet.getInt("age"));
user.setTel(resultSet.getString("tel"));
user.setAddress(resultSet.getString("address"));
list.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBHandle.close(resultSet, preparedStatement, connection);
}
return list;
}
public Page<User> doTotalPage(Page<User> page) {
int totalPage = 0;
connection = DBHandle.getConnection();
try {
preparedStatement = connection
.prepareStatement("select count(id) from users");
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
// 获得总页数;总页数=上取整(总记录/每页大小)
// System.out.println("总记录:"+rs.getInt(1)+"每页大小:"+dopage.getPageSize()+"页数:"+rs.getInt(1)/Double.parseDouble(dopage.getPageSize()+""));
totalPage = (int) Math.ceil(resultSet.getInt(1)
/ Double.parseDouble(page.getPageSize() + ""));
page.setTotalPage(totalPage);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBHandle.close(resultSet, preparedStatement, connection);
}
return page;
}
}
增删改查和分页,学习、练习,附件为源码
- TestInterview.zip (1 MB)
- 下载次数: 2
发表评论
-
Spring aop
2018-09-10 18:23 499org.springframework.aop.aspectj ... -
Java并发编程-客户端加锁机制
2017-06-25 17:14 1219Java并发编程P61 public class List ... -
Java并发实战-私有构造函数捕获
2017-06-24 21:58 773java并发编程实战P58 私有构造函数捕获:私有构造 ... -
java.lang.Void
2017-06-20 23:39 550java.util.concurrent.ForkJoinP ... -
java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit
2014-06-11 13:11 1234严重: Exception sending context i ... -
jvm学习笔记
2014-03-25 13:47 773学习笔记 HotSpot不区分虚拟机栈和本地方法栈,所以 ... -
自定义标签与spring注入报空
2014-03-25 13:08 771在自定义标签中注入spring对象报空 参考链接: ... -
javascript学习笔记
2013-05-02 17:32 1136<!-- form表单提交时, ... -
java中基本数据类型和引用类型的==和equals判断体会
2013-04-19 13:23 1207今天下午遇到一个问题 :两个相等的整数相比结果返回false, ... -
出现Exception之后会影响程序的执行流程吗
2013-04-19 13:02 903程序出现异常后catch块以后的代码是否执行?以下是一个小示例 ... -
eclipse时间
2013-03-14 21:50 743eclipse时间@date @time 时间格式转换成,例如 ... -
eclipse集成tomcat
2013-03-14 21:46 732在eclipse中找到${path}\eclipse\plug ... -
eclipse实现openexplorer功能
2013-03-14 21:42 724在eclipse中找到${path}\eclipse\plug ... -
连接工厂
2012-12-05 22:58 891//JDBC编程练习 public class DBHandl ... -
java有关英文缩写
2012-06-06 21:36 1055JDK(Java Development Kit,java ... -
解决Myeclipse导入包出现不了referenced libraries的问题
2011-09-02 09:29 2548我想往Web工程里面导入些包,复制到WebRoot ---&g ... -
Java多态性详解——父类引用子类对象
2011-08-17 12:41 1304面向对象编程有三个特征,即封装、继承和多态。 封装隐藏 ...
相关推荐
jdbc封装(实现对实体的增删改查[分页]),辅助学习Hibernate 包含三个文件夹,分别是: code-access实现 是用access实现的,本意是access方便,就一个文件,方便部署。但access有好多不支持,就写成这样.主要是可参考Dao...
在本项目中,DWR被用来实现在不刷新整个页面的情况下进行数据的增删改查操作。用户可以通过前端界面发送请求,DWR将这些请求转换为Java方法调用,执行后返回结果,前端再根据结果更新显示,极大地提升了用户体验。 ...
**JFinal 增删改查与分页查询详解** JFinal 是一款基于 Java 的轻量级 Web 开发框架,以其高效、简洁的特性深受开发者喜爱。本篇将深入讲解如何利用 JFinal 实现数据库的增删改查(CRUD)操作,并结合分页查询,...
在这个项目中,我们将会深入探讨如何利用这些技术实现数据库的增删改查(CRUD)操作以及分页功能,并将其部署到Oracle数据库。 首先,Struts2作为MVC(模型-视图-控制器)框架,负责处理HTTP请求,协调应用的业务...
在本项目中,"springboot+vue+elementui实现增删改查和分页查询"是一个基于现代Web技术栈的简单但实用的应用示例。它整合了Spring Boot、Vue.js和Element UI,这三个组件分别在后端、前端和UI设计方面发挥关键作用,...
在本项目中,我们将深入探讨如何利用这三大框架来实现数据的增删改查(CRUD)以及分页功能。 首先,Spring框架作为基础,它提供了依赖注入(DI)和面向切面编程(AOP)的核心特性,使得各个组件之间可以松耦合,...
这个"基于SSH2的增删改查及分页完整项目"提供了一个全面的示例,帮助开发者理解如何在实际应用中实现数据的增、删、改、查操作以及分页功能。 **Struts2** 是一个MVC(Model-View-Controller)框架,负责处理HTTP...
在探讨Struts2框架下的增删改查以及分页查询功能时,我们首先需要理解Struts2框架本身。Struts2是Apache软件基金会的一个开源Web应用框架,它继承了Struts1的一些特性,并在此基础上进行了大量的改进和扩展,提供了...
【Java EasyUi增删改查加分页案例】是一个适合初学者的实践项目,它涵盖了Web开发中的基础功能,包括添加、删除、修改和查询数据,并且整合了分页功能。EasyUI是一个基于jQuery的UI框架,它提供了丰富的组件,简化了...
在这个"jsp+jdbc+servlet分页带增删查改"项目中,开发者使用了MVC(Model-View-Controller)架构模式,实现了对数据的CRUD(Create, Read, Update, Delete)操作,并提供了分页功能。** 1. **JSP(JavaServer Pages...
* 分页查询数据 4.准备环境 5.Servlet + JSP +JavaBean + JDBC(DBUtils)+ mysql 6.数据库 create database day19; use day19; create table t_customer( id varchar(40) primary key, username varchar(20)...
在IT领域,尤其是在Web开发中,"jsp+servlet+mysql实现简单的增删改查+分页"是一个常见的学习主题,适合初学者理解基础的后端开发流程。这个组合提供了从客户端到服务器端再到数据库的数据交互能力,同时也包含了...
在本项目"Struts1 增删改查+分页"中,我们将深入探讨如何利用Struts1实现对数据的基本操作以及分页显示。 1. **Struts1基础**: - **Action类**:是Struts1的核心,用于接收用户请求并执行业务逻辑。每个Action类...
一个servlet + JDBC 写的一个新闻发布系统,附带Oracle 数据库,前台用户对新闻的点击查看,后台管理新闻,对新闻的增删改查,附带最基本的数据库分页,Myeclipse9.0 + Tomcat6.0 直接可以用
在IT行业中,数据库操作是开发应用的基础,"增删改查"(CRUD:Create, Read, Update, Delete)和分页功能是数据库管理的核心部分。本教程将深入讲解如何在MySQL数据库环境下,利用Eclipse集成开发环境实现这些功能。...
本教程"jsp+jdbc_oracle增删改查过滤分页排序"旨在帮助初学者掌握如何利用JSP与Oracle数据库进行数据操作,包括增、删、改、查以及查询结果的过滤、分页和排序。 首先,我们需要理解JDBC(Java Database ...
本项目“jdbc实现增删改查.rar”就是通过jQuery EasyUI和原生JDBC来实现数据库的CRUD(Create、Read、Update、Delete)操作,并且包含了分页显示的功能。 首先,让我们深入了解JDBC。JDBC提供了一组接口和类,允许...
本项目主要涉及了"java报表,增删改查分页等功能",涵盖了后端数据处理、前端展示以及用户交互等多个关键环节。以下是这些功能的详细解释: 1. **增删改查(CRUD)**:这是任何数据库管理系统的基础操作,包括创建...
这个项目包含了完整的登录功能、数据的增删改查以及分页显示,同时还提供了数据库的SQL脚本,使得用户可以直接运行项目而无需从零开始搭建环境。 1. **SpringMVC**:SpringMVC是Spring框架的一部分,它是一个轻量级...