标签:
SQL Parser 模块的介绍:
相关 API:
druid 使用说明:
<groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.19</version> </dependency>
package com.test.mvn.sql_parser; import java.util.List; import com.alibaba.druid.sql.SQLUtils; import com.alibaba.druid.sql.ast.SQLStatement; import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; import com.alibaba.druid.util.JdbcConstants; /** * Hello world! * */ public class App { public static void main(String[] args) { // String sql = "update t set name = ‘x‘ where id < 100 limit 10"; // String sql = "SELECT ID, NAME, AGE FROM USER WHERE ID = ? limit 2"; // String sql = "select * from tablename limit 10"; String sql = "select user from emp_table"; String dbType = JdbcConstants.MYSQL; //格式化输出 String result = SQLUtils.format(sql, dbType); System.out.println(result); // 缺省大写格式 List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType); //解析出的独立语句的个数 System.out.println("size is:" + stmtList.size()); for (int i = 0; i < stmtList.size(); i++) { SQLStatement stmt = stmtList.get(i); MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); stmt.accept(visitor); //获取表名称 System.out.println("Tables : " + visitor.getCurrentTable()); //获取操作方法名称 System.out.println("Tables : " + visitor.getTables()); //获取字段名称 System.out.println("fields : " + visitor.getColumns()); } } }
mvn install -Dmaven.javadoc.skip=true -Dmaven.test.skip=true
<groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.19-SNAPSHOT</version> </dependency>
利用 druid 的 sql parser 模块解析 sql 语句
标签:
原文地址:http://www.cnblogs.com/etangyushan/p/5490183.html