-1

consider a query like select a.apple,b.mango,c.orange from A a,B b,C c where ... (Some conditions)

here i need to fetch only the column names based on the query.

apple mango orange.

we have a query builder where end user creates/generates any type of query using that, my duty is to select only the column names from the query as above for my further operations.

How can i achieve this, through java code or query?

my db is sql server 2005.

bytecode77
  • 14,163
  • 30
  • 110
  • 141

5 Answers5

1

In the case you need only to display the columns name that you already know you can simply put them(column names) directly into the SELECT list :

SELECT apple AS apple ,mango AS mango,orange AS orange

otherwise you can query the information schema service table of SQL Server :

SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME=OBJECT_NAME(OBJECT_ID('a')) OR TABLE_NAME=OBJECT_NAME(OBJECT_ID('b')) OR TABLE_NAME=OBJECT_NAME(OBJECT_ID('c'))

With Java and from the original query you can read the column names using ResultSetMetaData object :

ResultSetMetaData rsmd = rs.getMetaData();
 String apple = rsmd.getColumnName(1); //Column apple
 String mango = rsmd.getColumnName(2);
aleroot
  • 71,077
  • 30
  • 176
  • 213
1

to get the ResultSetMetaData java interface

PreparedStatement ps=con.prepareStatement("select * from your_table_name");
ResultSet rs=ps.executeQuery();

ResultSetMetaData rsmd=rs.getMetaData();

System.out.println("Total columns: "+rsmd.getColumnCount());
System.out.println("Column Name of 1st column: "+rsmd.getColumnName(1));

con.close();
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0

if you are using eclipseLink you could get the columns names as per this link

JPA/EclipseLink - Retrieve Columns Names

Community
  • 1
  • 1
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • [How to Answer](http://stackoverflow.com/questions/how-to-answer): **Provide context for links** - A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. – BackSlash Aug 19 '13 at 06:52
  • Thank you for your valuable reply. – user2677772 Aug 19 '13 at 09:59
0

I found a solution for this and it is working fine. Thank you everyone for spending your valuable time.

We can achieve this in 2 ways.

  1. Through Map. ex:

List> lsMapStringObj = new ArrayList>();
for(Map myMap: lsMapStringObj)
{
for (Map.Entry entry : myMap.entrySet())
{
lsRsult.add(entry.getKey());
}
}

But here if the query returns > 0 rows then only i can fetch the column names.

  1. This gives the solution as i expect (>= 0 rows)

    PreparedStatement ps=con.prepareStatement(sQuery);
    ResultSet rs=ps.executeQuery();
    ResultSetMetaData rsmd=rs.getMetaData();
    int numColumns = rsmd.getColumnCount();
    for (int i = 1; i <= numColumns; ++i)
    {
    lsResult.add(rsmd.getColumnName(i));
    }
    return lsResult;

Finally Got it, Hope it helps to others. Enjoy Programming...!!!

0

One of the simpler solutions is to tokenize the column names using String.split. It will return an array of columnNames.

    String query = "select a.apple,b.mango,c.orange from A a,B b,C c where ... (Some conditions)";
    String[] columnNames= query.split(" [fF][rR][oO][mM] ")[0]
                               .split("[ ]*[sS][eE][lL][eE][cC][tT] ")[1]
                               .split("[ ]*,[ ]*");
    for(int i=0; i<columnNames.length;i++){
       System.out.println(i+".)"+columnNames[i]);
    }

Output:

{a.apple,b.mango,c.orange} //String array
//Sysout    
1.)a.apple        
2.)b.mango        
3.)c.orange
mattyman
  • 351
  • 2
  • 16