Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1.  
  2. import java.io.Serializable;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import javax.faces.bean.ManagedBean;
  11. import javax.faces.bean.SessionScoped;
  12.  
  13.  
  14. @ManagedBean
  15. @SessionScoped
  16. public class UserBean {
  17.  
  18.     private static final long serialVersionUID = 6081417964063918994L;
  19.  
  20.    
  21.     public List<User> getCars() throws ClassNotFoundException, SQLException {
  22.  
  23.         Connection connect = null;
  24.  
  25.         String url = "jdbc:mysql://localhost/pytania";
  26.  
  27.         String username = "root";
  28.         String password = "";
  29.  
  30.         try {
  31.  
  32.             Class.forName("com.mysql.jdbc.Driver");
  33.  
  34.             connect = DriverManager.getConnection(url, username, password);
  35.             // System.out.println("Connection established"+connect);
  36.  
  37.         } catch (SQLException ex) {
  38.             System.out.println("in exec");
  39.             System.out.println(ex.getMessage());
  40.         }
  41.  
  42.         List<User> users = new ArrayList<User>();
  43.         PreparedStatement pstmt = connect
  44.                 .prepareStatement("SELECT ID_user, login, password, rola, zablokowany, zmianahasla, wiadomosc, usuniety FROM users");
  45.         ResultSet rs = pstmt.executeQuery();
  46.  
  47.         while (rs.next()) {
  48.  
  49.             User user = new User();
  50.             user.setID_user(rs.getInt("ID_user"));
  51.             user.setLogin(rs.getString("login"));
  52.             user.setPassword(rs.getString("password"));
  53.                         user.setRola(rs.getString("rola"));
  54.             user.setUsuniety(rs.getInt("usuniety"));
  55.                         user.setZablokowany(rs.getInt("zablokowany"));
  56.             user.setZmianahasla(rs.getInt("zmianahasla"));
  57.                         user.setWiadomosc(rs.getInt("wiadomosc"));
  58.  
  59.             users.add(user);
  60.  
  61.         }
  62.  
  63.         // close resources
  64.         rs.close();
  65.         pstmt.close();
  66.         connect.close();
  67.  
  68.         return users;
  69.  
  70.     }
  71.    
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement