Is it possible to add a new record (new user) to the table that was extracted from the database? When you click the button, the fields for data entry and the button for saving them must open! I use jsf, the data is displayed in the xhtml page. Tell me please! Is everything clear, I explain!?

enter image description here

Methods for adding and saving new data: (myList- new ArrayList; Values ​​- class containing setters & getters. With the new list, everything is opened and added, but how to combine myList with getAllUsers () if I explain correctly?

public void addNewUser(){ Values newValue = new Values(); newValue.setEditable(true); myList.add(newValue); } public void saveNewUser(){ System.out.println("We're in saveNewUser()"); for(Values value: myList){ addUser(); value.setEditable(false); } } 

Method that retrieves data from the database:

 public ArrayList<Values> getAllUsers(){ ArrayList<Values> usersList= new ArrayList<>(); SQL= "SELECT id,orderNo,productName,price,qty FROM Registration"; System.out.println("Retreive values from Registration table..."); databaseConnection(); try { prepStatement= connection.prepareStatement(SQL); resultSet= prepStatement.executeQuery(); boolean found= false; while (resultSet.next()== true) { Values allValues= new Values(); allValues.setId(resultSet.getInt("id")); allValues.setOrderNo(resultSet.getString("orderNo")); allValues.setProductName(resultSet.getString("productName")); allValues.setPrice(resultSet.getBigDecimal("price")); allValues.setQty(resultSet.getInt("qty")); usersList.add(allValues); found= true; } resultSet.close(); prepStatement.close(); if (found) { return usersList; } else { return null; } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); System.out.println("Error in getAllUsers()-->"+e.getMessage()); }finally { close(connection); } return (null); } public List<Values> getInformation(){ return getAllUsers(); } 
  • why not use Hibernate or another ORM? - Mikhail Vaysman

2 answers 2

Create another addUser method in which you will access the database and write the value to the database table.

    If I understand correctly, then it is better to transfer to the getAllUsers() method a list of myList , and already fill it with data from the sample:

     public ArrayList<Values> getAllUsers(ArrayList<Values> usersList) { usersList = new ArrayList<Values>(); ... return usersList; } 

    And somewhere this method call:

     myList = getAllUsers(myList); 

    Only you need to take into account that myList will recreate the old data again, which is logical in principle, because it will be filled with records from the selection.