java - 在从sql数据库列表中检索到的新记录中添加

标签 java mysql arraylist

我需要向列表中添加从数据库检索的新记录,- 这看起来很简单,但是如何连接(合并)这两种方法:

public void addNewUser(){
    myList= new ArrayList<>();
    Values newValue = new Values(this.id, this.orderNo, this.productName, this.price, this.qty);
    newValue.setEditable(true);
    myList.add(newValue);
}

我使用这样的方法从sql数据库检索用户列表:

private List<Values> valuesList;
private Values allValues;

private static List<Values> getUsers() {
    try {
        databaseConnection();
        String SQL = "SELECT * FROM Registration";
        System.out.println("Retrieve 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();
}

这是我的显示页面:

 <h:dataTable value="#{user.information}" var="x" border="1">
    <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="#{user.add}"></h:commandButton>

最佳答案

如果您想将其他值(例如第一个方法)添加到列表中,那么您可以这样做:

//myList = new ArrayList<>(); you dont need to use this, this can empty your list

myList = getUsers();//get your List
Values newValue = new Values(this.id, this.orderNo, this.productName, this.price, this.qty);

//add a new element to your list
newValue.setEditable(true);
myList.add(newValue);

注意

您的xhtml有问题:

<h:commandButton value="AddNew" action="#{user.add}"></h:commandButton>

因此,如果您尝试调用 addNewUser()那么这对你不起作用。

解决方案

这可以调用你的方法addNewUser()

<h:commandButton value="AddNew" action="#{user.addNewUser}"></h:commandButton>

之后:

您应该更新您的数据表

<h:commandButton value="AddNew" action="#{user.addNewUser}" update="idOfYourDataTable"/>

不要忘记向您的数据表添加 ID:

<h:dataTable value="#{user.information}" var="x" border="1" id="idOfYourDataTable">

...

另一件事:

<h:dataTable value="#{user.information}"

确保您有 List<Values> information在你的managedBean ,因为你放的代码中,没有List信息。

希望这可以帮助你。

关于java - 在从sql数据库列表中检索到的新记录中添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41747094/

相关文章:

php - PDO:MySQL 服务器已经消失,无法捕获异常

mysql - 我可以使用 mysql Dockerfile 构建一个已经在本地构建的 mysql 数据库吗?

Java Arraylist 程序测试失败

java - 保持 Java 程序永远运行或长时间运行的最佳方法是什么?

java - 如何找出哪个 Spring 组件不兼容?

php MySQL在嵌套div中循环

java - 数组列表异常错误

java - 带数组的多个输入

java - 本地类不兼容错误

java - 通过 JDBC Access Access(使用 ODBC?)