如何编辑 JTable 行?
我想要一个教程或方法,以便我可以突出显示 Jtable 中的一行,该行从 mysql db 获取其数据,然后单击编辑按钮,以便它启动一个表单,我可以使用该表单来编辑该行,然后保存。 我使用了一种有效的方法,但它引发了很多异常,所以我认为这是一个糟糕的设计。
编辑:我还不想使用绑定。我想编写我能理解的基本代码。
编辑2:这是我调用的方法来获取在编辑查询中使用的密钥..我突出显示该行并调用此函数:
int id = ((Number) model.getValueAt(jTable1.getSelectedRow(), 0)).intValue() ;
函数体不是我编写的它是 ResultSetTableModel
文件
public Object getValueAt( int row, int column )
throws IllegalStateException
{
// ensure database connection is available
if ( !dbConnection.isConnectedToDatabase() )
throw new IllegalStateException( "Not Connected to Database" );
// 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
结果 集的一部分这里抛出了关闭的异常,我知道原因是因为我之前使用了相同的结果集来填充表格。所以我想要一个不同的选择。
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" );
// 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅如何使用表。但如果你能展示你的代码,那将会很有帮助。
如果您的问题是您的应用程序抛出
SQLException
,那么您需要显示您的 SQL 查询和该表的数据库架构。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.请参阅数据库中的表。 “数据库中的表示例”展示了如何从 ResultSet 中的数据创建 DefaultTableModel,因此您不必担心这一点。
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.