Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将ResultSet响应传递给JOptionPane showMessageDialog_Java_Mysql_Swing_Actionlistener_Joptionpane - Fatal编程技术网

Java 如何将ResultSet响应传递给JOptionPane showMessageDialog

Java 如何将ResultSet响应传递给JOptionPane showMessageDialog,java,mysql,swing,actionlistener,joptionpane,Java,Mysql,Swing,Actionlistener,Joptionpane,甲骨文“如何创建对话框”对我没有帮助,我也没有看到任何其他帖子,因为它们无法解决将值传递给showMessageDialog的问题 我有一个Java Swing应用程序,如下所示: 当用户按下按钮“fetchtweets”时,用户将信息输入到三个字段中的每一个字段中,这三个字段将合并到MySQL查询中 按下按钮后,我想将行数返回到ShowMessage对话框,让用户选择是否继续 问题:我不知道如何将结果集值传递给showMessageDialog。是否需要在JOptionPane上设置操作侦听

甲骨文“如何创建对话框”对我没有帮助,我也没有看到任何其他帖子,因为它们无法解决将值传递给showMessageDialog的问题

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

当用户按下按钮“fetchtweets”时,用户将信息输入到三个字段中的每一个字段中,这三个字段将合并到MySQL查询中

按下按钮后,我想将行数返回到ShowMessage对话框,让用户选择是否继续

问题:我不知道如何将结果集值传递给showMessageDialog。是否需要在JOptionPane上设置操作侦听器?如果是,您如何管理时间安排

我的ShowMessage对话框如下所示。如您所见,“0”的值不正确

my JButton和关联操作侦听器的代码段:

我为showMessageDialog创建了一个名为“ResponseOption”的类,认为这是初始化对话框所必需的。但是,对话框显示时不包含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,而是在tweet计数可用的构造函数中调用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。当第一次创建对象时,在调用构造函数之前调用它,这会把你搞得一团糟。就我自己而言,我想知道是否所有这些都可以封装在一个静态方法中。

您永远不应该传递java.sql工件。在方法中执行查询,将结果加载到对象或集合中,然后进行清理。请使用您的代码包含
app.getTweetCount()
的内容。还有,为什么不让app.getTweetCount()返回一个
int
并使用该数字,为什么要将其存储在公共字段中?durron597--getTweetCount方法的类型是int。此方法工作正常。但是,我无法将该值传递到showMessageDialog.duffymo中——您认为我在哪里“传递java.sql工件”?我相信我调用了
int
类型的方法,然后在实例化JOptionPane时尝试使用该方法。我不明白你的建议。你能详细说明一下吗?谢谢!将JOptionPane移动到构造函数是有效的。我想我需要更好地理解为什么这与我以前的选择不同。谢谢,气垫船。我只是在一块一块地修改,因为这是我的第一个Swing应用程序。既然我已经证明我可以将值传递到JOptionPane中,我将努力清理它。我想你是对的。旁注——为什么这个问题是否定的?“我认为这是一个很好的表述,不是很琐碎的问题。”萨利:根据你发布的内容,很难找出问题所在,因为发布的大部分信息与你的问题无关。事实上,您的关键问题与Swing、JOptionPane或数据库编程无关,而与您在初始化变量之前尝试使用变量有关。先考虑使用调试器(例如在NETBeaS、Eclipse或IntLyJ中可用),然后再发布以隔离问题。
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?
}