JTable用JComboBox填充数据 [英] JTable fill data with JComboBox

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

问题描述

我有一个带有表,组合框的框架,我想通过组合框用数据库中的数据填充表,但是如果我与itemlistener一起使用,我看不到该表,没有itemlistener和String sql="select * from Arlista",那么我看到的表中有数据 (combob = combobox)

I have a frame with table,combobox, i want to fill the table with data from database by combobox, but if i use with itemlistener i dont see the table, without itemlistener and String sql="select * from Arlista" then i see my table with data (combob=combobox)

combob.addItemListener(new ItemListener(){
    @Override
    public void itemStateChanged(ItemEvent e){
        tesztvalt=(combob.getSelectedItem()).toString();

        if (e.getItem().equals(tesztvalt))
        {
            try {

                Class.forName( driver );
                Connection connection = DriverManager.getConnection( url );
                String sql="select * from "+tesztvalt+"";
                Statement stmt = connection.createStatement();
                ResultSet rs = stmt.executeQuery( sql );
                ResultSetMetaData md = rs.getMetaData();
                int columns = md.getColumnCount();

                while (rs.next()) {
                    Vector row = new Vector(columns);

                    for (int i = 1; i <= columns; i++)
                    {
                        row.addElement( rs.getObject(i) );
                    }
                    data.addElement( row );

                }
                rs.close();
                stmt.close();
                connection.close();
            }catch(Exception ex) {
                System.out.println( e );
            }
            JTable table = new JTable(data, columnNames)
            {
                public Class getColumnClass(int column)
                {
                    for (int row = 0; row < getRowCount(); row++)
                    {
                        Object o = getValueAt(row, column);

                        if (o != null)
                        {
                            return o.getClass();
                        }
                    }
                    return Object.class;
                }
            };
            JScrollPane scrollPane = new JScrollPane( table );
            getContentPane().add( scrollPane );

            JPanel buttonPanel = new JPanel();
            getContentPane().add( buttonPanel, BorderLayout.SOUTH );
        }
    }
});

推荐答案

String sql="SELECT * FROM "+tesztvalt+""; 

PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rs = pst.executeQuery();

while (rs.next()) 
{
    tabla.setModel(model);
}

您有几个问题:

  1. 您错误地使用了PreparedStatement.您的代码可能可以运行(我不确定),但是它没有利用PreparedStatement的功能.

  1. You are using the PreparedStatement incorrectly. Your code may work (I'm not sure), but it does not take advantage of the features of the PreparedStatement.

从ResultSet读取数据的代码没有意义,因为您甚至都没有从ResultSet读取任何数据.

The code that read the data from the ResultSet makes no sense because, well you aren't even reading any data from the ResultSet.

要使用PreparedStatement,代码类似于:

To use a PreparedStatement the code is something like:

String sql = "Select * from ?";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString( 1, tesztvalt );
stmt.executeQuery();

现在,PreparedStatement将使用适当的定界符来构建SQL查询,因此您不必为此担心.

Now the PreparedStatement will build the SQL query with the proper delimiters so you don't need to worry about that.

如果要读取特定表中的所有数据,请查看TableFromDatabaseExample.java代码-database/"rel =" nofollow>数据库中的表.它显示了如何构建查询以及如何访问列名和数据.

If you want to read all the data from a specific table, then check out the TableFromDatabaseExample.java code found in Table From Database. It shows how to build the query as well as how to access the column names and data.

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

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