📄 easyjdbengine.java
字号:
boolean ret = false;
try {
dba = getDatabaseDAO();
dba.prepare(sql);
if (this.showSql) {
System.out.println("EasyDBO更新SQL :" + sql);
}
int lastPara = 0;
if (obj.getValue() != null) {
obj.getValue().remove(obj.getTable().getId());
lastPara = obj.getValue().values().size();
dba.setQueryParams(obj.getValue().values());
}
dba.setParameter(lastPara + 1, obj.getIdValue());
// todo : add piginzoo 加入事务支持
if (autoCommit == false) {
logger.debug("进行事务操作!");
vec.add(dba.getPreparedStatement());
} else {
// rs = dba.preparedQuery();
logger.debug("直接更新!");
ret = dba.isPreparedUpdate();
// dba.close();
}
} catch (Exception e) {
logger.error("修改数据错误:" + e + ":sql=" + sql);
e.printStackTrace();
if (dba != null) {
try {
dba.rollback();
// dba.close();
} catch (SQLException sqle) {
}
}
} finally {
try {
dba.close();
if (autoCommit)
releaseConnection();
} catch (Exception e) {
logger.error("释放数据库资源错误:" + e);
}
}
return ret;
}
/**
*
* @param obj
* DBObject
* @return boolean
*/
public boolean del(DBObject obj) {
DatabaseDAO dba = null; // 引入数据库操作工具类
String sql = sqlQuery.getDelSql(obj);
if (sql.equals("")) {
logger.error("空的sql语句无法执行!");
return false;
}
boolean ret = false;
try {
dba = getDatabaseDAO();
if (this.showSql) {
System.out.println("EasyDBO:" + sql);
}
dba.prepare(sql);
dba.setParameter(1, obj.getIdValue());
// todo : add piginzoo 加入事务支持
if (autoCommit == false) {
vec.add(dba.getPreparedStatement());
} else {
// rs = dba.preparedQuery();
ret = dba.isPreparedUpdate();
// dba.close();//既然有了finally,就不要在这里close
}
} catch (Exception e) {
logger.error("删除数据错误:" + e + ":sql=" + sql);
if (dba != null) {
try {
dba.rollback();
// dba.close();
} catch (SQLException sqle) {
}
}
} finally {
try {
dba.close();
if (autoCommit)
releaseConnection();
} catch (Exception e) {
logger.error("释放数据库资源错误:" + e);
}
}
return ret;
}
/**
*
* @param sql
* String
* @return List
*/
public List query(String sql) { // 查询一组sql
return query(sql, null);
}
/**
*
* @param sql
* String
* @param params
* Collection
* @return List
*/
public List query(String sql, Collection params) {
return query(sql, params, 0, -1);
}
/**
* 分页查询
*
* @param sql
* String SQL语句完成数据库查询操作。
* @param params
* Collection 参数
* @param first
* int 第一条记录
* @param max
* int 页面记录
* @return List
*/
public List query(String sql, Collection params, int first, int max) {
DatabaseDAO dba = null; // 引入数据库操作工具类
ResultSet rs = null;
String s = sql;
boolean quickQuery = true;
if (max > 0) {
s = sqlQuery.getTopSql(sql, first, max);
// 不能实现getTopSql(sql,first,max),则使用普通的查询算法
if (s == null || "".equals(s)) {
s = sqlQuery.getTopSql(sql, first + max);
quickQuery = false;
}
}
List list = new ArrayList();
try {
dba = getDatabaseDAO();
if (this.showSql) {
System.out.println("EasyDBO:" + s);
}
dba.readOnlyPrepare(s);// 此处把参数sql换成转换后的s,提高分页查询效率
dba.setQueryParams(params);
rs = dba.preparedQuery();
// 这里有严重的逻辑混乱及错误,需要调整,为了适合更好的分页查询算法。
// 若是使用了真正的getTolSql(sql,first,max)的话,需要调整
if (quickQuery)// 通过快速查询算法读取的记录
{
while (rs.next()) {
DBObject obj = new DBObject(new DBTable());
obj.setValue(dba.resultSet2Map(rs));
list.add(obj);
}
} else {
int fecth = 0;// 记录已经取得的条数
if (first > 0) {
int begin = 0;
while ((begin < first) && rs.next()) {
begin++;
}
//System.out.println("begin:"+begin+";max:"+max);
}
while (rs.next() && fecth < max&&fecth<maxSize) {
DBObject obj = new DBObject(new DBTable());
obj.setValue(dba.resultSet2Map(rs));
list.add(obj);
fecth++;
}
}
logger.debug("查询完数据!");
rs.close();
// dba.close();
} catch (Exception e) {
list = null;
logger.error("查询数据错误:" + e + ":sql=" + sql);
e.printStackTrace();
if (dba != null) {
try {
dba.rollback();
// dba.close();
} catch (SQLException sqle) {
}
}
} finally {
try {
dba.close();
if (autoCommit)
releaseConnection();
} catch (Exception e) {
logger.error("释放数据库资源错误:" + e);
}
}
return list;
}
/**
* 直接执行SQL语句完成数据库更新操作。
*
* @param sql
* String 执行insert、delete、update等的SQL语句
* @return int执行条数
* @throws Exception
*/
public int execute(String sql) throws Exception {
return execute(sql, null);
}
/**
* Description: 直接执行SQL语句完成数据库更新操作。
*
* @param sql
* String 执行insert、delete、update等的SQL语句
* @param params
* Collection 参数
* @return int 执行条数
* @throws Exception
*/
public int execute(String sql, Collection params) throws Exception { // 执行sql语句,返回影响的记录数
int ret = -1;
DatabaseDAO dba = null; // 引入数据库操作工具类
try {
dba = getDatabaseDAO();
dba.prepare(sql);
dba.setQueryParams(params);
if (this.showSql) {
System.out.println("EasyDBO:" + sql);
}
if (autoCommit == false) {
vecExe.add(dba.getPreparedStatement());
} else {
boolean result = dba.execute();
if (result) {
logger.debug("it is query sql.");
} else {
ret = dba.getPreparedStatement().getUpdateCount();
}
}
} catch (Exception e) {
logger.error("添加数据错误:" + e + ":sql=" + sql);
if (dba != null) {
try {
dba.rollback();
} catch (SQLException sqle) {
}
}
} finally {
try {
dba.close();
if (autoCommit)
releaseConnection();
} catch (Exception e) {
logger.error("释放数据库资源错误:" + e);
}
}
return ret;
}
/**
*
* @return DataSource
*/
public DataSource getDataSource() {
return dataSource;
}
public boolean isAutoCommit() {
return autoCommit;
}
/**
* 取得数据源
*
* @return Connection
*/
public Connection getConnection() {
Connection conn = null;
try {
conn = JDBCContext.getJdbcContext(dataSource).getConnection();
} catch (Exception e) {
logger.error("无没取得数据源,请确认数据源配置是否正确!");
return null;
}
return conn;
}
/**
* 取得数据库操作工具类
*
* @return DatabaseDAO
*/
public DatabaseDAO getDatabaseDAO() throws SQLException {
DatabaseDAO dba = null; // 引入数据库操作工具类
// try {
// 新增加,重新从连接沲中给DatabaseDAO赋值数据源
dba = new DatabaseDAO(getConnection(), autoCommit);
// dba.setConnection(getConnection());
// } catch (SQLException ex) {
// logger.error("无没取得数据源,请确认数据源配置是否正确!");
// }
return dba;
}
/**
* 设置DataSource
*
* @param dataSource
* DataSource
*/
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void setAutoCommit(boolean autoCommit) {
this.autoCommit = autoCommit;
}
public boolean isShowSql() {
return showSql;
}
public void setShowSql(boolean showSql) {
this.showSql = showSql;
}
public ISqlQuery getSqlQuery() {
return sqlQuery;
}
public void setSqlQuery(ISqlQuery sqlQuery) {
this.sqlQuery = sqlQuery;
}
/**
* todo: piginzoo 执行事务提交操作 BY PIGINZOO
*/
public void commit() {
try {
vec.trimToSize();
UpdateBatch();
} catch (Exception e) {
logger.error("释放数据库资源错误:" + e);
}
}
public void rollback() {
}
/**
* 本函数是用于批处理更新数据库表的数据
*
* @param vector:是需要更新的PreparedStatement的集合
* @throws WFManagerException
*/
private void UpdateBatch() {
DatabaseDAO dba = null; // 引入数据库操作工具类
PreparedStatement preTmp;
Object obj = null;
if ((vec != null && vec.size() > 0)
|| (vecExe != null && vecExe.size() > 0)) {
try {
if (dba == null) {
dba = getDatabaseDAO();
dba.setAutoCommit(false);
}
if (vec != null && vec.size() > 0) {
vec.trimToSize();
for (int i = 0; i < vec.size(); i++) {
obj = vec.get(i);
if (obj == null) {
throw new EasyDBOException("数据更新失败!");
}
preTmp = (PreparedStatement) (obj);
dba.setPreparedStatement(preTmp);
dba.preparedUpdate();
}
}
if (vecExe != null && vecExe.size() > 0) {
vecExe.trimToSize();
for (int i = 0; i < vecExe.size(); i++) {
obj = vecExe.get(i);
if (obj == null) {
throw new EasyDBOException("数据更新失败!");
}
preTmp = (PreparedStatement) (obj);
dba.setPreparedStatement(preTmp);
dba.execute();
}
}
dba.getConnection().commit();
} catch (Exception e) {
e.printStackTrace();
if (dba != null) {
try {
dba.rollback();
dba.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
} finally {
try {
dba.close();
if (autoCommit)
releaseConnection();
} catch (Exception e) {
logger.error("释放数据库资源错误:" + e);
}
}
} else {
logger.error("No CRUD exist.");
}
}
public void setMaxSize(int maxSize) {
this.maxSize = maxSize;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -