如何将 ResultSet 响应传递给 JOptionPane showMessageDialog

How to pass a ResultSet response to a JOptionPane showMessageDialog

Oracle "How to Make a Dialog" tutorial 没有帮助我,我也没有看到任何其他帖子,因为它们没有解决传递值的问题 一个showMessageDialog..

我有一个 Java Swing 应用程序,如下所示:

用户在三个字段中的每一个字段中输入信息,当用户按下按钮 "Fetch Tweets"。

时,这些字段将合并到 MySQL 查询中

按下按钮后,我想 return 将行计数添加到 showMessageDialog 中,让用户可以选择是否继续。

问题: 我不知道如何将 ResultSet 值传递给 showMessageDialog。我需要在 JOptionPane 上设置一个 Action Listener 吗?如果是这样,您如何管理时间?

我的 showMessageDialog 如下所示。如您所见,“0”的值不正确。

我的 JButton 和相关动作侦听器的代码片段:

JButton btnFetch = new JButton("Fetch Tweets!");
    btnFetch.setBackground(new Color(255, 153, 51));
    btnFetch.setForeground(new Color(0, 0, 128));
    btnFetch.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            app.Query_Variable = queryText.getText();
            app.Start_Date = startText.getText();
            app.End_Date = endText.getText();

            try {
                app.getTweetCount();
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }

            response = new ResponseOption(app.Tweet_Count);

            /*
            try {
                app.runApplication();

            } catch (InstantiationException | IllegalAccessException
                    | ClassNotFoundException | ParseException
                    | SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }*/

            JOptionPane.showMessageDialog(frame, "Find your file at " + app.Printed_Filename);                  
        }
    });

我为名为 "ResponseOption" 的 showMessageDialog 创建了一个 class,认为这是初始化对话框所必需的。但是,显示的对话框不包含 ResultSet 值...

public class ResponseOption {       
    int RowCount;       
    Object[] options = {"Yes, Please.", 
                        "No, Thanks."};     
    String question = "There are " + RowCount + " Tweets. Do you want to print them?";      
    int n = JOptionPane.showOptionDialog(frame,
            "There are " + X + " Tweets. Do you want to print them?",
            "Question",
            JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE,       
            null,                               // do not use custom icon
            options,                            // titles of buttons
            options[0]);                        // default button title


    public ResponseOption(int Rowcount) {           
        this.RowCount = RowCount;
    }       
}

getTweetCount() 代码: --- 这个代码很好。

public int getTweetCount() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {

    // create a mysql database connection
    String myDriver = "com.mysql.jdbc.Driver";
    String myUrl = "jdbc:mysql://localhost:3306/?";
    Class.forName(myDriver).newInstance();
    Connection conn = DriverManager.getConnection(myUrl, User, Password);
    PreparedStatement setNames = conn.prepareStatement("SET NAMES 'utf8mb4'");
    setNames.execute();

    // fetch tweet count
    String CountSQL = "SELECT count(*) "
                    + "FROM test.tweet "
                    + "WHERE text LIKE '%" + Query_Variable + "%' "
                    + "AND created_at BETWEEN '" + Start_Date + "' AND '" + End_Date + "';";

    PreparedStatement CountPS = conn.prepareStatement(CountSQL);
    ResultSet CountRS = CountPS.executeQuery();

    if (CountRS.next()) {           
        Tweet_Count = CountRS.getInt(1);
    }

    System.out.println("Tweet count = " + Tweet_Count);     
    return Tweet_Count;     
}

一个可能的改进,更改 ResponseOption 以便 JOptionPane 不在创建对象时调用,而是在推文计数可用的构造函数中调用:

public ResponseOption(int rowCount, JFrame frame) {

    this.rowCount = rowCount;
    String question = "There are " + rowCount + " Tweets. Do you want to print them?";
    n = JOptionPane.showOptionDialog(frame,
            question,
            "Question",
            JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE,       
            null,                               
            options,                            
            options[0]); 
    // do something with n here?
}

请注意,您之前在变量声明部分调用了 JOptionPane。这在首次创建对象时调用,并且在 之前 调用构造函数,这让你很困惑。我自己,我想知道是否所有这些都可以包含在一个静态方法中。