import net.sf.jsqlparser.parser.CCJSqlParserUtil; import net.sf.jsqlparser.schema.Table; import net.sf.jsqlparser.statement.Statement; import net.sf.jsqlparser.statement.select.PlainSelect; import net.sf.jsqlparser.statement.select.Select; import net.sf.jsqlparser.util.TablesNamesFinder; import net.sf.jsqlparser.visitor.StatementVisitorAdapter; import java.util.ArrayList; import java.util.List; public class StoredProcParserExample { public static void main(String[] args) { String storedProcDefinition = "CREATE PROCEDURE `get_products`()\n" + "BEGIN\n" + " SELECT * FROM products WHERE price > 100;\n" + "END"; try { // 解析存储过程定义 Statement stmt = CCJSqlParserUtil.parse(storedProcDefinition); // 自定义Visitor来提取表名 TablesNamesVisitor visitor = new TablesNamesVisitor(); stmt.accept(visitor); List<String> tableNames = visitor.getTableNames(); // 打印表名 for (String tableName : tableNames) { System.out.println("Table: " + tableName); } } catch (Exception e) { e.printStackTrace(); } } static class TablesNamesVisitor extends StatementVisitorAdapter { private List<String> tableNames = new ArrayList<>(); public List<String> getTableNames() { return tableNames; } @Override public void visit(PlainSelect plainSelect) { TablesNamesFinder tablesNamesFinder = new TablesNamesFinder(); List<Table> tables = tablesNamesFinder.getTableList(plainSelect); for (Table table : tables) { tableNames.add(table.getFullyQualifiedName()); } } } }
标签:test4,List,jsqlparser,tableNames,import,net,sf From: https://www.cnblogs.com/wanglichaoya/p/17495772.html