结果集中不支持请求的操作
requested operation is not supported in result sets
我需要在 Jtable 中显示从 SQL SERVER 存储过程返回的数据,但我无法做到这一点,因为它给了我以下错误: forwarding-only 个结果集不支持请求的操作。
这是我实现的代码:
try
{
CallableStatement mostararPacientesAusentes = conexionBBDD.getConexionBBDD()
.prepareCall("{call mostararPacientesAusentes()}");
ResultSet tabla = mostararPacientesAusentes.executeQuery();
AbstractTableModel mTN=new ModeloTabla(tabla);
ventanaNotificacion.getTabla().setModel(mTN);
ventanaNotificacion.getTabla().validate();
} catch (SQLException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage(), "BBDD", 2, null);
}
这是继承AbstractTableModel
的class
public class ModeloTabla extends AbstractTableModel {
private ResultSet tabla;
private ResultSetMetaData datosBBDD;
public ModeloTabla(ResultSet unResulset)
{
tabla=unResulset;
try
{
datosBBDD=tabla.getMetaData();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
try
{
tabla.absolute(rowIndex+1);
return tabla.getObject(columnIndex+1);
}
catch (SQLException e)
{
e.printStackTrace();
return null;
}
}
public String getColumnName(int c)
{
try
{
return datosBBDD.getColumnName(c+1);
}
catch (SQLException e) {
e.printStackTrace();
return null;
}
}
@Override
public int getRowCount() {
try {
tabla.last();
return tabla.getRow();
}
catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
@Override
public int getColumnCount()
{
try
{
return datosBBDD.getColumnCount();
}
catch (SQLException e)
{
e.printStackTrace();
}
return 0;
}
}
table 的列标题显示在 JTable 中,但数据没有。
我已经搜索了解决问题的信息,但没有成功。
非常感谢。
Documentation,共 ResultSet
:
... A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row. It is possible to produce ResultSet objects that are scrollable and/or updatable. ...
有一个 prepareCall() 版本接受额外的参数来做到这一点,例如:
...prepareCall("{call mostararPacientesAusentes()}",
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
请检查 documentation 其他 constants/types。
我不确定使用 ResultSet
来维护数据是否是个好主意 - 它可能拥有大量资源(连接、语句...) - 恕我直言,最好阅读数据一次并将其保存在列表或类似结构中;尽快释放数据库资源。
我需要在 Jtable 中显示从 SQL SERVER 存储过程返回的数据,但我无法做到这一点,因为它给了我以下错误: forwarding-only 个结果集不支持请求的操作。
这是我实现的代码:
try
{
CallableStatement mostararPacientesAusentes = conexionBBDD.getConexionBBDD()
.prepareCall("{call mostararPacientesAusentes()}");
ResultSet tabla = mostararPacientesAusentes.executeQuery();
AbstractTableModel mTN=new ModeloTabla(tabla);
ventanaNotificacion.getTabla().setModel(mTN);
ventanaNotificacion.getTabla().validate();
} catch (SQLException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage(), "BBDD", 2, null);
}
这是继承AbstractTableModel
的classpublic class ModeloTabla extends AbstractTableModel {
private ResultSet tabla;
private ResultSetMetaData datosBBDD;
public ModeloTabla(ResultSet unResulset)
{
tabla=unResulset;
try
{
datosBBDD=tabla.getMetaData();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
try
{
tabla.absolute(rowIndex+1);
return tabla.getObject(columnIndex+1);
}
catch (SQLException e)
{
e.printStackTrace();
return null;
}
}
public String getColumnName(int c)
{
try
{
return datosBBDD.getColumnName(c+1);
}
catch (SQLException e) {
e.printStackTrace();
return null;
}
}
@Override
public int getRowCount() {
try {
tabla.last();
return tabla.getRow();
}
catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
@Override
public int getColumnCount()
{
try
{
return datosBBDD.getColumnCount();
}
catch (SQLException e)
{
e.printStackTrace();
}
return 0;
}
}
table 的列标题显示在 JTable 中,但数据没有。 我已经搜索了解决问题的信息,但没有成功。 非常感谢。
Documentation,共 ResultSet
:
... A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row. It is possible to produce ResultSet objects that are scrollable and/or updatable. ...
有一个 prepareCall() 版本接受额外的参数来做到这一点,例如:
...prepareCall("{call mostararPacientesAusentes()}",
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
请检查 documentation 其他 constants/types。
我不确定使用 ResultSet
来维护数据是否是个好主意 - 它可能拥有大量资源(连接、语句...) - 恕我直言,最好阅读数据一次并将其保存在列表或类似结构中;尽快释放数据库资源。