我正在创建一个银行应用程序,我可以在其中使用搜索文本字段在数据库中查找客户(我使用的是 MySQL 数据库)。当我输入一封信以按姓氏搜索客户时,应用程序确实从数据库中获取数据并将结果显示在 JList 中。就我而言,我的数据库中共有 5 条记录,其中 4 个姓氏以字母“S”开头,一个姓氏以字母“M”开头。一旦行或结果在 JList 中,我可以单击一行并使用正确的信息自动填充名字、姓氏、城市等文本字段。但是,我遇到了两个问题:
就我而言,如果我按字母“S”搜索姓氏,我的 JList 将只显示数据库中的 4 条记录,这是正确的。当我从 JList 中选择一行时,它应该会自动用正确的信息填充所有文本字段。但是,如果我选择 JList 中的第一行,我的文本字段将始终填充 JList 中最后一行的信息。我在 JList 中选择哪一行并不重要,文本字段将始终用最后一行填充信息。
如果我搜索字母“S”,我会得到 4 条记录,这是正确的。但是,当应用程序仍在运行时,我决定搜索字母“M”,我得到一条记录,这也是正确的,但如果我再次尝试搜索字母“S”,我只得到一条记录。为什么?应该是4。
这就是我想要完成的: 银行申请链接
这是我的代码的一部分:
// function that will search for records in the database
private void searchRecord(){
try {
connect = DriverManager.getConnection(url,user,password);
SQLStatement = connect.createStatement();
Class.forName("com.mysql.jdbc.Driver");
model.clear();
String select = "SELECT * FROM customerinfo WHERE LastName LIKE '"+ txtSearch.getText().trim() +"%'";
rows = SQLStatement.executeQuery(select);
while(rows.next()){
//model.addElement(rows.getString("LastName"));
model.addElement(rows.getString("LastName") + ", " + rows.getString("FirstName") + " " + rows.getString("MiddleInitial") + ".");
}
rows.close();
SQLStatement.close();
connect.close();
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(this, "Where is your Mysql JDBC Driver?");
e.printStackTrace();
} catch (SQLException e){
JOptionPane.showMessageDialog(this, "SQLException: " + e.getMessage());
JOptionPane.showMessageDialog(this, "VendorError: " + e.getErrorCode());
}
}
// Implement Action Listener
private class handler implements ActionListener{
@Override
public void actionPerformed(ActionEvent event) {
// if user clicks on New Customer Button, do the following...
if(event.getSource() == newCustomer){
checkForEmptyFields();
insertDatabase();
clearFields();
} else if(event.getSource() == update){
checkForEmptyFields();
updateDatabase();
clearFields();
} else if(event.getSource() == remove){
checkForEmptyFields();
deleteRecord();
clearFields();
} else if(event.getSource() == cancel){
clearFields();
} else if(event.getSource() == open){
} else if(event.getSource() == search){
searchRecord();
}
}
}
// function that will set the text fields
private void setTextFields(int customerID, String firstName, String lastName, String middleInitial, String street, String city, String state, int zipCode, int phone, String email){
String convertCustomerID = Integer.toString(customerID);
txtCustomerID.setText(convertCustomerID);
txtFirstName.setText(firstName);
txtLastName.setText(lastName);
txtMiddleInitial.setText(middleInitial);
txtStreet.setText(street);
txtCity.setText(city);
txtState.setText(state);
String convertZipCode = Integer.toString(zipCode);
txtZip.setText(convertZipCode);
String convertPhone = Integer.toString(phone);
txtPhone.setText(convertPhone);
txtEmail.setText(email);
}
// Implement List Selection Listener
private class listener implements ListSelectionListener{
@Override
public void valueChanged(ListSelectionEvent event) {
try {
connect = DriverManager.getConnection(url,user,password);
SQLStatement = connect.createStatement();
String select = "SELECT * FROM customerinfo WHERE LastName LIKE '"+ txtSearch.getText().trim() + "%'";
rows = SQLStatement.executeQuery(select);
if(!event.getValueIsAdjusting()){
while(rows.next()){
setTextFields(rows.getInt("CustomerID"), rows.getString("FirstName"), rows.getString("LastName"), rows.getString("MiddleInitial"),
rows.getString("Street"), rows.getString("City"), rows.getString("State"), rows.getInt("ZipCode"), rows.getInt("Phone"),
rows.getString("Email"));
}
}
} catch(Exception ex) {
JOptionPane.showMessageDialog(null, "Cannot get field values");
}
}
}
必须是给我问题编号 1 的 rows.next()。 rows.next() 遍历数据库中的所有记录,在完成所有记录之后,它似乎停留在最后记录。不知何故,我需要重置位置,当我单击 JList 中的一行时,它应该等于具有相同值的行,并用正确的信息填充所有文本字段。
我已经尝试了一切,此时我已经放弃了!请帮我!谢谢!