Forums Register Login

exception for this code !! help me

+Pie Number of slices to send: Send
Hello
I need to retrive all the colums and rows in a Database to JTable......
But the problem here i am facing is i will get exception as "java.lang.ClassCastException:java.lang.String".

ResultSet rs = stat.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
columns= new Vector();
for(int i=1;i<=rsmd.getColumnCount();i++)
columns.addElement(rsmd.getColumnName(i));

rows = new Vector();
while(rs.next()){
Vector currow=new Vector();//Current Row
for(int i=1;i<=rsmd.getColumnCount();i++)
currow.addElement(rs.getString(i));
rows.addElement(currow);
}

JTable table = new JTable(columns,rows);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(table,v,h);


The rsmd.getColumnName returns as String aswell as rs.getString . but JTable constructor accepts only objects as parameters... so i am using Vector to get columns and rows.... "i cant cast String to Object " . so please help what i should do? is there a better solution for this......

thank you.
+Pie Number of slices to send: Send
Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. A single name isn't enough. You can change your display name here. Thanks!

It's not the column names, but the row data that's wrong. The second argument to that constructor is supposed to be a Vector of Vectors, but you're passing in a Vector of Strings. You need to build a Vector for each row, and put those Vectors into "rows".
+Pie Number of slices to send: Send
madhu, I informed you about the JavaRanch naming policy in this post on 29 June. Please go to your profile page and change your displayed name to be a first name and last name separated by a space and not obviously fake. Failure to do so will result in your account being locked.
+Pie Number of slices to send: Send
As a matter of fact my name is MADHU , yes just real name MADHU... is there any problem in using real names here?? i dont think so....
+Pie Number of slices to send: Send
"Madhu" is perfectly fine. But unless you're a famous singer or athlete, you've most likely got two names, a given name and a family name. You're required to use both. A single name isn't enough.

In any event, you seem to have changed your name, and although that second name still isn't a real word, if that's what you want to use, I'm going to let it go -- but I'd still prefer you to change it to a real last name.
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 724 times.
Similar Threads
My JTable is a bit confused
JTable type yes/no
JTable & JDBC
JTable refreshing problem
JTable using Vector
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:33:51.