Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @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();
- }
- 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);
- }
- <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>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement