java通用分页条件查询_通用分页查询
packagecom.dao;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.ArrayList;importjava.util.List;importcom.bean.Author;importcom.db.DB;importcom.mysql.jdbc.Connection;importcom.mysql.jdbc.PreparedStatement;public classAuthorDao {//檢驗用戶名或密碼是否正確
publicAuthor CheckLogin(String username,String password) {
Author author=null;
DB db=newDB();//獲取數(shù)據(jù)庫連接對象
Connection con=(Connection) db.getCon();//定義sql語句
String sql="select * from author where username= ? and password= ?";try{
PreparedStatement ps=(PreparedStatement) con.prepareStatement(sql);//設(shè)置用戶名和密碼和密碼作為參數(shù)放到sql
ps.setString(1,username);
ps.setString(2, password);
ResultSet rs=ps.executeQuery();//用戶名和密碼正確,得到用戶數(shù)據(jù),進(jìn)行打包
if(rs.next()) {
author=newAuthor();
author.setId(rs.getInt(1));
author.setUsername(rs.getString(2));
author.setPassword(rs.getString(3));
author.setEmail(rs.getString(4));
author.setAddress(rs.getString(5));
author.setPhone(rs.getString(6));
}
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnauthor;
}//查詢所有用戶信息
public Listselectallauthor(){
List authorlist=new ArrayList();
DB db=newDB();//獲取數(shù)據(jù)庫連接對象
Connection con=(Connection) db.getCon();
String sql="select * from author";try{
PreparedStatement ps=(PreparedStatement) con.prepareStatement(sql);
ResultSet rs=ps.executeQuery();//循環(huán)遍歷獲取用戶信息
while(rs.next()) {
Author author=newAuthor();
author.setId(rs.getInt(1));
author.setUsername(rs.getString(2));
author.setPassword(rs.getString(3));
author.setEmail(rs.getString(4));
author.setAddress(rs.getString(5));
author.setPhone(rs.getString(6));
authorlist.add(author);//System.out.println("存放成功");
}
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnauthorlist;
}//查詢用戶表總記錄數(shù)(分頁查詢)
public intselectallauctorcount() {
DB db=newDB();
Connection con=(Connection) db.getCon();
String sql="select count(*) from author";try{
PreparedStatement ps=(PreparedStatement) con.prepareStatement(sql);
ResultSet rs=ps.executeQuery();if(rs.next()) {return rs.getInt(1);
}
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}return 0;
}/** 分頁查詢用戶數(shù)據(jù)類型
* padeIndex數(shù)據(jù)起始索引
* padeSize每頁顯示的數(shù)據(jù)條數(shù)*/
public List sellectallauthorPage(int pageIndex,intpageSize){
List list=new ArrayList();
Author author=newAuthor();
DB db=newDB();
Connection con=(Connection) db.getCon();
String sql="select * from author limit ?,?";try{
PreparedStatement ps=(PreparedStatement) con.prepareStatement(sql);
ps.setObject(1,pageIndex);
ps.setObject(2, pageSize);
ResultSet rs=ps.executeQuery();//獲取結(jié)果集//遍歷結(jié)果集獲取用戶的列表信息
while(rs.next()) {
author=newAuthor();
author.setId(rs.getInt(1));
author.setUsername(rs.getString(2));
author.setPassword(rs.getString(3));
author.setEmail(rs.getString(4));
author.setAddress(rs.getString(5));
author.setPhone(rs.getString(6));
list.add(author);
}
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnlist;
}//新增用戶信息
public voidadd(Author author) {
DB db=newDB();
Connection con=(Connection) db.getCon();
String sql="insert into author values(id,?,?,?,?,?)";try{
PreparedStatement ps=(PreparedStatement) con.prepareStatement(sql);
ps.setObject(1,author.getUsername());
ps.setObject(2,author.getPassword());
ps.setObject(3,author.getEmail());
ps.setObject(4,author.getAddress());
ps.setObject(5, author.getPhone());
ps.execute();
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}//刪除用戶消息
public void delete(intid) {
DB db=newDB();
Connection con=(Connection) db.getCon();
String sql="delete from author where id=?";try{
PreparedStatement ps=(PreparedStatement)con.prepareStatement(sql);
ps.setObject(1,id);
ps.execute();
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
總結(jié)
以上是生活随笔為你收集整理的java通用分页条件查询_通用分页查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: K-means算法和矢量量化
- 下一篇: NEC学习 ---- 模块 - 带点文字