wood burning stoves 2.0*
The moose likes JDBC and the fly likes How to get field size of a column in a data base table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to get field size of a column in a data base table" Watch "How to get field size of a column in a data base table" New topic
Author

How to get field size of a column in a data base table

MohanRaj Gurubatham
Greenhorn

Joined: Sep 05, 2001
Posts: 16
Hi Friends,
could any one help me with sample code to get field size of a column in a data base table.
Thanking you in advance.
With Regards,
GMohanraj

Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
In the ResultSetMetadata interface, there is a method called getColumnDisplaySize(). This could be DB Driver dependent. Check the API for more info.

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
MohanRaj Gurubatham
Greenhorn

Joined: Sep 05, 2001
Posts: 16
Thank u Bosun,
already I have tried using ResultSetMetaData, but it gives only the default size. For example it gives the size of the DATE field in MySQL as 10, but it doesn't care about other fields like varchar(20).
I need to know the size which we have set for that field.
Please help me to solve this.
With Regards,
GMohanraj
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

I'm not sure what you are getting at, but rsmd.getColumnDisplaySize() the normal maximum number of characters allowed as the width of the designated column. Example for the first column in a database that is Varchar (20) field, rsmd.getColumnDisplaySize(1) should return 20. If it was a Varchar (5) column rsmd.getColumnDisplaySize(1) would return 5. This method will return to you (no matter which datatype) the "the size which we have set for that field"...which is what you are asking for
Jamie
MohanRaj Gurubatham
Greenhorn

Joined: Sep 05, 2001
Posts: 16
Yes you are very correct Jamie,
Thank you.
But this works only when there is a record in the table. My intention is to get the actual size of a field of a table in a database without considering, wheter there is a record or not.
I have written here the code which I have used.
Please go thru it and guide me where to change to succeed.
Thanking you in advance
GMohanraj

ResultSet rslt=stmt.executeQuery("SELECT * FROM user");
ResultSetMetaData rsmd=rslt.getMetaData();
int noCol=rsmd.getColumnCount();
String colName="";
int colSize=0;

System.out.println("Column Name | Column Size");

while (rslt.next())
{

for(int i=1; i<=noCol;++i)
{
colName=rsmd.getColumnName(i);
colSize=rsmd.getColumnDisplaySize(i);
System.out.println(colName+" | "+colSize);
}
System.out.println("----------------------------");
}
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
You should be able to get the column display sizes even without the resultset returning any records. I have a querry that retuns zero rows and I am still able to get the information.
int colCnt = yourResultSetMetaData.getColumnCount();
for(int column = 1; column <= colCnt; column++)
{
System.out.println(yourResultSetMetaData.getColumnDisplaySize()
}
Bosun
MohanRaj Gurubatham
Greenhorn

Joined: Sep 05, 2001
Posts: 16
I am realy sorry. Still I am not getting. Why don't you send me the entire code which you have used.
Sorry to trouble you.
With regards,
GMohanraj
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get field size of a column in a data base table