java 使用java解析SQL语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16733798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing SQL Statements using java
提问by Navyah
I need to parse sql statements and get column names and table names. I tried with sample code. I got table names but I am stucked to get column names for each table.
我需要解析 sql 语句并获取列名和表名。我尝试使用示例代码。我得到了表名,但我不得不为每个表获取列名。
回答by Mike Q
If you really really need to do this then you should look at using a proper parser toolkit like ANTLR. It is quite a steep learning curve but there are grammars around for SQL that others have already built.
如果你真的需要这样做,那么你应该考虑使用一个合适的解析器工具包,比如ANTLR。这是一个相当陡峭的学习曲线,但有其他人已经构建的 SQL 语法。
Hand-cranking your own parser will lead you down a mess of bugs for anything but the most basic of queries.
手动启动您自己的解析器将导致您解决除最基本查询之外的任何错误。
回答by NPKR
回答by nakosspy
What you need is an SQL parser.
你需要的是一个 SQL 解析器。
You can try JSQLParser, or ZQL.
您可以尝试JSQLParser或ZQL。
I have used both successfully for the same thing the you are trying to do.
我已经成功地将两者用于您正在尝试做的同一件事。
You can try also sqlparser. This is commercial however and I have no experience using it.
你也可以试试sqlparser。然而,这是商业用途,我没有使用它的经验。
回答by kervin
Checkout Foundation Parser http://foundationdb.github.io/sql-parser/
结帐基础解析器http://foundationdb.github.io/sql-parser/
Also note JSQLParser is no longer on SourceForge but maintain at https://github.com/JSQLParser/JSqlParser/wiki
还要注意 JSQLParser 不再在 SourceForge 上,而是在https://github.com/JSQLParser/JSqlParser/wiki维护
回答by Sebastien
You will need to create a parser for your SQL grammar. ANTLR allows to create such parsers but it can be quite difficult to create a grammar.
您将需要为您的 SQL 语法创建一个解析器。ANTLR 允许创建这样的解析器,但创建语法可能非常困难。
Here is a list of exisiting grammar for ANTLR http://www.antlr3.org/grammar/list.html
这是 ANTLR 的现有语法列表http://www.antlr3.org/grammar/list.html
It contains grammar for mysql, oracle SQL that should work for you.
它包含适用于您的 mysql、oracle SQL 的语法。