使用除jtable之外的组件检索数据库值? [英] Retrieval of database values using component other than jtable?

查看:105
本文介绍了使用除jtable之外的组件检索数据库值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使用jTable以外的组件检索数据库行,其中每行的唯一jButtons可以添加并执行特定任务?

is there any way to retrieve database rows using component other than jTable where unique jButtons for each row can added and made to perform specific task?

使用以下代码... jTable出现在对话框中

Currently I'm using the following code... jTable appears in a dialog box

public static DefaultTableModel buildTableModel(ResultSet rs)
            throws SQLException {
        ResultSetMetaData metaData = rs.getMetaData();
        // names of columns
        Vector<String> columnNames = new Vector<String>();
        int columnCount = metaData.getColumnCount();
        System.out.println("7");
        for (int column = 1; column <= columnCount; column++) {
            columnNames.add(metaData.getColumnName(column));
        }
        // data of the table
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while (rs.next()) {
            Vector<Object> vector = new Vector<Object>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(rs.getObject(columnIndex));
            }
            data.add(vector);
        }
        return new DefaultTableModel(data, columnNames);
    }

    public void searchb2() throws SQLException {
        this.be_cgpa = be_cg.getText();
        this.maj_proj = Major.getText();
        this.h_percent = hss_percent.getText();
        this.s_percent = sss_percent1.getText();
        preparedStatement = con.prepareStatement("select name,age,gender,email_id,phone_num,state from resume1 where qualification='be' and be_cgpa>='" + be_cgpa + "'" + "and maj_proj_tech='" + maj_proj + "'" + "and hss_percent>='" + h_percent + "'" + "and sss_percent='" + s_percent + "'");
        ResultSet rs;
        rs = preparedStatement.executeQuery();
        JTable table = new JTable(buildTableModel(rs));
        JOptionPane.showMessageDialog(null, new JScrollPane(table));
    }

是否可以修改此代码以在每行中添加jButton?

Can this code be modified to add jButton in each row?

推荐答案

没有理由不能使用JTable并向表中添加包含按钮的列。

There is no reason you can't use JTable and add a column containing buttons to the table.

请参见表按钮列方法来做到这一点。此类希望您提供在单击按钮时调用的 Action 。所有你需要做的是在完成循环列数据后,向向量中添加另一个字符串的文本。

See Table Button Column for one way to do this. This class expects you to provide an Action that is invoked when the button is clicked. All you need to do is add another String of text to the "vector" after you have finished looping through the column data.

此外,使用 PreparedStatement 为您的SQL。它比你现在的代码更容易编码和理解,更不容易出错。

Also, use a PreparedStatement for your SQL. It is easier to code and understand and less error prone than your current code.

这篇关于使用除jtable之外的组件检索数据库值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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