简体   繁体   English

Java Swing jComboBox无法从数据库填充

[英]Java Swing jComboBox not filling from Database

So I am working on a task force schedule for my mothers charity project. 因此,我正在为母亲的慈善项目制定工作队时间表。 The aim is to check in the volunteers, display the roster for times and what station they are at. 目的是登记志愿者,显示花名册以显示时间和他们所处的位置。 I have successfully built a login form, that pulls the data from the database and verify if your entered username (from dropdown) and password are correct. 我已经成功构建了一个登录表单,该表单从数据库中提取数据并验证您输入的用户名(从下拉列表中)和密码是否正确。 I have this method (NB: for the login form, it works and is not the problem) to fill the username dropdown called 'cbxUsername' which is called inside the main method when the window loads: 我有此方法(注意:对于登录表单,它是有效的,不是问题)来填充名为'cbxUsername'的用户名下拉列表,在加载窗口时在主方法中调用该用户名:

public void loginToProgram() throws Exception{
        user = (String) cbxUsername.getSelectedItem();
        if(user == "Please select a username"){
            JOptionPane.showMessageDialog(null, "Sorry, You need to select a username to continue");
        }else if(txtPassword.getText().length() == 0){
            JOptionPane.showMessageDialog(null, "Sorry, You need to enter a password to continue");
        } else {
            checkAuth();
        }

}

and then this method is refered to above, this is the checkAuth() method: 然后在上面引用此方法,这是checkAuth()方法:

public void checkAuth() throws Exception{
    Class.forName("com.mysql.jdbc.Driver");

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost/crm", "root", "t00rt00r");
    PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT `password` FROM users WHERE `username` = '"+ user +"'");
    ResultSet result = statement.executeQuery();
    if(result.next() != false){
        pass = result.getString(1);
    }
    if(txtPassword.getText().equalsIgnoreCase(pass)){
        //JOptionPane.showMessageDialog(null, "Worked");
        CheckIn chk =  new CheckIn();

        chk.setVisible(true);
    } else {
        JOptionPane.showMessageDialog(null, "Sorry, wrong password");
    }
}

Now I have copied the first code (the method 'loginToProgram()') and altered it abit. 现在,我复制了第一个代码(方法'loginToProgram()')并进行了一些改动。

Now I have a new window called 'CheckIn', with a jComboBox called 'cbxCIFirstName'. 现在,我有一个名为“ CheckIn”的新窗口,带有一个名为“ cbxCIFirstName”的jComboBox。 The code to fill up this combobox is: 填充此组合框的代码是:

public static void fillFirstNameCombobox() throws Exception{
    Class.forName("com.mysql.jdbc.Driver");

    Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/crm", "root", "t00rt00r");
    PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT `first_name` FROM `volunteers`");
    ResultSet result = statement.executeQuery();
    cbxCIFirstName.setToolTipText("Select a first name");
    cbxCIFirstName.setEditable(true);
    cbxCIFirstName.addItem("Please Select a first name");
    while(result.next()){
        cbxCILastName.addItem(result.getString(1));
    }
    System.out.println(result.getString(1));
}

Please note: I put the System.out.println(result.getString(1)); 请注意:我把System.out.println(result.getString(1)); there just to see if it was coming through in the console, and it is NOT coming through on the console. 只是看它是否在控制台中通过,而没有在控制台中通过。 What am I doing wrong? 我究竟做错了什么? Here is my Main method: 这是我的主要方法:

public static void main(String[] args) throws Exception{

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                fillFirstNameCombobox();
                CheckIn frame = new CheckIn();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

Any suggestions will be greatly appreciated, any comments, guides, tutorials or anything I will take into consideration and try! 任何建议将不胜感激,任何评论,指南,教程或任何我会考虑并尝试的内容!

Thanks for your time, reading this post and thanks in advance for your answers, Josh. 谢谢您的时间,阅读这篇文章,并预先感谢您的回答,乔希。

public static void fillFirstNameCombobox() throws Exception{
    Class.forName("com.mysql.jdbc.Driver");

    Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/crm", "root", "t00rt00r");
    PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT `first_name` FROM `volunteers`");
    ResultSet result = statement.executeQuery();
    cbxCIFirstName.setToolTipText("Select a first name");
    cbxCIFirstName.setEditable(true);
    cbxCIFirstName.addItem("Please Select a first name");
    while(result.next()){
        cbxCILastName.addItem(result.getString(1)); 
    }
    System.out.println(result.getString(1));// this line can not be outside while loop
}

make this way 这样

  public static void fillFirstNameCombobox() throws Exception{
        Class.forName("com.mysql.jdbc.Driver");

        Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/crm", "root", "t00rt00r");
        PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT first_name FROM volunteers");
        ResultSet result = statement.executeQuery();
        cbxCIFirstName.setToolTipText("Select a first name");
        cbxCIFirstName.setEditable(true);
        cbxCIFirstName.addItem("Please Select a first name");
        while(result.next()){
            cbxCILastName.addItem(result.getString(1)); 
System.out.println(result.getString(1));// this line can not be outside while loop
        }

}

REASON 原因

Actually the cursor is at top level.When you are calling result.next) then the cursor comes down and at the end the cursor lies at bottom. 实际上,光标位于顶层。当您调用result.next)时,光标下降,最后光标位于底部。 That means at the end it return false and control comes out of while. 这意味着最后它返回false,而控制权则退出了。 You are trying to print after the while that means the cursor is already at the bottom so thats the problem. 您稍后尝试打印while这意味着光标已在底部,这就是问题所在。

Also the way you are using preparedstatement is not the right way. 另外,您使用preparedstatement的方式也不正确。 use this way 用这种方式

PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT `password` FROM users WHERE `username` = ?);
statement.setString(1,user );

对于所有正在阅读此线程的人,我通过使用@javaBeginner提供的正确的prepareStatement方法(感谢)来修复它,然后从类的构造函数中调用它,而不是从主方法中调用方法“ fillFirstNameCombobox()”,所以该类称为“ ClockIn”,我在构造函数/方法“ ClockIn()”的最后一行调用了它

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

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