'Pass User ID from one page to another from Java ArrayList
I am developing a system using CRUD functionality. I am retrieving users from my database and passing these into an ArrayList where the user can click next user and previous user buttons to display each user in the system.
The code for getting the users from the database:
//refresh data from database
public void refreshData(){
try {
users.clear(); //clear users array list
String sqlGetTypes = "SELECT * FROM tbl_login"; //select all data from db
Connection conn = db.Connect(); //connection to db
PreparedStatement psGetUsers = conn.prepareStatement(sqlGetTypes);
ResultSet rs = psGetUsers.executeQuery();
while (rs.next()){ //filter through users
User u = new User(rs.getInt("id"), rs.getString("username"),
rs.getString("password"), rs.getString("role"));
users.add(u); //add each user
}
rs = psGetUsers.executeQuery(); //execute the query
this.comboBoxUserRole.removeAllItems(); //remove items from combo box
while(rs.next()){
roleTypeModel.addElement(new ComboBoxItem(rs.getInt("id"),
rs.getString("username")));
}
for (int i = 0; i < roleTypeModel.getSize(); i++){
ComboBoxItem cbi = (ComboBoxItem)roleTypeModel.getElementAt(i); //create
object of type combo box item
if (cbi.getId() == users.get(currentUser).getLoginID()){
roleTypeModel.setSelectedItem(cbi); //select users role from selected user
}
}
this.comboBoxUserRole.setModel(roleTypeModel); //set the user role in combo box
this.txtUserID.setText(String.valueOf(users.get(currentUser).getLoginID())); //set user ID in text field
this.txtUsername.setText(String.valueOf(users.get(currentUser).getUsername())); //set username in text field
}
catch (Exception e){
JOptionPane.showMessageDialog(null, "Cannot get users at this time. Error: " + e);
}
}
I then have a button named 'Update current user' which will redirect the user to another page in order to update that users login details. Here I am wanting to transfer the UserID over from the current user that is displayed at the time. I will then use the UserID to query the database again in the new page to identify the user, which will then be able to be updated based on the new values that the user enters.
The code for the update user page:
//refresh data from database
public void refreshData(){
try {
system_users su = new system_users(); //create new instance of previous page
String userId = (su.txtUserID.getText()); //set the ID field from previous page
String sqlGetTypes = "SELECT * FROM tbl_login WHERE id = ?"; //select user from database
Connection conn = db.Connect(); //connection to db
PreparedStatement ps = conn.prepareStatement(sqlGetTypes);
ps.setString(1, userID); //set ID to user ID variable
ResultSet rs = ps.executeQuery();
while (rs.next()){ //filter through users
User u = new User(rs.getInt("id"), rs.getString("username"), rs.getString("password"), rs.getString("role"));
users.add(u);
}
rs = ps.executeQuery(); //execute the query
this.comboBoxUserRole.removeAllItems();
while(rs.next()){
roleTypeModel.addElement(new ComboBoxItem(rs.getInt("id"), rs.getString("username")));
}
for (int i = 0; i < roleTypeModel.getSize(); i++){
//JOptionPane.showMessageDialog(null, i);
ComboBoxItem cbi = (ComboBoxItem)roleTypeModel.getElementAt(i); //create object of type combo box item
if (cbi.getId() == users.get(currentUser).getLoginID()){
roleTypeModel.setSelectedItem(cbi);
}
}
this.comboBoxUserRole.setModel(roleTypeModel);
this.txtUpdateUsername.setText(String.valueOf(users.get(currentUser).getUsername()));
this.txtUpdatePassword.setText(String.valueOf(users.get(currentUser).getPassword()));
}
catch (Exception e){
JOptionPane.showMessageDialog(null, "Cannot get customer information at this time. Error: " + e);
}
}
From this, the user is then able to overwrite the values input into the text fields and click on 'Update user' button, with the following code:
try{
JOptionPane.showMessageDialog(null, "cbi");
//ComboBoxItem gender = (ComboBoxItem)genderTypeModel.getSelectedItem(); //get the users role from combo box
JOptionPane.showMessageDialog(null, "conn");
Connection conn = db.Connect();
String sqlInsertUser = "UPDATE tbl_login "
+ " SET (username, password, role)"
+ " VALUES (?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sqlInsertUser);
ps.setString(1, this.txtUpdateUsername.getText());
ps.setString(2, this.txtUpdatePassword.getText());
ps.setString(3, String.valueOf(comboBoxUserRole));
ps.executeUpdate(); //execute the update
JOptionPane.showMessageDialog(null, "Customer has been successfully updated");
}
catch (Exception e){
JOptionPane.showMessageDialog(null, "Customer could not be updated. Please try again. Error: " + e);
}
The problem I am having is that I am currently unable to pass the UserID from the first block of code on one page, to the update user page. Is there any way to get the user ID which will change in the Array List I am using from the database, and pass this on to the next page so I am able to update my user login credentials.
Please let me know if this is able to be done :) Feel free to ask any questions if my explanation is bad. Thank you
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
