JAVA更新数据库上的JTable单元格更改 [英] JAVA update database on JTable cell change

查看:210
本文介绍了JAVA更新数据库上的JTable单元格更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他对Java不熟悉(那不是新手),为此我找不到很好的教程.我使用jTable来显示一个表,其中填充了MySQL数据库中的数据.

He I am noob to Java (Thats not new) and I can't find a good tutorial for this. I use jTable to display a table filled with data from a MySQL database.

到目前为止,我得到了桌子:

So far so good, I get the table:

标准,您可以单击一个表格单元格,表格单元格将变为一个文本字段,其中可以填充新内容.你们都知道,但是如何使用它来更新数据库中的值?

Standard you can click a table cell and it changes to a text-field with can be filled with something new. You all know that, but how can I use this to also update the value in my database?

我的代码:

import [...];
public class table extends JPanel {
    public String table;
    private Database db;

    public table(String tablename, Database db){

        try {
            table = tablename;
            this.db = db;

            //Get table with and height
            ResultSet res = db.query("SELECT COUNT( * ) FROM `"+table+"`");
            ResultSet res2 = db.query("SELECT COUNT( * ) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '"+table+"'");

            int rows = 0;
            int collums = 0;

            res.next();
            res2.next();

            rows = res.getInt(1);
            collums = res2.getInt(1);

            //Get table column names and set then in array
            ResultSet clom = db.query("DESCRIBE  `"+table+"`");

            String[] columnNames = new String[collums];

            int s = 0;

            while(clom.next()){
                columnNames[s] = clom.getString(1);
                s++;
            }

            //get table data and put in array
            Object[][] data = new Object[rows][collums];

            ResultSet result = db.query("SELECT * FROM `"+table+"`");

            int q = 0;

            while(result.next()){
                for(int a=0; a<= (collums - 1); a++){
                    data[q][a] = result.getString(a + 1);
                    //System.out.println(q + " - " + a);
                }
                q++;
            }

            //Make Jtable of the db result form the two array's
            final JTable table = new JTable(data, columnNames);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            table.setFillsViewportHeight(true);

            // do some event listening for cell change

            JScrollPane scrollPane = new JScrollPane(table);
            JFrame frame = new JFrame("table editor");
            scrollPane.setOpaque(true);
            frame.setContentPane(scrollPane);
            frame.pack();
            frame.setSize(600, 800);
            frame.setVisible(true);
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
    }
}

我想我需要绑定某种表侦听器,并且当发生某些更改时,我将获取表的所有值并通过查询对其进行更新.

I guess I need to bind some kind of table listener and when something changes I take all the values of the table and update them with a query.

我该怎么做?

这是我的伪代码:

table.bindCellEventListner(callback(t){
     Array row = t.getAllValuesAsArrayOfRow();

     String data = "";

     int f = 0
     while(row.next()){
         data .= "`"+clom[f]+"` = '"+row[f]+"',"
         f++;
     }
     data.delLastChar();
     db.query("UPDATE `"+table+"` SET "+data+" WHERE `id` ="+row[0]+";");

});

推荐答案

删除的答案answer窃了14年前Rachel Swailes的答案,发现

A deleted answer plagiarised Rachel Swailes' answer from 14 years ago, found here. For completeness and because the answer is correct and useful, I have quoted the relevant text here:

第1步:如果让您的表从现在开始扩展TableModel(如果还没有的话),那么整个过程将变得更加容易.因此,如果您需要帮助,就问一下.

Step 1: It's going to be way easier for the whole thing if you make your table extend a TableModel from now (if you haven't already). So if you need help with that just ask.

第2步:在表格模型中,您需要启用单元格的可编辑性.在您创建的TableModel类中,您需要添加这些方法

Step 2: In the table model you need to enable the cells to be editable. In the TableModel class that you make, you need to add these methods

public boolean isCellEditable(int row, int col) { 
  return true; 
}

public void setValueAt(Object value, int row, int col) {
  rowData[row][col] = value;
  fireTableCellUpdated(row, col);
}

第3步:您将在第二个方法中看到我们触发了一个名为fireTableCellUpdated的方法.因此,在这里我们可以了解其用途的变化.您需要在表中添加一个TableModelListener来捕获它.

Step 3: You will see in the second method that we fire a method called fireTableCellUpdated. So here we can catch what the use it changing. You need to add a TableModelListener to your table to catch this.

mytable.getModel().addTableModelListener(yourClass);

在您决定要实现TableModelListener的类中,您需要此

And in the class that you decide will implement the TableModelListener you need this

public void tableChanged(TableModelEvent e) {
  int row = e.getFirstRow();
  int column = e.getColumn();
  TableModel model = (TableModel)e.getSource();
  Object data = model.getValueAt(row, column);
  ...
}

现在您在单元格中有数据,并且在

now you have the data in the cell and the place in the grid where the

单元格是您可以根据需要使用数据

cell is so you can use the data as you want

这篇关于JAVA更新数据库上的JTable单元格更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆