• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the ResultSetMetadata interface, there is a method called getColumnDisplaySize(). This could be DB Driver dependent. Check the API for more info.

Bosun
 
MohanRaj Gurubatham
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic