如何向从DB中检索的列表添加新条目?

时间:2017-01-21 10:09:45

标签: java list arraylist

我需要做些什么才能通过打开的输入字段<h:inputText value="#{x.orderNo}" rendered="#{x.editable}"/>?

从xhtml页面添加此列表中的新条目(从数据库中检索)

我使用List方法从sql db中检索数据:

@SuppressWarnings("serial")
@ManagedBean(name= "user")
@SessionScoped
public class User implements Serializable{
      static final String JDBC_DRIVER= "com.mysql.jdbc.Driver";
      static final String DB_URL= "jdbc:mysql://localhost/updateTesting";
      static final String USER= "****";
      static final String PASS= "****";
      static Connection connection;
      static PreparedStatement prepStatement;
      static List<Values> valuesList;
      static Values allValues;

private static List<Values> getUsers(){
        try {
            databaseConnection();
        String SQL= "SELECT * FROM Registration";
        System.out.println("Retreive values from Registration table...");
        PreparedStatement prepStatement= connection.prepareStatement(SQL);
        valuesList= new ArrayList<>();
        ResultSet resultSet= prepStatement.executeQuery();
          boolean found= false;
          while(resultSet.next()== true){
              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"));
              valuesList.add(allValues);
               found= true;
          }
          resultSet.close();
          prepStatement.close();
           close(connection);
          if(found){
              return valuesList;
          }else {
            return null;
        }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            System.out.println("Error in getUsers()-->"+e.getMessage());
        }
        return (null);
    }

    public List<Values> getInformation(){
        return getUsers();
    }

这里我尝试创建新的List并将其与我的List方法getUsers()合并。当我点击按钮AddNew时,调用方法addNewUser()必须打开列表底部的输入字段。避免出现任何明显的错误,一切正常,但没有任何反应:

private List<Values> myList;

public List<Values> getMyList() {
    return myList;
}

public void addNewUser(){
myList= getUsers();                      
allValues = new Values();             
allValues.setEditable(true);
myList.add(allValues);
}

xhtml页面:

<h:form>
 <center>
 <h:dataTable value="#{user.information}" var="x" border="1" id="table">
    <h:column>
     <f:facet name="header">
      <h:outputText value="Id"></h:outputText>
     </f:facet>
     <h:outputText value="#{x.id}"></h:outputText>
   </h:column>
   <h:column>
     <f:facet name="header">
      <h:outputText value="Order No"></h:outputText>
     </f:facet>
     <h:inputText value="#{x.orderNo}" rendered="#{x.editable}"></h:inputText>
     <h:outputText value="#{x.orderNo}" rendered="#{not x.editable}"></h:outputText>
   </h:column>
   <h:column>
     <f:facet name="header">
      <h:outputText value="Product name"></h:outputText>
     </f:facet>
     <h:inputText value="#{x.productName}" rendered="#{x.editable}"></h:inputText>
     <h:outputText value="#{x.productName}" rendered="#{not x.editable}"></h:outputText>
   </h:column>
   <h:column>
     <f:facet name="header">
      <h:outputText value="Price"></h:outputText>
     </f:facet>
     <h:inputText value="#{x.price}" rendered="#{x.editable}"></h:inputText>
     <h:outputText value="#{x.price}" rendered="#{not x.editable}"></h:outputText>
   </h:column>
   <h:column>
     <f:facet name="header">
      <h:outputText value="Quantity"></h:outputText>
     </f:facet>
     <h:inputText value="#{x.qty}" rendered="#{x.editable}"></h:inputText>
     <h:outputText value="#{x.qty}" rendered="#{not x.editable}"></h:outputText>
   </h:column>
 </h:dataTable>
 <h:commandButton value="AddNew" action="#{values.addNewUser}" update="table"/>
 </center>
</h:form>

0 个答案:

没有答案