Guest User

Untitled

a guest
Mar 2nd, 2019
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 KB | None | 0 0
  1. package com.database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.Map;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import javax.inject.Named;
  13. import javax.enterprise.context.Dependent;
  14. import javax.faces.context.FacesContext;
  15.  
  16. @Named(value = "user1")
  17. @Dependent
  18. public class User {
  19.  
  20.     int id;
  21.     String name;
  22.     String email;
  23.     String password;
  24.     String gender;
  25.     String address;
  26.     ArrayList usersList;
  27.  
  28.     Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
  29.     Connection connection;
  30.  
  31.     public int getId() {
  32.         return id;
  33.     }
  34.  
  35.     public void setId(int id) {
  36.         this.id = id;
  37.     }
  38.  
  39.     public String getName() {
  40.         return name;
  41.     }
  42.  
  43.     public void setName(String name) {
  44.         this.name = name;
  45.     }
  46.  
  47.     public String getEmail() {
  48.         return email;
  49.     }
  50.  
  51.     public void setEmail(String email) {
  52.         this.email = email;
  53.     }
  54.  
  55.     public String getPassword() {
  56.         return password;
  57.     }
  58.  
  59.     public void setPassword(String password) {
  60.         this.password = password;
  61.     }
  62.  
  63.     public String getGender() {
  64.         return gender;
  65.     }
  66.  
  67.     public void setGender(String gender) {
  68.         this.gender = gender;
  69.     }
  70.  
  71.     public String getAddress() {
  72.         return address;
  73.     }
  74.  
  75.     public void setAddress(String address) {
  76.         this.address = address;
  77.     }
  78.  
  79.     public ArrayList getUsersList() {
  80.         return usersList;
  81.     }
  82.  
  83.     public void setUsersList(ArrayList usersList) {
  84.         this.usersList = usersList;
  85.     }
  86.  
  87.     public Connection getConnection() {
  88.         try {
  89.             Class.forName("com.mysql.jdbc.Driver");
  90.             connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/Emp", "root", "root");
  91.         } catch (Exception e) {
  92.             System.out.println(e);
  93.  
  94.         }
  95.         return connection;
  96.     }
  97.  
  98.     public ArrayList getList() {
  99.         try {
  100.             usersList = new ArrayList();
  101.             connection = getConnection();
  102.             Statement stmt = connection.createStatement();
  103.  
  104.             ResultSet rs = stmt.executeQuery("select * from users");
  105.             while (rs.next()) {
  106.                 User user = new User();
  107.                 user.setId(rs.getInt("id"));
  108.                 user.setName(rs.getString("name"));
  109.                 user.setEmail(rs.getString("email"));
  110.                 user.setPassword(rs.getString("password"));
  111.                 user.setGender(rs.getString("gender"));
  112.                 user.setAddress(rs.getString("address"));
  113.                 usersList.add(user);
  114.  
  115.             }
  116.             connection.close();
  117.            
  118.         } catch (Exception e) {
  119.             System.out.println(e);
  120.  
  121.         }
  122.         return usersList;
  123.     }
  124.     public String delete(int id)
  125.     {
  126.         System.out.println(id);
  127.         connection = getConnection();
  128.         try {
  129.                     Statement stmt = connection.createStatement();
  130.  
  131.             stmt.executeUpdate("delete from users where id="+id);
  132.         } catch (SQLException ex) {
  133.             Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
  134.         }
  135.         return "index";
  136.     }
  137.    public String edit(int id)
  138.    {
  139.        User user = null ;
  140.        connection = getConnection();
  141.         try {
  142.             Statement stmt = connection.createStatement();
  143.             ResultSet rs = stmt.executeQuery("select * from users where id="+id);
  144.             rs.next();
  145.             user = new User();
  146.             user.setId(rs.getInt("id"));
  147.             user.setName(rs.getString("name"));
  148.             user.setEmail(rs.getString("email"));
  149.             user.setGender(rs.getString("gender"));
  150.             user.setAddress(rs.getString("address"));
  151.             user.setPassword(rs.getString("password"));
  152.             System.out.println(rs.getString("password"));
  153.            
  154.             sessionMap.put("editedUser", user);
  155.             connection.close();
  156.            
  157.         } catch (SQLException ex) {
  158.             Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
  159.         }
  160.        
  161.        return "edit.xhtml?faces-redirect=true";
  162.                
  163.    }
  164.  
  165. }
Add Comment
Please, Sign In to add comment