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

📄 sqlparserutil.java

📁 简单的web应用,实现基本的购物车功能,有良好的扩展性
💻 JAVA
字号:
package myjdbc.util;

import java.util.StringTokenizer;

public final class SQLParserUtil {
    //获得SQL语句中查询的列名
    public static String[] parserSQL(String  _sql ,String beginClause ,String endClause)
    {
      String sql = new String(_sql);
      String temp = null;
      int bg = sql.indexOf(beginClause);
      int end = sql.indexOf(endClause);
      if (end != -1)
        temp = sql.substring(bg+beginClause.length()-1+2,end);
      else
        temp = sql.substring(bg+beginClause.length()-1+2);
//      System.out.println("temp = " + temp);
      if (temp.trim().equals("*"))
        return  null;

      StringTokenizer st = new StringTokenizer(temp,",");
      String[] result = new String[st.countTokens()];
//      if (result.length == 1)
//     {
//         result[0] = temp.trim();
//         return result;
//     }
      int i= 0;
      while(st.hasMoreTokens())
      {
        String tempa = st.nextToken().trim();
        if (tempa.indexOf(".")!= -1)
        {
          tempa = tempa.substring(tempa.indexOf(".")+1).trim();
        }
        if (tempa.indexOf(" as ")!= -1)
        {
          tempa = tempa.substring(tempa.indexOf(" as ")+4).trim();
        }
        if (tempa.indexOf(" ") != -1)
        {
          tempa = tempa.substring(tempa.lastIndexOf(" ")+1).trim();
        }
        result[i] = tempa;
        i++;
        System.out.println(" tokens = " + tempa);
      }
      return result;
    }

    //获得SQL查询中的表名
    public static String[] getTableName(String sql) {
      int bg = sql.indexOf("from");
      int end = sql.indexOf("where");
      String temp = sql.substring(bg + 5);
      if (end != -1)
        temp = temp.substring(0, end).trim();

      if (temp.indexOf(" as ") != -1) {
        temp = temp.substring(0, temp.indexOf(" as ")).trim();
      }
      if (temp.indexOf(" ") != -1) {
        temp = temp.substring(0, temp.indexOf(" ")).trim();
      }
     // System.out.println("tabel name = " + temp);
      return new String[] {
          temp};
    }

}

⌨️ 快捷键说明

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