📄 datautil.java~2~
字号:
boolean fixPageNo)
throws Exception {
int i;
String strQuery;
Statement stmt;
ResultSet rs;
DataObjectList dol = new DataObjectList();
if (sqlFrom() == null)
throw new Exception("试图在不支持从数据库读取的数据对象上执行读取操作。");
dol.m_iItemsPerPage = itemsPerPage;
//构造Where子句
String strFullWhere = sqlWhere();
if (strWhere != null)
if (!strWhere.equals(""))
if (!strFullWhere.equals(""))
strFullWhere = strFullWhere + " and " + strWhere;
else
strFullWhere = strWhere;
//{符合条件的记录数
stmt = conn.createStatement();
strQuery =
"select count(*) from "
+ sqlFrom()
+ (strFullWhere.equals("") ? "" : " where " + strFullWhere);
rs = stmt.executeQuery(strQuery);
rs.next();
dol.m_iTotalCount = rs.getInt(1);
rs.close();
stmt.close();
//}
//{总页数与当前显示的页数
dol.m_iPageCount =
(int) Math.ceil((double) dol.m_iTotalCount / (double) dol.m_iItemsPerPage);
dol.m_iPageNo = pageNo;
if (dol.m_iPageNo < 1) {
if (fixPageNo)
dol.m_iPageNo = 1;
else
return dol;
}
if (dol.m_iPageNo > dol.m_iPageCount) {
if (fixPageNo)
dol.m_iPageNo = dol.m_iPageCount;
else
return dol;
}
//}
if (dol.m_iPageNo != 0) {
//{构造列表
stmt = conn.createStatement();
strQuery =
"select "
+ sqlSelect()
+ " from "
+ sqlFrom()
+ (strFullWhere.equals("") ? "" : " where " + strFullWhere)
+ (sqlOrder().equals("") ? "" : " order by " + sqlOrder());
rs = stmt.executeQuery(strQuery);
//跳到显示页
for (i = (dol.m_iPageNo - 1) * dol.m_iItemsPerPage; i > 0; i--) {
rs.next();
}
//取出对象
DataUtil dataObject;
for (i = 0; rs.next() && i < dol.m_iItemsPerPage; i++) {
dataObject = (DataUtil) getClass().newInstance();
dataObject.objectCreated(this);
dataObject.getFromResultSet(conn, rs);
dataObject.m_bIsNewDataObject = false;
dol.m_vecObjects.add(dataObject);
}
rs.close();
stmt.close();
//}
for (i = 0; i < dol.m_vecObjects.size(); i++)
((DataUtil) dol.m_vecObjects.elementAt(i)).objectLoaded(conn);
}
return dol;
}
protected void objectCreated(DataUtil byObject) {
m_objCreator = byObject;
}
protected void objectLoaded(Connection conn) throws Exception {
}
/**
* 数据对象成功更新到数据库中后,DataObject会调用afterUpdate方法,
* @param conn Connection 到数据库的连接
* @param isInsert boolean 是否是新插入
*/
protected void objectUpdated(Connection conn, boolean isInsert) throws Exception {
}
/**
* 用于将数据对象更新到数据库中。
* 创建插入语句。
* @param conn Connection 到数据库的连接
* @return PreparedStatement 插入语句。若为null则表示该数据对象不支持向数据库插入新对象。
*/
protected PreparedStatement prepareInsertStatement(Connection conn)
throws Exception {
return null;
}
/**
* 用于将数据对象更新到数据库中。
* 创建插入语句。
* @param conn Connection 到数据库的连接
* @param vecStmts Vector 装载插入语句的向量数组。初始为空。如果返回后仍然为空,
* 则表示该数据对象不支持向数据库插入新对象。
*/
protected void prepareInsertStatements(Connection conn, Vector vecStmts)
throws Exception {
PreparedStatement stmt = prepareInsertStatement(conn);
if (stmt != null)
vecStmts.add(stmt);
}
/**
* 用于将数据对象更新到数据库中。
* 创建更新语句。
* @param conn Connection 到数据库的连接
* @return PreparedStatement 更新语句。若为null则表示该数据对象不支持向数据库中更新对象。
*/
protected PreparedStatement prepareUpdateStatement(Connection conn)
throws Exception {
return null;
}
/**
* 用于将数据对象更新到数据库中。
* 创建更新语句。
* @param conn Connection 到数据库的连接
* @param vecStmts Vector 装载插入语句的向量数组。初始为空。如果返回后仍然为空,
* 则表示该数据对象不支持向数据库中更新对象。
*/
protected void prepareUpdateStatements(Connection conn, Vector vecStmts)
throws Exception {
PreparedStatement stmt = prepareUpdateStatement(conn);
if (stmt != null)
vecStmts.add(stmt);
}
/**
* 设置对象状态标记。
* @param isNew boolean true表示将数据对象的状态强制设置为不是从数据库中取出的。
*/
public void setDataObjectStatus(boolean isNew) {
m_bIsNewDataObject = isNew;
}
/**
* 用于将数据对象更新到数据库中。
* 将数据对象的数据设置给prepareInsertStatement()或
* prepareUpdateStatement()准备好的语句中。
* @param stmt PreparedStatedStatement 准备好的语句
*/
protected void setToPreparedStatement(PreparedStatement stmt)
throws Exception {
}
/**
* 用于将数据对象更新到数据库中。
* 将数据对象的数据设置给prepareInsertStatements()或
* prepareUpdateStatements()准备好的语句中。
* @param vecStmts Vector 准备好的语句
*/
protected void setToPreparedStatements(Vector vecStmts) throws Exception {
if (vecStmts.size() > 0)
setToPreparedStatement((PreparedStatement) vecStmts.elementAt(0));
}
/**
* 用于实现从数据库中删除数据对象。
* 返回从数据库中删除数据对象的SQL语句。
* @return String delete语句
*/
protected String sqlDelete() {
return null;
}
/**
* 用于实现从数据库中读出数据对象。
* 返回从数据库中选择数据对象的SQL语句的from子句,不包括from本身。
* @return String from子句。若为null则表示该数据对象不支持从数据库中读取对象。
*/
protected String sqlFrom() {
return null;
}
/**
* 用于实现从数据库中读出数据对象。
* 返回从数据库中选择数据对象的SQL语句的order by子句,不包括order by本身。
* @return String order by子句
*/
protected String sqlOrder() {
return "";
}
/**
* 用于实现从数据库中读出数据对象。
* 返回从数据库中选择数据对象的SQL语句的select子句,不包括select本身。
* @return String select子句
*/
protected String sqlSelect() {
return "*";
}
/**
* 用于实现从数据库中读出数据对象。
* 返回从数据库中选择数据对象的SQL语句的where子句,不包括where本身。
* @return String where子句
*/
protected String sqlWhere() {
return "";
}
/**
* 将数据对象更新到数据库中。如果数据对象不存在与数据库,则插入,
* 否则更新。
* @param conn Connection 到数据库的连接
*/
public void update(Connection conn) throws SQLException, Exception {
boolean bAutoCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
if (m_bDebug) {
System.out.println("Update:");
dump(System.out);
}
try {
Vector vecStmts = new Vector();
PreparedStatement stmt;
if (m_bIsNewDataObject) { //插入
prepareInsertStatements(conn, vecStmts);
if (vecStmts.size() == 0)
throw new Exception("试图在不支持插入的数据对象上执行插入操作。");
} else { //更新
prepareUpdateStatements(conn, vecStmts);
if (vecStmts.size() == 0)
throw new Exception("试图在不支持更新的数据对象上执行更新操作。");
}
setToPreparedStatements(vecStmts);
for (int i = 0; i < vecStmts.size(); i++) {
stmt = (PreparedStatement) vecStmts.elementAt(i);
stmt.executeUpdate();
stmt.close();
}
boolean bIsInsert = m_bIsNewDataObject;
m_bIsNewDataObject = false;
objectUpdated(conn, bIsInsert);
conn.commit();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
conn.rollback();
conn.setAutoCommit(bAutoCommit);
}
}
/**
* 将一组数据对象更新到数据库中。公用准备好的SQL语句,提高运行效率。
* @param conn Connection 到数据库的连接
* @param vecDataObjects Vector 要更新的对象数组
*/
public void update(Connection conn, Vector vecDataObjects)
throws SQLException, Exception {
boolean bAutoCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try {
Vector vecInsertStmts = new Vector();
Vector vecUpdateStmts = new Vector();
PreparedStatement stmt;
DataUtil dataObject;
prepareInsertStatements(conn, vecInsertStmts);
prepareUpdateStatements(conn, vecUpdateStmts);
int i, j;
int iInsertLen = vecInsertStmts.size();
int iUpdateLen = vecUpdateStmts.size();
String strThisClassName = getClass().getName();
int len = vecDataObjects.size();
for (i = 0; i < len; i++) {
dataObject = (DataUtil) vecDataObjects.elementAt(i);
if (m_bDebug) {
System.out.println("Update:");
dataObject.dump(System.out);
}
//检查数据对象类型是否正确
if (!strThisClassName.equals(dataObject.getClass().getName()))
continue;
if (dataObject.m_bIsNewDataObject) { //插入
if (iInsertLen != 0) {
dataObject.setToPreparedStatements(vecInsertStmts);
for (j = 0; j < iInsertLen; j++)
((PreparedStatement) vecInsertStmts.elementAt(j)).executeUpdate();
} else
throw new Exception("试图在不支持插入的数据对象上执行插入操作。");
} else { //更新
if (iUpdateLen != 0) {
dataObject.setToPreparedStatements(vecUpdateStmts);
for (j = 0; j < iUpdateLen; j++)
((PreparedStatement) vecUpdateStmts.elementAt(j)).executeUpdate();
} else
throw new Exception("试图在不支持更新的数据对象上执行更新操作。");
}
boolean bIsInsert = dataObject.m_bIsNewDataObject;
dataObject.m_bIsNewDataObject = false;
dataObject.objectUpdated(conn, bIsInsert);
}
for (i = 0; i < iInsertLen; i++)
((PreparedStatement) vecInsertStmts.elementAt(i)).close();
for (i = 0; i < iUpdateLen; i++)
((PreparedStatement) vecUpdateStmts.elementAt(i)).close();
conn.commit();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
conn.rollback();
conn.setAutoCommit(bAutoCommit);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -