JTable不更新 [英] JTable not updating

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

问题描述

我正在尝试创建表并通过使用Java swing中的表将值添加到sql.但是在for循环中,我根本不增加.我认为这是因为while(rs2.next()).当我将它放置在for循环之外时,出现以下错误:在开始结果集之前".我需要帮助!

I'm trying to create a table and add values to sql by using table in java swing. But in for loop i doesn't incrementing at all. I think its because of while(rs2.next()).When i place it out of for loop i get this error: "Before start of result set". I need help!

以上问题已解决,但我现在有新问题.看来JTable不会自我更新.当我在组合框表中选择表名时,应用程序崩溃,并给数组索引超出范围.

我的代码在这里:

public class DBC extends JFrame{

static String tablo;
static JTextField tf = new JTextField(20);
static int columnCount;
static JPanel tfPanel = new JPanel();
static String[] sutunlar;
static JLabel sutunLabel;
static JPanel sutunPanel = new JPanel(new BorderLayout());
static JTable table;
static JScrollPane scrollPane;

public static void main(String[] args) throws Exception {

    Class.forName("com.mysql.jdbc.Driver");
    Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/project"
              ,"root","123456789");

    final Statement statement = connect.createStatement();

    JLabel tabloSec = new JLabel("Tablo Seçin:");
    final JComboBox<String> tablolar = new JComboBox<String>();
    final DatabaseMetaData md = connect.getMetaData();
    final ResultSet rs = md.getTables(null, null, "%", null);

    while (rs.next()) {
        tablolar.addItem(rs.getString(3));
    }

    tablolar.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {

            tablo = tablolar.getSelectedItem().toString();

            try {

                 ResultSet rs2 = statement.executeQuery("SELECT * FROM "+tablo);
                 ResultSetMetaData rsmd = rs2.getMetaData();
                 columnCount = rsmd.getColumnCount();

                 sutunlar = new String[columnCount];
                 table = new JTable(1,columnCount);
                 Object columnNames[] = new Object[columnCount];
                 Object rowData[][] = {{""}};

                     for(int i=0;i<columnCount;i++){

                         sutunlar[i] = rsmd.getColumnLabel(i+1);
                         columnNames[i] = sutunlar[i];

                         }

                     table = new JTable(rowData, columnNames);
                     table.repaint();
                     scrollPane = new JScrollPane(table);
                     sutunPanel.add(scrollPane);
                     sutunPanel.revalidate();

            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

    JButton ekle = new JButton("Ekle");
    ekle.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {

            try {
                switch(tablo){
                case "department":

                    statement.executeUpdate("INSERT INTO department(Name,Location) VALUES('"+tf.getText()+"')");
                case "employee":

                    statement.executeUpdate("INSERT INTO employee(Id,FirstName,LastName,Sex,Address,Email,Salary,BirthDate,JoinDate) VALUES('"+tf.getText()+"')");
                case "engineer":

                    statement.executeUpdate("INSERT INTO engineer(EngineerType) VALUES('"+tf.getText()+"')");
                case "manager":

                    statement.executeUpdate("INSERT INTO manager(Department) VALUES('"+tf.getText()+"')");
                case "project":

                    statement.executeUpdate("INSERT INTO project(Name,Number,Value) VALUES('"+tf.getText()+"')");
                case "secretary":

                    statement.executeUpdate("INSERT INTO secretary(TypingSpeed) VALUES('"+tf.getText()+"')");
                }

            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });

    JButton cik = new JButton("Çık");
    cik.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            System.exit(0);
        }
    });

    JPanel panel = new JPanel(new FlowLayout());

    panel.add(tabloSec);
    panel.add(tablolar);
    panel.add(sutunPanel);
    panel.revalidate();
    panel.add(ekle);
    panel.add(cik);

    JFrame frame = new JFrame("Deneme");
    frame.setSize(600,600);
    frame.setLocationRelativeTo(null);
    frame.add(panel);
    frame.setVisible(true);
  }
}

推荐答案

我认为您混淆了一些东西.定义时

I think you mixed something up. When you define

columnCount = rsmd.getColumnCount();

似乎您希望columnCount保存该表的列数;但这种情况并非如此.这是元数据提供的列数.

It seems you expect that columnCount holds the number of columns of the table; but this is not the case. This is the number of columns the metadata provides.

如果要获取所选表的列的名称,请将其放在您的try/catch块中:

If you want to fetch the names of the columns of the selected table, put this in your try/catch block:

ResultSet rs2 = md.getColumns(null, null, tablo, null);

Set<String> columnNames = new HashSet<String>();
while (rs2.next()) {
  columnNames.add(rs2.getString(4));
}

Object[][] rowData = { null };
table = new JTable({ null }, columnNames.toArray());
scrollPane = new JScrollPane(table);
sutunPanel.add(scrollPane);

您应该真正考虑对静态字段的使用;)

And you should really think about your usage of static fields ;)

这篇关于JTable不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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