简体   繁体   English

我如何使用JTABLE将数据插入数据库

[英]How can i use a JTABLE to insert data into a database

I am making an application for inserting examination marks into a database. 我正在制作一个将检查标记插入数据库的应用程序。 I have managed to use a JTable to display data from the database but now i want to use the same JTable to insert data to the database. 我设法使用JTable来显示数据库中的数据,但是现在我想使用相同的JTable来将数据插入数据库中。 how i do that?I will appreciate any assistance given 我该怎么办?我将不胜感激

You could: 你可以:

1) Allow the user to select a row they want to edit. 1)允许用户选择他们要编辑的行。

2) Allow the user to click a button that opens a new window with textfields that are populated with the data from the selected row. 2)允许用户单击一个按钮,以打开一个新窗口,该窗口的文本字段填充了所选行中的数据。

3) Add a "save" button to this newly opened window which, when clicked, queries the database and "updates" where xyz = row that has been selected. 3)在此新打开的窗口中添加一个“保存”按钮,单击该按钮可查询数据库并“更新”,其中xyz =已选择的行。

How are you changing the TableModel from you JTable ? 您如何从JTable更改TableModel what structure are you using, if you are using an ArrayList to fill your Jtable you could use the same structure in order to edit your elements, just create a class that implement a TableModelListener , and override the tableChanged method in order to make the update 您使用的是什么结构,如果您使用ArrayList来填充Jtable,则可以使用相同的结构来编辑元素,只需创建实现TableModelListener的类,并覆盖tableChanged方法以进行更新即可。

Edit: ok so you have to get the model from your JTable, 编辑:好的,所以您必须从JTable中获取模型,

tbl_report.getModel().getValueAt(row, column); and this will give you the value from a specific cell, since you want all the JTable Values you could make this using a for (indeed two for's) something like this: 这将为您提供来自特定单元格的值,因为您需要所有JTable值,因此可以使用for(实际上是两个for)这样的值:

 ArrayList<Object> value= new  ArrayList<Object> ();
    for (int column = 0; column < resumenTable.getColumnCount(); column++) {
        for (int row = 0; row < resumenTable.getRowCount(); row++) {
         value.add(resumenTable.getModel().getValueAt(row, column));
        }
    }

You will have to cast the ArrayList elements in order to construct your query 您将必须ArrayList元素才能构建查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM