Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get details without querying full table ResultSetMetaData

 
sorabh jaiswal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I wish to get the metadata information from database.I want to write my code independent of the database. I am using the following code...

String queryString = "select employeeName,address from employee where 1 = -1"
ResultSetMetaData rsmd = null;
rs = stmt.executeQuery(queryString);
rsmd = rs.getMetaData();

rsmdSize = rsmd.getColumnCount();

for(int i = 1;i <= rsmdSize;i++){
System.out.println("the col types " + rsmd.getColumnTypeName(i) );
System.out.println("the Size types " + rsmd.getColumnDisplaySize(i));
}


There is a drawback in this, that select statement will search the full table for fetching the data bcoz the where clause is "where 1 = -1"
As if the employee table consist of billion records it will take lot of time.
Can anyone guide me how to get the details through resultset without searching the full table?
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sorabh jaiswal:
Hi All,

I wish to get the metadata information from database.I want to write my code independent of the database. I am using the following code...

String queryString = "select employeeName,address from employee where 1 = -1"
ResultSetMetaData rsmd = null;
rs = stmt.executeQuery(queryString);
rsmd = rs.getMetaData();

rsmdSize = rsmd.getColumnCount();

for(int i = 1;i <= rsmdSize;i++){
System.out.println("the col types " + rsmd.getColumnTypeName(i) );
System.out.println("the Size types " + rsmd.getColumnDisplaySize(i));
}


There is a drawback in this, that select statement will search the full table for fetching the data bcoz the where clause is "where 1 = -1"
As if the employee table consist of billion records it will take lot of time.
Can anyone guide me how to get the details through resultset without searching the full table?


So even if you get ResultSet of one row, you can get meta data information about table?

If yes, then will it work?

Select * from employee where rownum="1";

Or insert a dummy employee just for this purpose and do this:

Select * from employee where employeeName="dummyName";

BTW, any reason for providing a where clause (1=-1). Even if you don't provide, it will fetch all the rows.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic