Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. @SuppressWarnings("serial")
  2. @ManagedBean(name= "user")
  3. @SessionScoped
  4. public class User implements Serializable{
  5. static final String JDBC_DRIVER= "com.mysql.jdbc.Driver";
  6. static final String DB_URL= "jdbc:mysql://localhost/updateTesting";
  7. static final String USER= "****";
  8. static final String PASS= "****";
  9. static Connection connection;
  10. static PreparedStatement prepStatement;
  11. static List<Values> valuesList;
  12. static Values allValues;
  13.  
  14. private static List<Values> getUsers(){
  15. try {
  16. databaseConnection();
  17. String SQL= "SELECT * FROM Registration";
  18. System.out.println("Retreive values from Registration table...");
  19. PreparedStatement prepStatement= connection.prepareStatement(SQL);
  20. valuesList= new ArrayList<>();
  21. ResultSet resultSet= prepStatement.executeQuery();
  22. boolean found= false;
  23. while(resultSet.next()== true){
  24. allValues= new Values();
  25. allValues.setId(resultSet.getInt("id"));
  26. allValues.setOrderNo(resultSet.getString("orderNo"));
  27. allValues.setProductName(resultSet.getString("productName"));
  28. allValues.setPrice(resultSet.getBigDecimal("price"));
  29. allValues.setQty(resultSet.getInt("qty"));
  30. valuesList.add(allValues);
  31. found= true;
  32. }
  33. resultSet.close();
  34. prepStatement.close();
  35. close(connection);
  36. if(found){
  37. return valuesList;
  38. }else {
  39. return null;
  40. }
  41. } catch (Exception e) {
  42. // TODO: handle exception
  43. e.printStackTrace();
  44. System.out.println("Error in getUsers()-->"+e.getMessage());
  45. }
  46. return (null);
  47. }
  48.  
  49. public List<Values> getInformation(){
  50. return getUsers();
  51. }
  52.  
  53. public static void databaseConnection() {
  54. try {
  55. Class.forName(JDBC_DRIVER);
  56. //open connection
  57. System.out.println("Connecting to a selected database...");
  58. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  59. System.out.println("Connected database successfully...");
  60. }catch(SQLException sqlEx){
  61. sqlEx.printStackTrace();//handle errors for jdbc
  62. System.out.println("Exeption occured in the process: "+sqlEx);
  63. }catch(Exception ex){
  64. ex.printStackTrace();//handle errors for ClassForName
  65. System.out.println("Exception occured in the process: "+ex);
  66. }
  67. }
  68.  
  69. public static void close(Connection connect) {
  70. try {
  71. if(connect!= null){
  72. connect.close();
  73. }
  74. System.out.println("DataBase Connection closed!!!");
  75. System.out.println("Good bye!!!");
  76. } catch (Exception ex) {
  77. ex.printStackTrace();
  78. }
  79. }
  80.  
  81. @ManagedBean(name="values")
  82. @RequestScoped
  83. public static class Values{
  84. private int id;
  85. private String orderNo;
  86. private String productName;
  87. private BigDecimal price;
  88. private int qty;
  89. private boolean editable;
  90.  
  91. public Values(){
  92. }
  93.  
  94. public Values(int id, String orderNo, String productName, BigDecimal price, int qty){
  95. this.id= id;
  96. this.orderNo= orderNo;
  97. this.productName= productName;
  98. this.price= price;
  99. this.qty= qty;
  100. }
  101. public int getId() {
  102. return id;
  103. }
  104. public void setId(int id) {
  105. this.id = id;
  106. }
  107. public String getOrderNo() {
  108. return orderNo;
  109. }
  110. public void setOrderNo(String orderNo) {
  111. this.orderNo = orderNo;
  112. }
  113. public String getProductName() {
  114. return productName;
  115. }
  116. public void setProductName(String productName) {
  117. this.productName = productName;
  118. }
  119. public BigDecimal getPrice() {
  120. return price;
  121. }
  122. public void setPrice(BigDecimal price) {
  123. this.price = price;
  124. }
  125. public int getQty() {
  126. return qty;
  127. }
  128. public void setQty(int qty) {
  129. this.qty = qty;
  130. }
  131. public boolean isEditable() {
  132. return editable;
  133. }
  134. public void setEditable(boolean editable) {
  135. this.editable = editable;
  136. }
  137.  
  138. private List<Values> myList;
  139.  
  140. public List<Values> getMyList() {
  141. return myList;
  142. }
  143.  
  144. public void addNewUser(){
  145. myList= getUsers();
  146. allValues = new Values();
  147. allValues.setEditable(true);
  148. myList.add(allValues);
  149. }
  150.  
  151. <h:form>
  152. <center>
  153. <h:dataTable value="#{user.information}" var="x" border="1" id="table">
  154. <h:column>
  155. <f:facet name="header">
  156. <h:outputText value="Id"></h:outputText>
  157. </f:facet>
  158. <h:outputText value="#{x.id}"></h:outputText>
  159. </h:column>
  160. <h:column>
  161. <f:facet name="header">
  162. <h:outputText value="Order No"></h:outputText>
  163. </f:facet>
  164. <h:inputText value="#{x.orderNo}" rendered="#{x.editable}"></h:inputText>
  165. <h:outputText value="#{x.orderNo}" rendered="#{not x.editable}"></h:outputText>
  166. </h:column>
  167. <h:column>
  168. <f:facet name="header">
  169. <h:outputText value="Product name"></h:outputText>
  170. </f:facet>
  171. <h:inputText value="#{x.productName}" rendered="#{x.editable}"></h:inputText>
  172. <h:outputText value="#{x.productName}" rendered="#{not x.editable}"></h:outputText>
  173. </h:column>
  174. <h:column>
  175. <f:facet name="header">
  176. <h:outputText value="Price"></h:outputText>
  177. </f:facet>
  178. <h:inputText value="#{x.price}" rendered="#{x.editable}"></h:inputText>
  179. <h:outputText value="#{x.price}" rendered="#{not x.editable}"></h:outputText>
  180. </h:column>
  181. <h:column>
  182. <f:facet name="header">
  183. <h:outputText value="Quantity"></h:outputText>
  184. </f:facet>
  185. <h:inputText value="#{x.qty}" rendered="#{x.editable}"></h:inputText>
  186. <h:outputText value="#{x.qty}" rendered="#{not x.editable}"></h:outputText>
  187. </h:column>
  188. </h:dataTable>
  189. <h:commandButton value="AddNew" action="#{values.addNewUser}" update="table"/>
  190. </center>
  191. </h:form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement