-1

I want to parse a sql query and extract only column names from it. I can't just extract the part between 'select' and 'from' because the query can also have an alias. This is the requirement for now, but later on any type of query can come in, so I need a dynamic method/ library which will return the column names.

2
  • Note that questions asking us to recommend or find a library are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Commented Feb 6, 2014 at 6:01
  • possible duplicate of SQL - Parsing a query Commented Feb 6, 2014 at 8:56

1 Answer 1

2

You need to use the ResultSetMetaData to get the names of the columns in the result set returned after executing the query.

ResultSet rs = stmt.executeQuery(...); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount() for (int i = 1; i <= columnCount; i++) { System.out.println(rsmd.getColumnName(i)); } 

Note: The index starts from 1 and not from 0

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.