* 分页
* @author yun
*
*/
public class Pagination {
private int pageSize = 3;//每页显示几条数据
private int totalPage = Integer.MAX_VALUE;//共多少页
private int page = 1;//第几页
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
if(pageSize lt;= 0){
pageSize = 3;
}
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
if(totalPage lt;=0){
totalPage = 1;
}
this.totalPage = totalPage;
//下面的setPage(page)一定要有,因为totalPage是查询出来的,这会影响page的值 。
//如:原来有12页数据,现在查询出来的只有2页,那么page大于2的页应该就不存在java上一条代码了
setPage(page);
}
public int getPage() {
return page;
}
public void setPage(int page) {
System.out.println("=========totalPage:"+totalPage);
if(page lt;= 0){
page = 1;
}
if(page gt; totalPage){
page = totalPage;
}
this.page = page;
}
}
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.pojo.User;
import com.util.DBUtil;
import com.util.Pagination;
public class UserDaoImpl implements UserDao {
public void add(User u) throws Exception {
int id = searchMaxId();//获得id值 , 相当于id = seq.nextval()
System.out.println("获得id值=============="+id);
u.setId(id);
String sql = "insert into s_user(id,username,password,valid) " +
" values(?,?,?,?)";
Connection conn = DBUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, u.getId());
pstmt.setString(2, u.getUsername());
pstmt.setString(3, u.getPassword());
pstmt.setString(4, u.getValid());
System.out.println("打印sql:"+sql+"\t参数:["+u.getId()+","+u.getUsername()+","+u.getPassword()+","+u.getValid()+"]");
pstmt.executeUpdate();
DBUtil.close(pstmt);
DBUtil.close(conn);
}
/**
* 查询记录中最大的id,如果没有就默认为1,有就+1返回
* @return
*/
private int searchMaxId() {
int id = 1;
String sql = "select max(id) mid from s_user";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
【java上一条代码 java第一条代码】stmt = conn.createStatement();
System.out.println("打印sql:"+sql);
rs = stmt.executeQuery(sql);
if(rs.next()){
id = rs.getInt("mid")+1;//有结果,就+1
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return id;
}
public void delete(int id) throws Exception {
String sql = "delete from s_user where id = ?";
Connection conn = DBUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
System.out.println("打印sql:"+sql+"\t参数:["+id+"]");
pstmt.executeUpdate();
DBUtil.close(pstmt);
DBUtil.close(conn);
}
public User findUserById(int id) throws Exception {
User u = null;
String sql = "select * from s_user where id = ?";
Connection conn = DBUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
System.out.println("打印sql:"+sql+"\t参数:["+id+"]");
推荐阅读
- css简单样式代码,css样式代码写在什么位置
- scratch中文版下载,scratch中文版下载30
- 手机app毕业设计用什么软件做,适合毕业设计的手机app
- pdf如何切换成PPT,pdf怎么变为ppt
- java代码居中 java代码怎么对齐
- 玩手游平板安卓推荐,玩手游平板电脑推荐
- 气压毕业设计的计算,气压毕业设计可以设计写什么
- 夏季工厂清仓直播文案,直播间清仓玩法
- 前端代码多还是java 前端比java简单吗