This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
How to get field size of a column in a data base table
MohanRaj Gurubatham
Greenhorn
Joined: Sep 05, 2001
Posts: 16
posted
0
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: 1506
posted
0
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
posted
0
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
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
posted
0
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;
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
posted
0
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