This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JDBC and the fly likes Trying to make Java method to get the length of a column Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Trying to make Java method to get the length of a column" Watch "Trying to make Java method to get the length of a column" New topic
Author

Trying to make Java method to get the length of a column

Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 234
    
    1

Here's what I've tried. The 'rs' variable references a ResultSet object, and the 'meta' variable references a DatabaseMetaData object.



The column I'm trying to access contains 3 rows with the values being "Joel", "Hannah", and "Kevin. Since I'm trying to get the size of the column, my desired return value would be 3, but it's returning 25.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

That's because the size of a column is its length. It has nothing whatsoever to do with the number of rows.

In fact, the number of rows has nothing at all to do with a specific column but the table as a whole.

If you want to get the number of rows, you'd do that with a SELECT COUNT(*) statement.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19685
    
  20

If you want to exclude records with NULL values you should replace that COUNT(*) with COUNT(name_of_column).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 234
    
    1

Thanks for the help; it now returns 3. My new method:

 
Don't get me started about those stupid light bulbs.
 
subject: Trying to make Java method to get the length of a column