如何从按钮事件中的数据库填充JTable [英] How to populate JTable from database on button event

查看:230
本文介绍了如何从按钮事件中的数据库填充JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,允许用户放入选择类型查询,当他们单击一个按钮时,select语句的结果将显示在JTable中。我没有得到任何错误,但是当我的按钮按下时,文本面板中没有显示任何内容。我有以下:

I have a text box that allows users to put in select type queries with the idea that when they click a button the result of the select statement will be shown in a JTable. I don't get any errors but also nothing is shown in the textPane when my button is pressed. The I have is below:

public class Console {

    String myquery="";

    private JFrame frame;
    private JTextField textField;


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Console window = new Console();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public Console() {
        initialize();
    }


    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 950, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextArea textAreaQuery = new JTextArea();
            JTable table_ResQues = new JTable();
            JButton btnNewButton = new JButton("Execute");
            btnNewButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e)
                {
                    String connectDB = "jdbc:ucanaccess:///Users/sebastianzeki/Documents/PhysJava/Physiology.mdb;";
                    System.out.println("Connection To Database Made");
                    Connection conn = null;

                        try {
                            conn = DriverManager.getConnection(connectDB);
                        } catch (SQLException e1) {
                            e1.printStackTrace();
                        }
                        Statement st = null;
                        try {
                            st = conn.createStatement();
                        } catch (SQLException e1) {
                            e1.printStackTrace();
                        }
                         myquery=textAreaQuery.getText();
                        String stg2 = "Select "+myquery;
                        ResultSet rs = null;
                        try {
                            rs = st.executeQuery(stg2);
                            table_ResQues.setModel(getDataFromDatabase);
                        } catch (SQLException e1) {
                            e1.printStackTrace();
                        }
                }
}

模型:

  public TableModel getDataFromDatabase()
{
    DefaultTableModel model = new DefaultTableModel(5, 5);
    model.setValueAt("Hard", 0, 0);
    model.setValueAt("Coded", 1, 1);
    model.setValueAt("Data", 2, 2);

    return model;
}
    }


推荐答案


我删除了本地变量

Ive removed the local variable

否,您删除了实例变量。你真的用你的真正的代码尝试这个或只是编辑问题?

No, you removed the instance variable. Did you actually try this with your real code or just edit the question?


我不知道如何为这个问题提供一个可测试的数据库

I have no idea how to provide a testable database for this question

我已建议您创建一个简化逻辑的方法。 Sometnerg like:

I already suggested you create a method to simplify the logic. Somethng like:

public TableModel getDataFromDatabase()
{
    DefaultTableModel model = new DefaultTableModel(5, 5);
    model.setValueAt("Hard", 0, 0);
    model.setValueAt("Coded", 1, 1);
    model.setValueAt("Data", 2, 2);

    return model;
}

然后在你的ActionListener中,你可以这样做:

Then in your ActionListener you simple do something like:

table_ResQues.setModel( getDataFromDataBase() );

一旦你得到这个基本逻辑,你将SQL逻辑移动到 getDataFromDatabase ()方法。

Once you get this basic logic working you move the SQL logic into the getDataFromDatabase() method.

现在你创建你的SSCCE,显示你如何实际创建框架并将组件添加到框架。代码应该是可编译和可执行的。

So now you create your SSCCE showing how you actually create the frame and add the components to the frame. The code should be compilable and executable.

编辑:

你被告知要显示一个表是滚动条。这是没有额外的努力。而不是使用:

You have been told to display a table is a scrollpane. It is no extra effort to do this. Instead of using:

panel.add(table);

您使用:

panel.add( new JScrollPane( table ) );

我也建议测试你的布局,你可以使用如下代码来显示一个虚拟表:

I would also suggest that to test your layout you can use code like the following to display a dummy table:

//JTable table_ResQues = new JTable();
JTable table_ResQues = new JTable(5,5);

然后当使用setModel()方法时,只有数据受影响,表。

Then when you use the setModel() method, only the data is affected, not the layout of the table.


我无法帮助,但是觉得这是一个fireTableDataChanged问题。

I cant help but feel this is a fireTableDataChanged problem though.

我怀疑。当您更改模型中的数据时,该方法由TableModel调用。在这种情况下不使用,因为您使用 setModel(...)方法。这将导致表自动重绘()。

I doubt it. That method is invoked by the TableModel when you change the data in the model. It is not used in this case because you are using the setModel(...) method. This will cause the table to repaint() itself automatically.

这篇关于如何从按钮事件中的数据库填充JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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