Guest User

UserHelper

a guest
Apr 28th, 2016
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.05 KB | None | 0 0
  1. package databaseHelpers;
  2.  
  3.  
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. import javafx.collections.FXCollections;
  9. import javafx.collections.ObservableList;
  10. import userTypes.USER;
  11.  
  12. public class UserHelper extends DatabaseHelper {
  13.  
  14.     /**
  15.      * Creates an ObservableList containing every user in the user table
  16.      * @return
  17.      */
  18.     public ObservableList<USER> getAllUsers() {
  19.         ObservableList<USER> list = FXCollections.observableArrayList();
  20.         try {
  21.             con = DriverManager.getConnection(url, username, dbPassword);
  22.             Statement stmt = con.createStatement();
  23.             rs = stmt.executeQuery("SELECT * FROM user");
  24.  
  25.             while (rs.next()) {
  26.                 // assigning the user_id to the the id pulled from database
  27.                 int user_id = rs.getInt("id");
  28.                 // assigning the first name to the the first_name pulled from
  29.                 // database
  30.                 String firstName = rs.getString("first_name");
  31.                 // assigning the last name to the the last_name pulled from
  32.                 // database
  33.                 String lastName = rs.getString("last_name");
  34.  
  35.                 String userType = rs.getString("user_type").toUpperCase();
  36.  
  37.                 String userPassword = rs.getString("password");
  38.  
  39.                 //Not new record, so it cannot be true when checking login
  40.                 boolean newRecord = false;
  41.                
  42.             list.add(new USER(user_id, firstName, lastName, userPassword, userType, newRecord));
  43.            
  44.             }
  45.  
  46.         } catch (SQLException e) {
  47.             e.printStackTrace();
  48.         }
  49.        
  50.         return list;
  51.  
  52.     }
  53.    
  54.     /**
  55.      * Gets user details based on ID
  56.      * @param userID
  57.      * @return
  58.      */
  59.     public USER getUser(int userID) {
  60.         USER user = null;
  61.         try {
  62.             con = DriverManager.getConnection(url, username, dbPassword);
  63.             Statement stmt = con.createStatement();
  64.             rs = stmt.executeQuery("SELECT * FROM user WHERE id = " + userID);
  65.  
  66.             while (rs.next()) {
  67.                 // assigning the user_id to the the id pulled from database
  68.                 int user_id = rs.getInt("id");
  69.                 // assigning the first name to the the first_name pulled from
  70.                 // database
  71.                 String firstName = rs.getString("first_name");
  72.                 // assigning the last name to the the last_name pulled from
  73.                 // database
  74.                 String lastName = rs.getString("last_name");
  75.                
  76.                 String userPassword = rs.getString("password");
  77.                
  78.                 String userType = rs.getString("user_type").toUpperCase();
  79.  
  80.                
  81.  
  82.                 //Not new record, so it cannot be true when checking login
  83.                 boolean newRecord = false;
  84.                
  85.                 user = new USER(user_id, firstName, lastName, userPassword, userType, newRecord);
  86.            
  87.             }
  88.  
  89.         } catch (SQLException e) {
  90.             e.printStackTrace();
  91.         }
  92.        
  93.         return user;
  94.  
  95.     }
  96.    
  97.  
  98.     /**
  99.      * Adds a user to the user table
  100.      * @param fName
  101.      * @param sName
  102.      * @param password
  103.      */
  104.     public void addUser(String fName, String sName, String password, String type) {
  105.         executeQuery("INSERT INTO user VALUES(null, '" + fName + "', '" + sName + "', '" + password + "', '" + type +"')");
  106.        
  107.    
  108.     }
  109.    
  110.     /**
  111.      * Get the latest User ID
  112.      * @return
  113.      */
  114.     public int getLatestUser(){
  115.         int latestUserID = 0;
  116.         try {
  117.             con = DriverManager.getConnection(url, username, dbPassword);
  118.             Statement stmt = con.createStatement();
  119.             rs = stmt.executeQuery("SELECT MAX(id) FROM user");
  120.             rs.next();
  121.             latestUserID = rs.getInt(1);
  122.  
  123.         } catch (SQLException e) {
  124.             e.printStackTrace();
  125.         }
  126.        
  127.         return latestUserID;
  128.  
  129.     }
  130.    
  131.     /**
  132.      * Creates an entry in the enrols table for the newest added student under the specified teacher id
  133.      * @param teacherID
  134.      */
  135.     public void logEnrolment(int teacherID, int studentID){
  136.         executeQuery("INSERT INTO enrols VALUES("+teacherID+", "+studentID+")");
  137.     }
  138.    
  139.    
  140.  
  141.     /**
  142.      * Removes a user from the user table
  143.      * @param userID
  144.      */
  145.     public void removeUser(int userID) {
  146.         executeQuery("DELETE FROM user WHERE id = +" + userID);
  147.     }
  148.    
  149.     /**
  150.      * Updates a user in the user table
  151.      * @param userID
  152.      */
  153.     public void updateUser(int userID, String firstName, String lastName, String password){
  154.         executeQuery("UPDATE user SET first_name = '"+firstName+"', "+ "last_name = '"+lastName+"', password = '"+password+"' WHERE id = +" + userID);
  155.     }
  156.  
  157.  
  158. }
Add Comment
Please, Sign In to add comment