如何在Java中合并2个列表?
我想从数据库中检索数据行,然后对这些数据进行一些编辑:添加,删除,编辑记录。但是当我添加新记录并重新加载我的页面时,数据库中的所有记录都是重复的!?
出了什么问题?我的缺陷在哪里?
提前致谢!
我的名单:
private List<Order> orderList = new ArrayList<>(getAllUsers());
public List<Order> getOrderList() {
return orderList;
}
public List<Order> getAllUsers() {
try {
orderList = new ArrayList<>();
Connection connection = databaseConnection.getConnection();
SQL = "SELECT * FROM Registration";
prepStatement = connection.prepareStatement(SQL);
resultSet = prepStatement.executeQuery();
boolean foundResult = false;
while (resultSet.next() == true) {
orders = new Order(id, orderNo, productName, price, qty);
orders.setId(resultSet.getInt("id"));
orders.setOrderNo(resultSet.getString("orderNo"));
orders.setProductName(resultSet.getString("productName"));
orders.setPrice(resultSet.getBigDecimal("price"));
orders.setQty(resultSet.getInt("qty"));
orderList.add(orders);
foundResult = true;
}
resultSet.close();
prepStatement.close();
if (foundResult) {
return orderList;
} else {
return null; // no entries found.
}
} catch (Exception eX) {
// TODO: handle exception
System.out.println("Error in getMeineList()--> " + eX.getMessage());
return (null);
}
}
public String addNew() {
orders = new Order();
orders.setEditable(true);
orderList.add(orders);
return "";
}
public String edit(Order ord) {
ord.setEditable(true);
return null;
}
public void insert() {
// int q= 0;
System.out.println("Inserting...!");
try {
connection = databaseConnection.getConnection();
statement = connection.createStatement();
SQL = "INSERT INTO Registration (orderNo,productName,price,qty) VALUES (?,?,?,?)";
prepStatement = connection.prepareStatement(SQL);
prepStatement.setString(1, orderNo);
prepStatement.setString(2, productName);
prepStatement.setBigDecimal(3, price);
prepStatement.setInt(4, qty);
prepStatement.executeUpdate();
System.out.println("User added Successfully!");
FacesMessage message = new FacesMessage("New User added!");
FacesContext.getCurrentInstance().addMessage("adminForm: saveButton", message);
statement.close();
prepStatement.close();
connection.close();
} catch (SQLException SQLex) {
// TODO: handle exception
SQLex.printStackTrace();
} catch (Exception IOex) {
// TODO: handle exception
IOex.printStackTrace();
}
}