JDBC-Resultsetmetadata
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Gilles_Crettaz
Posted On:   Thursday, November 29, 2001 03:58 AM


I use the jdbc sql server 2000 to connect to my sql database. I can get a resultset with a query like "select * from region" but i need to keep the table name.. How can i get it ??


i try to do ResultSetMetaData rsmd = myResultSet.getMetaData() ; String tableName = rsmd.getTableName(1);


but nothing is happen why tanks a lot of for the answers

Re: JDBC-Resultsetmetadata

Posted By:   Bernie_Acs  
Posted On:   Thursday, November 29, 2001 09:19 AM

Since I do not have a SQL Server at my disposal at this time I tested several scenerios on several other DB's and reproduced a circumstance that made a "nothing happen" after several sucessful runs. In my test a created a table with a few rows in it. I wrote a tiny class to fetch those rows and call the method getTableName()
to print the table's name before the data dump. The circumstance that produced a null return from the getTableName() method was when I used a hardvalue or calculated value as the column being referenced by the call.
In every case against each DB type the method produced the correct output, print the tables name and the only exception was when the column being referenced by the method was a calculcated or hardvalue the resultant was a null value. Below is a snipplet of code that contains three line which define the queries used to preform these test with the results of each embedded in the comments.


Java Snipplet

java.sql.Statement statmnt = conctn.createStatement();
/*
** get all rows and columns defined on table2
** getTableName() works as expected
*/
// java.sql.ResultSet reslts = statmnt.executeQuery("Select * from table2");

/*
** get a calculated value plus rows and columns defined on table2
** getTableName() works correctly too.
*/
// java.sql.ResultSet reslts = statmnt.executeQuery("Select ID+col2,* from table2");

/*
** get a calculated value plus all rows and columns defined on table2
** calc value makes null in getTableName() ** this is equal to a hardvalue
*/
// java.sql.ResultSet reslts = statmnt.executeQuery("Select 1+2 , * from table2");

/*
** get A hardvalue plus all rows and columns defined on table2
** hardvalue makes null in getTableName()
*/
java.sql.ResultSet reslts = statmnt.executeQuery("Select 'hardvalue' , * from table2 where col1 is null");

java.sql.ResultSetMetaData rsmd = reslts.getMetaData() ;
String tableName = rsmd.getTableName(1);

System.out.println("TableName: "+tableName);

while(reslts.next())
{
System.out.println("ID: "+reslts.getString("ID")+" , " + reslts.getString("col1")+ " , " + reslts.getString("col2"));
}
System.out.println("Printed all Rows");
}




Is it possible that your scenerio could involve a circumstance similar to these findings ?

You could try setting up a loop and cycle through all the columns available in your result set to see what might be happening.. Since your are not being returned an error and the result seems to be null, you will have to dig deeper into it to figure out the WHY...
About | Sitemap | Contact