所以我正在为我母亲的慈善项目制定一个工作组时间表。目的是检查志愿者,显示名册的时间和他们所在的车站。我已经成功构建了一个登录表单,它从数据库中提取数据并验证您输入的用户名(来自下拉列表)和密码是否正确。我有这种方法(注意:对于登录表单,它可以工作并且不是问题)来填充调用的用户名下拉列表'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();
}
}
});
}
任何建议将不胜感激,任何评论、指南、教程或任何我都会考虑和尝试的东西!
感谢您抽出宝贵时间阅读这篇文章,并提前感谢您的回答,乔希。