'getRow()' 方法只允许在滚动游标上使用 SQLException 错误
The 'getRow()' method is only allowed on scroll cursors SQLException error
我正在尝试从我在 Netbeans 中设置的嵌入式数据库填充我的 JTable。
我的数据库包含 3 行和 3 列,我想将其插入到 JTable 中,并使 table 在我的 GUI 应用程序中可见。
但是,如上标题所述,我收到 java.sql.SQLException 错误,并且我的 table 在我的 GUI 应用程序中不可见。
这是我的代码:
public void FillTable(JTable table, String Query)
{
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:derby:STOCK_CONTROL");
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(Query);
//Remove previously added rows
while (table.getRowCount() > 0)
{
((DefaultTableModel) table.getModel()).removeRow(0);
}
int columns = rs.getMetaData().getColumnCount();
while (rs.next())
{
Object[] row = new Object[columns];
for (int i = 1; i <= columns; i++)
{
row[i - 1] = rs.getObject(i);
}
//The error is being generated here at 'rs.getRow()'
((DefaultTableModel) table.getModel()).insertRow(rs.getRow() - 1, row);
}
rs.close();
stat.close();
conn.close();
}
catch (InstantiationException |
IllegalAccessException |
ClassNotFoundException |
SQLException e)
{
System.out.println(e);
e.printStackTrace();
}
}
然后我创建我的 table 并调用上面的方法:
JTable tigerTable = new JTable();
FillTable(tigerTable, "SELECT * FROM TIGER_INFO");
我试图找到有关导致此错误的原因的信息,但无济于事。
我的问题是,如何正确地从我的嵌入式数据库填充我的 JTable,同时又能避免我遇到的这个未知错误?
这个错误到底是什么意思?我不确定 'scroll cursor' 是什么。
如 ResultSet.getRow()
中所述:
Note: Support for the getRow
method is optional for ResultSet
s with a result set type of TYPE_FORWARD_ONLY
[..]
Throws:
[..]
SQLFeatureNotSupportedException
- if the JDBC driver does not support this method
您需要使用
请求可滚动光标
conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
或者 - 可能更好 - 自己跟踪行号而不是使用 ResultSet.getRow()
。例如:
int rowIdx = 0;
while (rs.next()) {
Object[] row = new Object[columns];
// ...
((DefaultTableModel) table.getModel()).insertRow(rowIdx++, row);
}
我已经有一段时间没有用 swing 做过任何事情了,但是从 table 模型中删除现有行并调用 addRow
.
可能会更简单
我正在尝试从我在 Netbeans 中设置的嵌入式数据库填充我的 JTable。
我的数据库包含 3 行和 3 列,我想将其插入到 JTable 中,并使 table 在我的 GUI 应用程序中可见。
但是,如上标题所述,我收到 java.sql.SQLException 错误,并且我的 table 在我的 GUI 应用程序中不可见。
这是我的代码:
public void FillTable(JTable table, String Query)
{
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:derby:STOCK_CONTROL");
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(Query);
//Remove previously added rows
while (table.getRowCount() > 0)
{
((DefaultTableModel) table.getModel()).removeRow(0);
}
int columns = rs.getMetaData().getColumnCount();
while (rs.next())
{
Object[] row = new Object[columns];
for (int i = 1; i <= columns; i++)
{
row[i - 1] = rs.getObject(i);
}
//The error is being generated here at 'rs.getRow()'
((DefaultTableModel) table.getModel()).insertRow(rs.getRow() - 1, row);
}
rs.close();
stat.close();
conn.close();
}
catch (InstantiationException |
IllegalAccessException |
ClassNotFoundException |
SQLException e)
{
System.out.println(e);
e.printStackTrace();
}
}
然后我创建我的 table 并调用上面的方法:
JTable tigerTable = new JTable();
FillTable(tigerTable, "SELECT * FROM TIGER_INFO");
我试图找到有关导致此错误的原因的信息,但无济于事。
我的问题是,如何正确地从我的嵌入式数据库填充我的 JTable,同时又能避免我遇到的这个未知错误?
这个错误到底是什么意思?我不确定 'scroll cursor' 是什么。
如 ResultSet.getRow()
中所述:
Note: Support for the
getRow
method is optional forResultSet
s with a result set type ofTYPE_FORWARD_ONLY
[..]Throws:
[..]
SQLFeatureNotSupportedException
- if the JDBC driver does not support this method
您需要使用
请求可滚动光标conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
或者 - 可能更好 - 自己跟踪行号而不是使用 ResultSet.getRow()
。例如:
int rowIdx = 0;
while (rs.next()) {
Object[] row = new Object[columns];
// ...
((DefaultTableModel) table.getModel()).insertRow(rowIdx++, row);
}
我已经有一段时间没有用 swing 做过任何事情了,但是从 table 模型中删除现有行并调用 addRow
.