0

所以我正在为我母亲的慈善项目制定一个工作组时间表。目的是检查志愿者,显示名册的时间和他们所在的车站。我已经成功构建了一个登录表单,它从数据库中提取数据并验证您输入的用户名(来自下拉列表)和密码是否正确。我有这种方法(注意:对于登录表单,它可以工作并且不是问题)来填充调用的用户名下拉列表'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();
        }

}

然后就是上面提到的这个方法,就是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");
    }
}

现在我已经复制了第一个代码(方法'loginToProgram()')并对其进行了修改。

现在我有一个名为“CheckIn”的新窗口,其中有一个名为“cbxCIFirstName”的 jComboBox。填充此组合框的代码是:

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));
}

请注意:我把它放在System.out.println(result.getString(1));那里只是为了看看它是否在控制台中通过,而它没有在控制台上通过。我究竟做错了什么?这是我的主要方法:

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();
            }
        }
    });
}

任何建议将不胜感激,任何评论、指南、教程或任何我都会考虑和尝试的东西!

感谢您抽出宝贵时间阅读这篇文章,并提前感谢您的回答,乔希。

4

2 回答 2

0
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
}

这样做

  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
        }

}

原因

实际上光标位于顶层。当您调用 result.next) 时,光标会下降,最后光标位于底部。这意味着最后它返回 false 并且控制权消失了。您正在尝试打印,while 这意味着光标已经在底部,这就是问题所在。

此外,您使用preparedstatement 的方式也不是正确的方式。用这种方式

PreparedStatement statement = (PreparedStatement) con.prepareStatement("SELECT `password` FROM users WHERE `username` = ?);
statement.setString(1,user );
于 2013-09-07T08:34:19.823 回答
0

对于所有正在阅读该线程的人,我通过使用@javaBeginner(谢谢)给出的正确preparedStatement方法来修复它,然后我没有从main方法调用方法“fillFirstNameCombobox()”,而是从类的构造函数中调用它,所以这个类被称为“ClockIn”,我在构造函数/方法“ClockIn()”的最后一行调用它

于 2013-09-07T11:09:11.477 回答