How to edit a JTable row?
I want a tutorial or a method so that I can highlight a row in a Jtable which grabs its data from a mysql db , then click on edit button so it launches a form that I can use to edit the row and then save. I used a method that worked but it threw a lot of exceptions so I think it's a bad design.
Edit : I don't want to use binding yet . I want to write basic code that I can understand.
Edit 2 : Here's what I call to get the key I use in editing query .. I highlight the row and call this function :
int id = ((Number) model.getValueAt(jTable1.getSelectedRow(), 0)).intValue() ;
the function body wasn't written by me It's part of ResultSetTableModel
file
public Object getValueAt( int row, int column )
throws IllegalStateException
{
// ensure database connection is available
if ( !dbConnection.isConnectedToDatabase() )
throw new IllegalStateException( "Not Connected to Database" );
/开发者_如何学Go/ obtain a value at specified ResultSet row and column
try
{
getResultSet().absolute( row + 1 );
return getResultSet().getObject( column + 1 );
} // end try
catch ( SQLException sqlException )
{
System.out.println("Exception from here dude");
sqlException.printStackTrace();
} // end catch
return ""; // if problems, return empty string object
} // end method getValueAt
A resultset closed exception is thrown here and I know the reason is because I used the same resultset before to fill the table . So I want a different alternative.
See How to Use Tables. But it would be helpful if you could show your code.
If your problem is that your application throws a SQLException
then you need to show your SQL-query and your database schema for that table.
A resultset closed exception is thrown here and I know the reason is because I used the same resultset before to fill the table . So I want a different alternative.
See Table From Database. The "Table From Database Example" shows how you can create a DefaultTableModel from data in a ResultSet so you don't have to worry about this.
精彩评论