日期:2014-05-16 浏览次数:20648 次
数据校验功能后台主要包括两个类:QTPCommonServlet.java和QTPCommonDao.java
其中QTPCommonServlet.java为:
package com.runqianapp.qtp; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.runqianapp.qtp.QTPCommonDao; import com.geezn.exception.GeeznException; import com.runqianapp.common.log.GEZLoggerManager; import com.runqianapp.common.servlet.AbstractGezServlet; /** * QTP数据检查Servlet * @author RAQ * */ public class QTPCommonServlet extends AbstractGezServlet { /**获取表中所有数据*/ private static final String GET_ALL_DATA = "getAllData"; /**检查删除操作结果*/ private static final String CHECK_DELETE_RESULT = "checkDeleteResult"; /** * 重写service()方法 */ public void service(HttpServletRequest request, HttpServletResponse response){ response.setContentType("text/html"); PrintWriter out = null; try { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); out = response.getWriter(); } catch (Exception e) { GeeznException ge; if(e instanceof GeeznException){ ge = (GeeznException) e; }else{ ge = new GeeznException(QTPCommonDao.MODULE_QTP, GEZLoggerManager.LOGLEVEL_ERROR,"设置字符格式、获取输出对象时,发生错误", e, "{}"); } out.print(ge.getErrorMessageJson()); } String action = request.getParameter("action"); if(GET_ALL_DATA.equals(action)){ String sql = request.getParameter("sql"); String sqlDateNumber = request.getParameter("sqlDateNumber"); String allDatas = ""; try { allDatas = QTPCommonDao.getAllDatas(sql,sqlDateNumber); } catch (GeeznException e) { GeeznException ge; if(e instanceof GeeznException){ ge = (GeeznException) e; }else{ ge = new GeeznException(QTPCommonDao.MODULE_QTP, GEZLoggerManager.LOGLEVEL_ERROR,"得到表中数据时,发生错误", e, "{}"); } out.print(ge.getErrorMessageJson()); } out.print(allDatas); }else if(CHECK_DELETE_RESULT.equals(action)){ String sql = request.getParameter("sql"); boolean deleteResult = false; try { deleteResult = QTPCommonDao.checkDeleteResult(sql); } catch (GeeznException e) { GeeznException ge; if(e instanceof GeeznException){ ge = (GeeznException) e; }else{ ge = new GeeznException(QTPCommonDao.MODULE_QTP, GEZLoggerManager.LOGLEVEL_ERROR,"检查删除动作结果时,发生错误", e, "{}"); } out.print(ge.getErrorMessageJson()); } out.print(deleteResult); } } }
QTPCommonDao.java为:
package com.runqianapp.qtp; import java.sql.*; import com.geezn.exception.GeeznException; import com.runqian.mis2.util.DBAccess; import com.runqianapp.common.log.GEZLoggerManager; /** * QTP通用数据库操作类 * @author RAQ * */ public class QTPCommonDao { /** * 用于存储表字段名称,各字段名称之间用“;”分隔 */ public static StringBuffer columns = new StringBuffer(); /** * 用于存储当前操作表名 */ public static String tableName = ""; /** * 用于日志输出,模块标识 */ public static final String MODULE_QTP = "qtpModule"; /*** * 获取表字段名称及所有数据信息 * @param sql 用户输入的查询sql语句 * @param sqlDateNumber 查询数据条数(保留功能,目前暂不处理) * @return 表字段名称及所有链接信息 如:"short_link;...;link_name||/link1,...,百度@_@{};/link2,...,谷歌@_@{}" * 表字段名称与表数据之间用"||"分隔,各表字段名称之间与各表数据之间用";"分隔,每条表数据内部用"#"和"@_@"分隔 * 其中"#"用来分隔表字段数据,"@_@"用来分隔表字段数据与json字符串 * @throws GeeznException */ public static synchronized String getAllDatas(String sql, String sqlDateNumber) throws GeeznException { getTableName(sql); //解析出表名 PreparedStatement pstmt = null; DBAccess dba = null; ResultSet rs = null; StringB