jdbc增删改查加分页

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;
}
}
增删改查和分页,学习、练习,附件为源码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值