开发者

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

This question already has answers here: Closed 11 years ago.

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,


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜