0

I'm currently trying to get specific information from a String containing SQL queries. These Strings can be as simple as a single simple SQL query to hundreds of complex queries.

What I'm looking for is a simple way to get the names of all the tables from the FROM statements. Could someone point me towards any tools or classes suited for handling SQL in Java?

For example:

SELECT
     ITEM1, ITEM2
FROM
     TABLE1, 
     TABLE2
ORDER BY
     ITEM1

Now, the end goal would be to extract the table names from the FROM statement, and use them to create a query like this:

SELECT
     SUM(COUNT)
FROM
     (SELECT COUNT(*) AS COUNT FROM TABLE1
      UNION ALL
      SELECT COUNT(*) AS COUNT FROM TABLE2
)
AS ROWCOUNT;

If I had the table names as strings, it would be fairly simple to wrap them in the subqueries. From that we'd be able to get the total number of rows being returned by the database.

4
  • 2
    This maybe? stackoverflow.com/questions/660609/sql-parser-library-for-java Commented Jan 23, 2015 at 15:29
  • You can use String.indexof and mid to cut the tablenames out and split to make a array of table names. Commented Jan 23, 2015 at 15:32
  • Its not that easy, there are many ways a table name comes within the FROM statement like OP's second query Commented Jan 23, 2015 at 15:51
  • Exactly. Using String methods would be a nightmare to handle all the cases. Commented Jan 23, 2015 at 16:02

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.