是否可以从另一种方法引用List方法?

时间:2017-01-21 12:34:40

标签: java list arraylist

我的目标是: enter image description here 打开空白字段以在此表中添加新数据。

我想从addNewUser方法中引用List方法来添加新用户,这根本不工作,没有任何错误:

public void addNewUser(){
myList= getUsers();       // new list     
allValues = new Values(); // Values class with setters & getters           
allValues.setEditable(true); //boolean variable must open input fields in xhtml page
myList.add(allValues);
}

列表方法,其中ret。来自DB:

static List<Values> valuesList;
static Values allValues;
static List<Values> myList;

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

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();
    }

Comlete课程:

@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"; //newUsers
      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();
    }

    public static void databaseConnection() {
            try {
                Class.forName(JDBC_DRIVER);
                //open connection
                   System.out.println("Connecting to a selected database...");
                    connection = DriverManager.getConnection(DB_URL, USER, PASS);
                   System.out.println("Connected database successfully...");
         }catch(SQLException sqlEx){
             sqlEx.printStackTrace();//handle errors for jdbc
             System.out.println("Exeption occured in the process: "+sqlEx);
         }catch(Exception ex){
             ex.printStackTrace();//handle errors for ClassForName
             System.out.println("Exception occured in the process: "+ex);
            }
        }

    public static void close(Connection connect) {
        try {
            if(connect!= null){
            connect.close();
            }
        System.out.println("DataBase Connection closed!!!");
        System.out.println("Good bye!!!");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
@ManagedBean(name="values")
@RequestScoped
public static class Values{
      private int id;
      private String orderNo;
      private String productName;
      private BigDecimal price;
      private int qty;
      private boolean editable;

 public Values(){
 }
 public Values(int id, String orderNo, String productName, BigDecimal price, int qty){
     this.id= id;
     this.orderNo= orderNo;
     this.productName= productName;
     this.price= price;
     this.qty= qty;
 }
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getOrderNo() {
    return orderNo;
}
public void setOrderNo(String orderNo) {
    this.orderNo = orderNo;
}
public String getProductName() {
    return productName;
}
public void setProductName(String productName) {
    this.productName = productName;
}
public BigDecimal getPrice() {
    return price;
}
public void setPrice(BigDecimal price) {
    this.price = price;
}
public int getQty() {
    return qty;
}
public void setQty(int qty) {
    this.qty = qty;
}
public boolean isEditable() {
    return editable;
}
public void setEditable(boolean editable) {
    this.editable = editable;
}

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:column>
     <f:facet name="header">Action</f:facet>
     <h:commandLink value="Delete" action="#{values.dataDel(x.id)}"/>     
   </h:column>
 </h:dataTable>
 <h:commandButton value="AddNew" action="#{values.addNewUser}" update="table"/>
 </center>
</h:form>

1 个答案:

答案 0 :(得分:0)

伊万,

错过了定义变量(myList,allValues)的代码。

似乎这些变量是全局的(不是一个好主意),因为它们没有在示例中定义。还要检查getUsers()是否为静态而addNewUser不是。

您需要提供该上下文以使答案有用。