CCJSqlParserManager pm = new CCJSqlParserManager();
Statement statement = pm.parse(new StringReader(sql));
2.定义一个visitor,对语句中的group by节点进行访问判断
public class SqlElementFinder implements SelectVisitor, FromItemVisitor{
private boolean isGroupBy;
private List<String> tables;
public SqlElementFinder(){
isGroupBy = false;
tables = new ArrayList<>();
public boolean isGroupBy() {
return isGroupBy;
public List<String> getTables() {
return tables;
public boolean judge(Select select){
select.getSelectBody().accept(this);
return isGroupBy;
@Override
public void visit(PlainSelect plainSelect) {
if(plainSelect.getGroupBy() != null){
isGroupBy = true;
plainSelect.getFromItem().accept(this);
@Override
public void visit(Table table) {
tables.add(table.getName());
@Override
//考虑子查询情况
public void visit(SubSelect subSelect) {
subSelect.getSelectBody().accept(this);
3.判断是否为select语句,并处理
SqlElementFinder sqlElementFinder = new SqlElementFinder();
if(statement instanceof Select){
System.out.println("select");
sqlElementFinder.judge(((Select) statement));
System.out.println(sqlElementFinder.isGroupBy());
System.out.println(sqlElementFinder.getTables());
} else if (statement instanceof Insert) {
System.out.println("insert");
} else if (statement instanceof Update){
System.out.println("update");
select cc from (select aa,bb,cc from tt1 where id = 1) group by cc;
select
[tt1]