Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

exception for this code !! help me  RSS feed

 
madhu cm
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
madhu cm
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!