所以我正在为我母亲的慈善项目制定一份特遣部队时间表。目的是检查志愿者,显示时间表和他们所在的站点。我已经成功构建了一个登录表单,它从数据库中提取数据并验证您输入的用户名(来自下拉列表)和密码是否正确。我有这个方法(注意:对于登录表单,它工作,并不是问题)填写名为'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();
}
}
});
}
我将非常感谢任何建议,任何评论,指南,教程或任何我会考虑并尝试的内容!
感谢您的时间,阅读此帖并提前感谢您的回答, 约什
答案 0 :(得分: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 statement = (PreparedStatement) con.prepareStatement("SELECT `password` FROM users WHERE `username` = ?);
statement.setString(1,user );
答案 1 :(得分:0)
对于正在阅读此线程的所有人,我使用@javaBeginner(thanks)给出的正确的preparedStatement方法修复它,然后从main方法调用方法“fillFirstNameCombobox()”,我从class的构造函数,所以该类被称为“ClockIn”,我在构造函数/方法的最后一行调用它“ClockIn()”