aspose file tools*
The moose likes JDBC and the fly likes count number of coulmns in table? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "count number of coulmns in table?" Watch "count number of coulmns in table?" New topic
Author

count number of coulmns in table?

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

how to get count of number of coulmns in a table in sql query?


No Kaustubh No Fun, Know Kaustubh Know Fun..
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
java.sql.Connection.getMetaData() is the DB-independent way to do this.
Pravin Shirke
Ranch Hand

Joined: Apr 05, 2008
Posts: 150

hi kaustubh,
create a ResultSetMetaData object on resultset and get the column count of your sql query. like this



where rs is the resultset object

Hope this helps


[Vipassana] - It is seeing the reality as it is, And not as you want it to be.!!!
SCJP1.5.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

sir I want this is sql query not on java side...
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Then you need to consult the documentation of whatever DB you're using. There's no standard for that.
Sandeep Sanaboyina
Ranch Hand

Joined: Dec 14, 2009
Posts: 72
Which database are you using..???


They say you have to be the first, the best or different. I say, is it too much to ask for all three.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

Microsoft SQL Server 2005
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Kaustubh G Sharma wrote:sql 2005

You mean Microsoft SQL Server 2005 ?


[My Blog]
All roads lead to JavaRanch
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

yes sir
Sandeep Sanaboyina
Ranch Hand

Joined: Dec 14, 2009
Posts: 72
Kaustubh G Sharma wrote:Microsoft SQL Server 2005
No experience in SQL Server

Googled it though.. found the below link. Check post 11 in it. Hope it helps.

http://blog.sqlauthority.com/2007/01/10/sql-server-query-to-find-number-rows-columns-bytesize-for-each-table-in-the-current-database-find-biggest-table-in-database/
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

If I search for ms sql server column count the sixth hit is http://forums.devx.com/archive/index.php/t-25101.html. This shows you how to do it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

thanks rob well I found this idea before not found any single way out to solve the problem....or atleast possible please let me knew how to do it in "derby" will be very greatful...
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Sorry, are you looking for a way that will work across all databases? Lester has already pointed you at the solution for that.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

Come to know that we don't have any standard for all the databases for the above query but want to know the solution for "derby"
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Substituting "derby" for "ms sql server" in the search Rob mentioned would be the obvious first step to try; have you done that?
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

yup I already did that..
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
So the problem is solved now?
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

No Sir I want the same thing for derby....
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Does the documentation not tell you this? Given your responses so far I'm not sure anyone here knows. You are probably going to get your answer a bit faster if you check there.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Let me make it clear:
There is no query that will work on all database systems.
Sorry that I made it so big, but the message didn't seem to get through.

So you have two options:
- use a different query for all database systems you want to support (using Google initially to find those queries)
- follow Lester's advice: check out DatabaseMetaData; you can get an instance using Connection.getMetaData(). There is one method that returns a ResultSet for all columns. You then only need to go through the ResultSet, incrementing a counter for each result:
Make sure to close() the ResultSet when done.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Rob Prime wrote:There is no query that will work on all database systems.

There is no such query... I say that for people who could take it out of its context.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Christophe Verré wrote:I say that for people who could take it out of its context.

You must have ... interesting ... people at your workplace
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

There is no query that will work on all database systems.


sad to here that....


No Solution found for DERBY as well...
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

Kaustubh G Sharma wrote:

No Solution found for DERBY as well...


Did you check their manual? Specifically, did you check their documentation on system tables? Based on that documentation, I can see that it's possible to find the column count. You'll need admin privileges to query those system tables.

[My Blog] [JavaRanch Journal]
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1281

thanks jaikiran your link really help me out to solve the problem
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: count number of coulmns in table?