⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlparser.java

📁 java版源代码,里面包含很多源代码,大家可以看看.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.trulytech.mantis.system;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import java.util.HashMap;import java.sql.Connection;import java.sql.Statement;import com.trulytech.mantis.result.DBResult;/** * <p>Title: SQLParser</p> * <p>Description: SQL解析器</p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: trulytech</p> * @author WangXian * @version 1.2 */public class SQLParser    extends DBController {//页面请求  protected HttpServletRequest request = null;//数据库连接  protected Connection conn = null;  /**   *构造函数   * @param stmt Statement   * @param request SQL请求   * @throws Exception   */  public SQLParser(Statement stmt, HttpServletRequest request) throws Exception {    super(stmt);    this.request = request;    this.conn = stmt.getConnection();  }  /**   * 获得数据库连接   * @return Connection 数据库连接   */  public Connection getConnection() {    return conn;  }  /**   *解析单一SQL (insert,update)   * @param SQLStatement SQL语句   * @throws Exception   */  public void SQLExecute(String SQLStatement) throws Exception {    String SQL = SQLStatement.trim();//是否运行多次    boolean isMulti = false;    int i = 0;//写日志    logWriter.Debug(SQLStatement);    if (SQL.length() == 0)      throw new Exception("SQL语句为空");    else if (SQL.substring(0, 6).equalsIgnoreCase("select"))      throw new Exception("SQL查询语句使用错误");//判断是否为多记录操作    for (i = 0; i < SQL.length() - 2; i++) {      if (SQL.charAt(i) == '[' && SQL.charAt(i + 1) == '_' &&          SQL.charAt(i + 2) == '*') {        isMulti = true;        break;      }    }    if (!isMulti)      ParseSingle(SQL);    else      ParseMulti(SQL);  }  /**   *解析单一查询SQL (select)   * @param SQLStatement SQL语句   * @return DBResult   * @throws Exception   */  public DBResult QueryExecute(String SQLStatement) throws Exception {    String SQL = SQLStatement.trim();//写日志    logWriter.Debug(SQLStatement);    if (SQL.length() == 0)      throw new Exception("SQL语句为空");    else if (!SQL.substring(0, 6).equalsIgnoreCase("select"))      throw new Exception("SQL查询语句使用错误");    return ParseQuery(SQL);  }  /**   *解析单一查询SQL (select), 如果查询不到记录,则返回的String[]中每个item都是null, 而String[]不为null   * @param SQLStatement SQL语句   * @return String[]   * @throws Exception   */  public String[] QuerySingle(String SQLStatement) throws Exception {    String SQL = SQLStatement.trim();//写日志    logWriter.Debug(SQLStatement);    if (SQL.length() == 0)      throw new Exception("SQL语句为空");    else if (!SQL.substring(0, 6).equalsIgnoreCase("select"))      throw new Exception("SQL查询语句使用错误");    return QrySingle(SQL);  }  /**   * 解析分页查询SQL (select)   * @param SQLStatement SQL语句   * @return DBResult   * @throws Exception   */  public DBResult PageExecute(String SQLStatement) throws Exception {    return PageExecute(SQLStatement, getReqParam(Properties.Action_Tag));  }  /**   * 解析分页查询SQL (select)   * @param SQLStatement String SQL语句   * @param ActionName String Action名字,用于别的Action调用另外的Action进行分页使用   * @return DBResult 返回结果集   * @throws Exception   */  public DBResult PageExecute(String SQLStatement, String ActionName) throws      Exception {    String SQL = SQLStatement.trim();    int i = 0;//页码    String Page = null;//每页大小    String rowsPerPage = null;//总页码    String TotalPage = null;    Page = getReqParam("page");    rowsPerPage = getReqParam("len");    TotalPage = getReqParam("total");    if (Page == null)      Page = "0";    if (rowsPerPage == null)      rowsPerPage = Properties.PageSize;    if (TotalPage == null)      TotalPage = "0";//写日志    logWriter.Debug(SQLStatement);    if (SQL.length() == 0)      throw new Exception("SQL语句为空");    else if (!SQL.substring(0, 6).equalsIgnoreCase("select"))      throw new Exception("SQL查询语句使用错误");    return ParseQuery(SQL, Integer.valueOf(Page.trim()).intValue(),                      Integer.valueOf(rowsPerPage.trim()).intValue(),                      Integer.valueOf(TotalPage.trim()).intValue(), ActionName);  }  /**   *解析单一查询SQL[单条]   * @param SQLStatment SQL语句   * @return DBResult   * @throws Exception   */  private DBResult ParseQuery(String SQLStatment) throws Exception {    StringBuffer tmpBuffer = new StringBuffer();//是否出现 [    boolean isPreBegin = false;//是否出现[_    boolean isBegin = false;//是否出现[!    boolean isSession = false;//参数列表    HashMap param = new HashMap();//变量值    String paramValue=null;//变量名称    StringBuffer paramName = new StringBuffer();    int nLength=SQLStatment.length();    for (int i = 0; i < nLength; i++) {      if (!isBegin && !isSession && SQLStatment.charAt(i) == '[')        isPreBegin = true;      else if (isPreBegin && SQLStatment.charAt(i) == '_') {        isBegin = true;        isPreBegin = false;      }      else if (isPreBegin && SQLStatment.charAt(i) == '!') {        isSession = true;        isPreBegin = false;      }      else if (isPreBegin && SQLStatment.charAt(i) != '!' &&               SQLStatment.charAt(i) != '_') {        tmpBuffer.append('[');        tmpBuffer.append(SQLStatment.charAt(i));        isPreBegin = false;        isBegin = false;        isSession = false;      }      else if ( (isBegin || isSession) && SQLStatment.charAt(i) != ']') {        paramName.append(SQLStatment.charAt(i));      }      else if ( (isBegin || isSession) && SQLStatment.charAt(i) == ']') {        if (isBegin) {          if (tmpBuffer.charAt(tmpBuffer.length() - 1) == '\'')          {            paramValue = getParameter(paramName.toString(), true, true);            tmpBuffer.append(paramValue);             param.put(paramName.toString(), paramValue);          }          else          {            paramValue = getParameter(paramName.toString(), true, false);            tmpBuffer.append(paramValue);             param.put(paramName.toString(), paramValue);          }        }        else if (isSession)        {          paramValue = getSessionParameter(paramName.toString());          tmpBuffer.append(paramValue);           param.put(paramName.toString(), paramValue);        }        isPreBegin = false;        isBegin = false;        isSession = false;        paramName.delete(0, paramName.length());      }      else {        tmpBuffer.append(SQLStatment.charAt(i));        isPreBegin = false;        isBegin = false;        isSession = false;      }    }    return QrySQL(tmpBuffer.toString(),param);  }  /**   *解析单一查询SQL[单条]   * @param SQLStatment SQL语句   * @throws Exception   */  private void ParseSingle(String SQLStatment) throws Exception {    StringBuffer tmpBuffer = new StringBuffer();//是否出现 [    boolean isPreBegin = false;//是否出现[_    boolean isBegin = false;//是否出现[!    boolean isSession = false;//变量名称    StringBuffer paramName = new StringBuffer();    int nLength=SQLStatment.length();    for (int i = 0; i < nLength; i++) {      if (!isBegin && !isSession && SQLStatment.charAt(i) == '[')        isPreBegin = true;      else if (isPreBegin && SQLStatment.charAt(i) == '_') {        isBegin = true;        isPreBegin = false;      }      else if (isPreBegin && SQLStatment.charAt(i) == '!') {        isSession = true;        isPreBegin = false;      }      else if (isPreBegin && SQLStatment.charAt(i) != '!' &&               SQLStatment.charAt(i) != '_') {        tmpBuffer.append('[');        tmpBuffer.append(SQLStatment.charAt(i));        isPreBegin = false;        isBegin = false;        isSession = false;      }      else if ( (isBegin || isSession) && SQLStatment.charAt(i) != ']') {        paramName.append(SQLStatment.charAt(i));      }      else if ( (isBegin || isSession) && SQLStatment.charAt(i) == ']') {        if (isBegin) {          if (tmpBuffer.charAt(tmpBuffer.length() - 1) == '\'')            tmpBuffer.append(getParameter(paramName.toString(), false, true));          else            tmpBuffer.append(getParameter(paramName.toString(), false, false));        }        else if (isSession)          tmpBuffer.append(getSessionParameter(paramName.toString()));        paramName.delete(0, paramName.length());        isPreBegin = false;        isBegin = false;        isSession = false;      }      else {        tmpBuffer.append(SQLStatment.charAt(i));        isPreBegin = false;        isBegin = false;        isSession = false;      }    }    SQL(tmpBuffer.toString());  }  /**   *解析单一查询SQL[单条]   * @param SQLStatment SQL语句   * @return String[]   * @throws Exception   */  private String[] QrySingle(String SQLStatment) throws Exception {    StringBuffer tmpBuffer = new StringBuffer();//是否出现 [    boolean isPreBegin = false;//是否出现~_    boolean isBegin = false;//是否出现[!    boolean isSession = false;//变量名称    StringBuffer paramName = new StringBuffer();    int nLength=SQLStatment.length();    for (int i = 0; i < nLength; i++) {      if (!isBegin && SQLStatment.charAt(i) == '[')        isPreBegin = true;      else if (isPreBegin && SQLStatment.charAt(i) == '_') {        isBegin = true;        isPreBegin = false;      }      else if (isPreBegin && SQLStatment.charAt(i) == '!') {        isSession = true;        isPreBegin = false;      }      else if (isPreBegin && SQLStatment.charAt(i) != '!' &&               SQLStatment.charAt(i) != '_') {        tmpBuffer.append('[');        tmpBuffer.append(SQLStatment.charAt(i));        isPreBegin = false;        isBegin = false;        isSession = false;      }      else if ( (isBegin || isSession) && SQLStatment.charAt(i) != ']') {        paramName.append(SQLStatment.charAt(i));      }      else if ( (isBegin || isSession) && SQLStatment.charAt(i) == ']') {        if (isBegin) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -