Parser to parse an SQL query and return the column name's and the corresponding table name in Java [duplicate]

爱⌒轻易说出口 提交于 2019-12-06 04:39:08

问题


Possible Duplicate:
SQL parser library for Java

I need a parser that should return me column names with their corresponding table names in the format

column_name table_name

Preferably a java library, that can make the implementation easier as I dont want to get into all the nuances of SQL parsing.

regards,


回答1:


If you have a connection to the database, you can do that using a prepared statement:

String sql = "....";
PreparedStatement pstmt = connection.prepareStatement(sql);
ResultSetMetaData meta = pstmt.getMetaData();

Then you can use ResultSetMetaData.getColumnCount() and getColumnName(int) to retrieve the column names. If your JDBC driver supports it, you can even get the underlying table name using getTableName(int)

Note that not all drivers support getting the metadata before actually executing the statement, you need to test that with the one you are using.




回答2:


Good question! I have just performed some google search and found the following interesting references:

SQL parser library for Java

http://www.gibello.com/code/zql/

http://www.sqlparser.com/sql-parser-java.php

I hope this helps.



来源:https://stackoverflow.com/questions/6353879/parser-to-parse-an-sql-query-and-return-the-column-names-and-the-corresponding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!