我正在創建一個銀行應用程序,我可以使用搜索文本字段來查找數據庫中的客戶(我正在使用MySQL數據庫)。當我輸入一個字母來搜索客戶的姓氏時,應用程序會從數據庫中提取數據並在JList中顯示結果。就我而言,我的數據庫中共有5條記錄,其中4個姓氏以字母「S」開頭,一個以字母「M」開頭。一旦行或結果在JList中,我可以單擊一行,並自動填充名字,姓氏,城市等文本文件以及適當的信息。然而,也有我有兩個問題:從數據庫中獲取數據,但沒有用Java中的正確數據填充文本框
就我而言,如果我用字母搜索的姓氏「S,」我的JList將顯示從數據庫中,這是正確的只有4條。當我從JList中選擇一行時,它應該自動填充所有帶有正確信息的文本字段。但是,如果我選擇JList中的第一行,我的文本字段將始終用JList中最後一行填充信息。無論我在JList中選擇哪一行,文本字段都會一直填充最後一行的信息。
如果我搜索字母「S」,我會得到4條記錄,這是正確的。然而,當應用程序仍在運行時,我決定搜索字母「M」,我得到一條記錄,這也是正確的,但如果我再次嘗試搜索字母「S」,則只能得到一條記錄。爲什麼?它應該是4
這就是我要完成的: Bank Application link
這裏是我的代碼部分:
// 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");
}
}
}
它是rows.next( )這是給我的問題編號1. rows.next()遍歷數據庫中的所有記錄,在完成所有記錄後,它似乎停留在最後一個記錄。不知何故,我需要重置位置,當我點擊JList中的一行時,它應該等於具有相同值的行,並用適當的信息填充所有文本字段。
我已經試過了一切,在這一點上我放棄了!請幫幫我!謝謝!
-1投票的原因:使用調試器並放入一堆打印語句,您應該能夠找到您的問題。 –
弗朗西斯,我不是先進的Java程序員,我嘗試了一切,從創建自己的函數,將結果打印到控制檯,創建自定義循環遍歷行,等等......我只是問一點點的幫助 – user1183022
你不需要成爲一名高級Java程序員。添加更多的打印語句,以真正看到發生了什麼,你將能夠發現問題。然後用調試器仔細地分析它(儘管你使用的事件監聽器可能很難)。用打印來驗證你的每一個假設。 –