//分页主体函数:
package com.zz.util;
import java.util.*;
public class Page{
private String tableName;
private int pageNow;
private int pageSize;
private int pageAll;
private int count;
private List<Info> allInfo;
private List<Info> pageInfo;
public Page(String tableName,int pageNow,int pageSize) throws Exception {
this.tableName=tableName;
this.pageNow=pageNow;
this.pageSize=pageSize;
Factory fac=new Factory();
try{
this.count=fac.getInstance().getCount(this.tableName);
this.allInfo=fac.getInstance().findAll("",this.tableName);
}catch(Exception e){
throw e;
}
if(this.count%this.pageSize==0){
this.pageAll=this.count/this.pageSize;
}else{
this.pageAll=this.count/this.pageSize+1;
}
int start=(this.pageNow-1)*this.pageSize;
int end=start+pageSize;
if(end>=count){
end=count;
}
this.pageInfo=this.allInfo.subList(start, end);
}
public List<Info> getPageInfo(){
return this.pageInfo;
}
public static void main(String [] args) {
Page p=null;
try{
p=new Page("lzkx",4,3);
}catch(Exception e){
e.printStackTrace();
}
List<Info> nowInfo=p.getPageInfo();
Iterator it=nowInfo.iterator();
while(it.hasNext()){
Info info=(Info)it.next();
System.out.println(info.getId());
}
}
}
工厂工具类函数编写如下:
package com.zz.util;
import java.util.*;
import java.sql.*;
public class Factory{
private Connection conn=null;
public Factory(){
DbConn dbc=new DbConn();
conn=dbc.getConnection();
}
public InfoImpl getInstance(){
return new InfoImpl(conn);
}
}
package com.zz.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbConn {
private static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
private static final String DBURL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK" ;
private static final String DBUSER = "root" ;
private static final String DBPASS = "admin" ;
/*
private static final String DBDRIVER ="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String DBURL = "jdbc:sqlserver://localhost:1433;databaseName=test";
private static final String DBUSER = "sa" ;
private static final String DBPASS = "123456" ;
*/
private Connection conn = null ;
public DbConn(){
try {
Class.forName(DBDRIVER) ;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(DBURL, DBUSER,DBPASS) ;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getConnection(){
return this.conn ;
}
public void close(){
if(this.conn!=null){
try {
this.conn.close() ;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.zz.util;
import java.util.List;
public interface IinfoDao {
/**
* *
* @param Info
* @return
* @throws Exception
*/
public boolean doCreate(Info info) throws Exception;
public boolean doUpdate(Info Info) throws Exception;
/**
* *
* @param id
* @return
* @throws Exception
*/
public boolean doDelete(int id,String talbeName) throws Exception;
/**
* *
* @param id
* @return
* @throws Exception
*/
public Info findById(int id,String talbeName) throws Exception;
/**
* *
* @param keyWord
* @return
* @throws Exception
*/
public List<Info> findAll(String keyWord,String talbeName) throws Exception;
}
package com.zz.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class InfoImpl implements IinfoDao {
private Connection conn = null;
public InfoImpl(Connection conn) {
this.conn = conn;
}
public int getCount(String tableName)throws Exception {
int Count=-1;
boolean flag = false;
PreparedStatement pstmt = null;
String sql = "select count(*) from "+tableName+"";
try {
pstmt = this.conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
Count=rs.getInt(1);
}
rs.close();
} catch (Exception e) {
throw e;
} finally { //
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e1) {
}
}
}
return Count;
}
@Override
public boolean doCreate(Info info) throws Exception {
boolean flag = false;
PreparedStatement pstmt = null;
String tableName=info.getTableName();
int Count=this.getCount(info.getTableName())+1;
String sql = "INSERT INTO "+tableName+" (newId,title,content,newsTime,comeFrom) VALUES (?,?,?,?,?) ";
try {
pstmt = this.conn.prepareStatement(sql);
pstmt.setInt(1,Count);
pstmt.setString(2, info.getTitle()); //
pstmt.setString(3, info.getContent()); //
pstmt.setDate(4, new java.sql.Date(info.getDate().getTime()));
pstmt.setString(5, info.getComeFrom());
if (pstmt.executeUpdate() > 0) {//
flag = true;
}
} catch (Exception e) {
throw e;
} finally { //
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e1) {
}
}
}
return flag;
}
@Override
public boolean doDelete(int id,String tableName) throws Exception {
boolean flag = false;
PreparedStatement pstmt = null;
String sql = "DELETE FROM "+tableName+" WHERE newId=? ";
String sql2 = "update "+tableName+" set newId=newId-1 WHERE newId>? ";
try {
pstmt = this.conn.prepareStatement(sql);
pstmt.setInt(1, id); //
if (pstmt.executeUpdate() > 0) {//
flag = true;
}
pstmt=this.conn.prepareStatement(sql2);
pstmt.setInt(1,id);
if(pstmt.executeUpdate()<0){
flag=false;
}
} catch (Exception e) {
throw e;
} finally { //
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e1) {
}
}
}
return flag;
}
@Override
public boolean doUpdate(Info info) throws Exception {
boolean flag = false;
PreparedStatement pstmt = null;
String tableName=info.getTableName();
String sql = "UPDATE "+tableName+" SET title=?,content=?,comeFrom=? WHERE newId=?";
try {
pstmt = this.conn.prepareStatement(sql);
pstmt.setString(1,info.getTitle()); //
pstmt.setString(2, info.getContent()); //
pstmt.setString(3, info.getComeFrom());
pstmt.setInt(4, info.getId());
if (pstmt.executeUpdate() > 0) {//
flag = true;
}
} catch (Exception e) {
throw e;
} finally { //
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e1) {
}
}
}
return flag;
}
@Override
public List<Info> findAll(String keyWord,String tableName) throws Exception {
List<Info> all = new ArrayList<Info>();
PreparedStatement pstmt = null;
String sql = "SELECT newId,title,content,newsTime,comeFrom FROM "+tableName+" WHERE title LIKE ? OR content LIKE ?" ;
try {
pstmt = this.conn.prepareStatement(sql);
pstmt.setString(1, "%" + keyWord + "%");
pstmt.setString(2, "%" + keyWord + "%");
ResultSet rs = pstmt.executeQuery(); //
while (rs.next()) {
Info info = new Info();
info.setId(rs.getInt(1));
info.setTitle(rs.getString(2));
info.setContent(rs.getString(3));
info.setDate(rs.getDate(4));
info.setComeFrom(rs.getString(5));
all.add(info); // }
rs.close();
} catch (Exception e) {
throw e;
} finally { //
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e1) {
}
}
}
return all;
}
@Override
public Info findById(int id,String tableName) throws Exception {
Info info = new Info();
PreparedStatement pstmt = null;
String sql = "SELECT newId,title,content,newsTime,comeFrom FROM "+tableName+" WHERE newId=?";
try {
pstmt = this.conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery(); //
if (rs.next()) {
info.setId(rs.getInt(1));
info.setTitle(rs.getString(2));
info.setContent(rs.getString(3));
info.setDate(rs.getDate(4));
info.setComeFrom(rs.getString(5));
}
rs.close();
} catch (Exception e) {
throw e;
} finally { //
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e1) {
}
}
}
return info;
}
}
package com.zz.util;
import java.util.Date;
public class Info {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getComeFrom(){
return comeFrom;
}
public void setComeFrom(String comeFrom){
this.comeFrom=comeFrom;
}
public String getTableName(){
return this.talbeName;
}
public void setTableName(String tableName){
this.talbeName=tableName;
}
private String talbeName;
private int id;
private String title;
private String content;
private Date date;
private String comeFrom;
public String toString(){
String str=this.id+" "+this.title+" "+this.content+" "+this.date.toString()+this.comeFrom;
return str;
}
}