Jatin Bansal
Jatin Bansal

Reputation: 181

how to use setValueAt() in MyTableModel if i passed a resultset to the tablemodel

I have a MyTableModel in which i passed a resultset as an argument to the constructor.

import javax.swing.table.*;
import java.sql.*;
class MyTableModel extends AbstractTableModel {
ResultSet rs;
ResultSetMetaData rsmd;
MyTableModel(ResultSet rs){
    try{
        this.rs=rs;
        rsmd=rs.getMetaData();
    }
    catch(Exception e) {
        System.out.println(e);
    }
}
public int getColumnCount(){
    try{
        return rsmd.getColumnCount();
    }
    catch(Exception e){
        return -1;
    }
}
public String getColumnName(int i){
    try{
        return rsmd.getColumnName(i+1);
    }
    catch(Exception e){
        return null;
    }
}
public int getRowCount(){
    try{
        rs.last();
        return rs.getRow();
    }
    catch(Exception e){
        return -1;
    }
}
public Object getValueAt(int i,int j){
    try{
        rs.absolute(i+1);
        return rs.getObject(j+1);
    }
    catch(Exception e){
        return null;
    }
}

public boolean isCellEditable(int i,int j){
    return true;
}

public void setValueAt(Object value, int i, int j){
}

}

now how can i use setValueAt() method in order to set the specific cell. After set the value of cell i want to update this data in the database please tell me how can i update this data.

Upvotes: 0

Views: 75

Answers (1)

ControlAltDel
ControlAltDel

Reputation: 35096

You shouldn't use a ResultSet as a backstore for a TableModel, as the ResultSet needs to be closed to free up the Database / connection resources.

Rather, you should copy the content of each element in the ResultSet to a String[], and add each row to a List. Then use that List as the parameter for your TableModel.

Upvotes: 1

Related Questions