Guest User

Untitled

a guest
Jan 6th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. private static UserAccountList instance;
  2. private ArrayList<user> userList;
  3. user newUser = null;
  4. int currentUser;
  5.  
  6. protected UserAccountList() throws IOException {
  7. this.userList = new ArrayList<>();
  8. readDatabase();
  9. }
  10.  
  11. public static void main(String[] args) throws IOException{
  12. ArrayList<user> userList1 = new UserAccountList().getUserList();
  13. for(int i=0;i<userList1.size();i++){
  14. System.out.println(userList1.get(i).getUsername());
  15. }
  16. }
  17. public ArrayList getUserList() throws IOException {
  18. return this.userList;
  19.  
  20. }
  21.  
  22. public static UserAccountList getInstance() throws IOException {
  23. if (instance == null) {
  24. instance = new UserAccountList();
  25. }
  26. return instance;
  27. }
  28.  
  29.  
  30.  
  31. private void readDatabase() throws IOException {
  32. PreparedStatement ps;
  33. ResultSet rs;
  34.  
  35. String query = "SELECT * FROM `user`" ;
  36.  
  37. try{
  38. ps = MyConnection.getConnection().prepareStatement(query);
  39.  
  40.  
  41. rs = ps.executeQuery();
  42.  
  43. while(rs.next() != false){
  44.  
  45. String uname = rs.getString("username");
  46. String pass = rs.getString("password");
  47. String fname = rs.getString("firstname");
  48. String lname = rs.getString("lastname");
  49. String email = rs.getString("email");
  50. String uni = rs.getString("university");
  51. String course = rs.getString("course");
  52.  
  53. user newUser = new user(fname, lname, uname, pass, email, uni, course);
  54. addUser(newUser);
  55. }
  56.  
  57. }
  58.  
  59. catch (SQLException ex) {
  60. Logger.getLogger(FXMLRegistrationController.class.getName()).log(Level.SEVERE, null, ex);
  61. }
  62. }
  63.  
  64.  
  65. public void addUser(user newUser){
  66. userList.add(newUser);
  67. return;
  68. }
Add Comment
Please, Sign In to add comment