aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable-Getting Data out to db Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable-Getting Data out to db" Watch "JTable-Getting Data out to db" New topic
Author

JTable-Getting Data out to db

Stuart Hoffman
Greenhorn

Joined: Feb 13, 2004
Posts: 8
Having great difficulty getting the data out of my JTable once it is populated and then the data is changed by the user. I extended DefaultTableModel for my tableModel then returned a ResultSet via SQLJ to populate the table. The data displays when I run the Applet and I have overridden isCellEditable making cells 1 to 8 editable. Structure of the table is as follows:
ID, Project, hrsMon hrsSun,total hrs
int, String, float, float, float, float, float, float, float, float
When I call getDataVector and try to display the data (for debug, what I really need to do is post changes to the db: oracle by the way) I get back null.
Code Snippet for accessing getDataVector:
private void saveData()
{
Vector newVector;
String newString[];// = new String[10];
String stupidString;
try
{
newVector = aModel.getDataVector();
newVector.trimToSize();
newString = new String[newVector.size()];
//just testing to see if I can show one element from the Vector. What
//I'll really do is pass the Vector to a SQLJ servlet then parse the data
//out and insert into the db.
stupidString = newString[1];
//newString[1] = (String)newVector.elementAt(2);
//newString = newVector.toString();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
newString = new String[10];
stupidString ="err";
}
JOptionPane.showMessageDialog(null,"Record = "+stupidString);
//return newString;
}
As you can see I tried a couple different ways of getting the data out of the Vector but have met with defeat.
Tried the tutorial using tables and this part seems to be assumed that we'd know how to do it.
Any help will be greatly appreciated.
Thanks
Stuart
Peyton McCullough
Ranch Hand

Joined: Feb 07, 2004
Posts: 31
Are you aware that 'newVector.elementAt(2)' will return a vector conisting of more vectors? It will return all of row two. If you wanted to get, say, row two's column one, you'd use:

[ February 15, 2004: Message edited by: Peyton McCullough ]
Stuart Hoffman
Greenhorn

Joined: Feb 13, 2004
Posts: 8
No I was not aware, thanks.
I changed that line as follows:
try
{
newVector = bottomPanel.getAModel().getDataVector();
newVector.trimToSize();
newString = new String[newVector.size()];
stupidString = newString[1];
//newString[1] = (String)newVector.elementAt(2);
//NEW line below
newString[1] = (String)(((Vector)newVector.elementAt(1)).elementAt(0));
//newString = newVector.toString();
}
and I get the following error:
--
java.lang.ClassCastException: java.math.BigDecimal
void SwingTimeSheet.saveData()
SwingTimeSheet.java:193
--
line 193 is the change in the code. The column
I'm getting back would be an int so what did I do wrong?
Peyton McCullough
Ranch Hand

Joined: Feb 07, 2004
Posts: 31
I have no idea.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

What are the datatypes in the columns of your table? This may shed some light on the problem...


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Stuart Hoffman
Greenhorn

Joined: Feb 13, 2004
Posts: 8
The column types are
Integer, Varchar2(30),float,float,float,float,float,float,float,float
0 1 2 3 4 5 6 7 8 9
Cols 0 and 9 are not editable.
cols 2-8 will be summed into 9.
Thanks
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

I saw in this other post where you were using BigDecimal for the sum column... if you have changed this to Float, doublecheck and make sure you have changed this in all the places it could possibly be... this is the only place I could think you might be getting your ClassCastException from.
Stuart Hoffman
Greenhorn

Joined: Feb 13, 2004
Posts: 8
Yes,
That was only my thrashing , trying several different datatypes in order to see what worked. Notice that long sum calculation was commented out?
That was the only way I could get it to compile.
Any ideas?
Can't can it and start over though
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable-Getting Data out to db