编辑: xiong447385 2019-07-03

if (ctx == null) throw new Exception( 没有匹配的环境 );

ds = (DataSource) ctx.lookup(jdbcStr.toString());

if (ds == null) throw new Exception( 没有匹配数据库 );

} catch (NamingException e) { e.printStackTrace();

} catch (Exception e) { e.printStackTrace();

} } } 至此,数据库连接池建立完毕.在开发中通过初始化DB,调用getConnection ()即可获得打开的数据库连接Connection.为了开发过程的快捷,建立了BaseOp.java,其中每个与数据库直接操作的bean都要继承BaseOp类. 数据库操作父类BaseOp.java package huc.blog.op;

import huc.blog.util.DB;

import huc.blog.util.PageObject;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

public class BaseOp { protected DB db = null;

protected Connection conn = null;

protected PreparedStatement pst = null;

protected ResultSet rs = null;

public BaseOp(){ this.db = new DB();

} /** * 关闭打开的ResultSet,PreparedStatement,Connection * */ public void close(){ try { if(rs != null) rs.close();

if(pst != null) pst.close();

if(conn != null) conn.close();

} catch (SQLException e) { e.printStackTrace();

} } /** * 得到分页的sql语句 * @param page 页面对象 * @param keySQL 查询的字段和数据库 like( * from table ) * @param otherSQL 查询条件 * @param orderBy 排序方式 like( order by time desc ) * @return pageCount 当前分页开始的记录号 */ public StringBuffer getSQL(PageObject page, String keySQL, String otherSQL, String orderBy){ StringBuffer sql = new StringBuffer();

//检查Page对象的页号是否合法,根据toatl和pageSize合理设置页号 if(page.getPageNo() <

1) page.setPageNo(1);

int count = (page.getPageNo() -

1 ) * page.getPageSize();

if(count >

= page.getTotal()){ if(page.getTotal() % page.getPageSize() == 0) page.setPageNo(page.getTotal()/page.getPageSize());

else page.setPageNo((int)page.getTotal()/page.getPageSize() + 1);

count = (page.getPageNo() -

1 ) * page.getPageSize();

} if(orderBy == null || orderBy.trim().equals( )){ orderBy = order by id asc ;

} StringBuffer tempOrderBy = new StringBuffer();

if(orderBy.indexOf( desc ) != -1){ tempOrderBy.append( );

tempOrderBy.append(orderBy.substring(0,orderBy.indexOf( desc )));

tempOrderBy.append( asc );

} else if(orderBy.indexOf( asc ) != -1){ tempOrderBy.append( );

tempOrderBy.append(orderBy.substring(0,orderBy.indexOf( asc )));

tempOrderBy.append( desc );

} else{ orderBy = (new StringBuffer(orderBy)).append( asc ).toString();

tempOrderBy.append( );

tempOrderBy.append(orderBy.substring(0,orderBy.indexOf( asc )));

tempOrderBy.append( desc );

} sql.append( select top );

sql.append(page.getPageSize());

sql.append( page.* from ( );

sql.append( select top );

sql.append(page.getTotal() - count);

sql.append(keySQL);

if(otherSQL != null &

&

!otherSQL.trim().equals( )){ sql.append( where

1 =

1 );

sql.append(otherSQL);

} sql.append(tempOrderBy);

sql.append( ) page );

sql.append(orderBy);

return sql;

} } 备注:为了开发过程的快捷和方便,对于分页建立了分页对象PageObject 分页对象类PageObject.java /** * 将分页设计的元素用此对象封装 */ package huc.blog.util;

import java.util.ArrayList;

import java.util.List;

public class PageObject { //当前页面的对象的集合 private List list = new ArrayList();

//符合分页的对象的总数 private int total = 0;

下载(注:源文件不在本站服务器,都将跳转到源网站下载)
备用下载
发帖评论
相关话题
发布一个新话题