简体   繁体   English

在单个JTable单元中显示ResultSet中的多个值

[英]Displaying multiple values from a ResultSet in a single JTable cell

im currently stuck on my project wherein i need to generate a daily report. 我目前停留在我的项目中,我需要生成每日报告。 I need to display all the fault number that is involved in a call for the whole day. 我需要显示一整天电话中涉及的所有故障号。

Heres what i currently have 这是我目前拥有的

| Number of calls from ISG |  2  |    |
| Fault Numbers            |     |  x |

Here x should be those 2 fault numbers i need to display all the fault numbers from my mySQL database. 在这里x应该是那2个故障号,我需要显示我的mySQL数据库中的所有故障号。 for i need to display 2 fault numbers from ISG IN A SINGLE LINE on the 3rd column separated by comma. 因为我需要在第3列的单行中显示ISG的2个故障号,并以逗号分隔。 How do i fetch those 2 results because on my first try, only the first row was retrieved. 我如何获取这2个结果,因为在我的第一次尝试中,仅检索了第一行。

For the number of calls, here is my method where q is the query. 对于调用次数,这是我的方法,其中q是查询。

public void dailyResult(String q, int x, int y){

    try{
        Statement stmtDr = (Statement)daily.createStatement();
        ResultSet rs = stmtDr.executeQuery(q);
    if(rs.next()){
        reportTable1.setValueAt(rs.getInt(1), x, y);
    }
    }catch(Exception e){
                   JOptionPane.showMessageDialog(rootPane, "Error 106\n\nAn error has occured with the resultset procedure method. Please try again later.", "Error!", JOptionPane.ERROR_MESSAGE);
    }
}

With regards to displaying those numbers in a single jtable cell, i dont know how to do such task. 关于在单个jtable单元格中显示这些数字,我不知道该怎么做。 Im a beginner in mySQL queries and am very confused also with how the ResultSet class works, any help would be great, cheers! 我是mySQL查询的初学者,并且对ResultSet类的工作方式也感到非常困惑,任何帮助都将是很大的,加油!

You shouldn't be interacting with the table, but with the table's model. 您不应该与表进行交互,而应与表的模型进行交互。

If you're using a DefaulTableModel , you could take advantage of the addRow method, who would allow you to add a new row to the table (via the model), else you'll need to supply an implementation of the TablModel that has the appropriate methods to allow you to update the underlying data structure 如果您使用的是DefaulTableModel ,你可以利用的addRow方法,谁允许你在新行添加到表(通过模型),否则你会需要提供的实现TablModel具有允许您更新基础数据结构的适当方法

Take a close look at How to use tables for more details 详细了解如何使用表格

The ResultSet class basically has a concept of a "current" row, which allows you to extract individual values from the resulting columns (as specified by your original query). ResultSet类基本上具有“当前”行的概念,它使您可以从结果列中提取单个值(由原始查询指定)。 These can be extract either by order or by name, depending on your needs 这些可以根据您的需要按顺序或按名称提取

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM