Advertisement
Guest User

Untitled

a guest
Jan 11th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.46 KB | None | 0 0
  1. package models;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.Statement;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import java.util.HashMap;
  10.  
  11. public class DBManager {
  12.  
  13. public void deleteZooKeeper (ZooKeeper zooKeeper)
  14. {
  15. try
  16. {
  17. Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
  18. Connection conn = DriverManager.getConnection("jdbc:ucanaccess://H:\\James hood object oriented programing\\Example project\\ZooSystem james version\\Data\\Database1.accdb");
  19. Statement stmt = conn.createStatement();
  20. stmt.executeUpdate("DELETE FROM ZooKeepers WHERE EmailAddress = '" + zooKeeper.getEmailAddress() + "'");
  21. conn.close();
  22. }
  23. catch(Exception ex)
  24. {
  25. String message = ex.getMessage();
  26. }
  27. }
  28.  
  29.  
  30.  
  31. public void deleteAnimal (Animal animal)
  32. {
  33. try
  34. {
  35. Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
  36. Connection conn = DriverManager.getConnection("jdbc:ucanaccess://H:\\James hood object oriented programing\\Example project\\ZooSystem james version\\Data\\Database1.accdb");
  37. Statement stmt = conn.createStatement();
  38. stmt.executeUpdate("DELETE FROM Animals WHERE AnimalID = '" + animal.getAnimalId() + "'");
  39. conn.close();
  40. }
  41. catch(Exception ex)
  42. {
  43. String message = ex.getMessage();
  44. }
  45. }
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. public void updateZooKeeper(ZooKeeper newZooKeeper)
  53. {
  54. try
  55. {
  56. Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
  57. Connection conn = DriverManager.getConnection("jdbc:ucanaccess://H:\\James hood object oriented programing\\Example project\\ZooSystem james version\\Data\\Database1.accdb");
  58. Statement stmt = conn.createStatement();
  59. stmt.executeUpdate("UPDATE ZooKeepers SET PersonName = '" + newZooKeeper.getName() + "', " +
  60. "Age = '" + newZooKeeper.getAge() + "', Password = '" + newZooKeeper.getPassword() + "', " +
  61. "DateHired = '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newZooKeeper.getDateHired()) + "', " +
  62. "Salary = '" + newZooKeeper.getSalary() + "', TelephoneNo = '" + newZooKeeper.getTelephoneNo() + "' " +
  63. "WHERE EmailAddress = '" + newZooKeeper.getEmailAddress() + "'");
  64. conn.close();
  65.  
  66.  
  67. }
  68. catch(Exception ex)
  69. {
  70. String message = ex.getMessage();
  71. }
  72.  
  73. }
  74.  
  75. public void updateAnimal(Animal newAnimal)
  76. {
  77. String waterType = "";
  78. String canFly = "NULL";
  79.  
  80. if(newAnimal.getClass().getName().equals("models.Fish"))
  81. {
  82. Fish newFish = (Fish)newAnimal;
  83. waterType = newFish.getWaterType();
  84. }
  85. else
  86. {
  87. Bird newBird = (Bird)newAnimal;
  88. canFly = String.valueOf(newBird.getFlies());
  89. }
  90.  
  91. try
  92. {
  93. Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
  94. Connection conn = DriverManager.getConnection("jdbc:ucanaccess://H:\\James hood object oriented programing\\Example project\\ZooSystem james version\\Data\\Database1.accdb");
  95. Statement stmt = conn.createStatement();
  96. stmt.executeUpdate("UPDATE Animals SET AnimalName = '" + newAnimal.getName() + "', " +
  97. "Gender = '" + newAnimal.getGender() + "', Type = '" + newAnimal.getType() + "', " +
  98. "Age = '" + newAnimal.getAge() + "', Location = '" + newAnimal.getLocation() + "', " +
  99. "WaterType ='" + waterType + "', CanFly = " + canFly +
  100. " WHERE AnimalID = '" + newAnimal.getAnimalId() + "'");
  101. conn.close();
  102.  
  103.  
  104. }
  105. catch(Exception ex)
  106. {
  107. String message = ex.getMessage();
  108. }
  109. }
  110.  
  111.  
  112.  
  113.  
  114. public HashMap<Integer, Animal> loadAnimals()
  115. {
  116. HashMap<Integer, Animal> animals = new HashMap();
  117.  
  118. try
  119. {
  120. Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
  121. Connection conn = DriverManager.getConnection("jdbc:ucanaccess://H:\\James hood object oriented programing\\Example project\\ZooSystem james version\\Data\\Database1.accdb");
  122. Statement stmt = conn.createStatement();
  123.  
  124. ResultSet rs = stmt.executeQuery("SELECT * FROM Animals");
  125. while (rs.next())
  126. {
  127. int animalId = rs.getInt("animalID");
  128. String animalName = rs.getString("AnimalName");
  129. char gender = rs.getString("Gender").charAt(0);
  130. String type = rs.getString("Type");
  131. int age = rs.getInt("Age");
  132. String location = rs.getString("Location");
  133. String waterType = rs.getString("WaterType");
  134. boolean availability = rs.getBoolean("Availability");
  135. double cost = rs.getDouble("Cost");
  136.  
  137.  
  138. if (waterType != null && !waterType.isEmpty())
  139. {
  140. Fish loadedFish = new Fish(animalName, gender, type, age, location, animalId, waterType, availability, cost);
  141. animals.put(animalId, loadedFish);
  142. }
  143. else
  144. {
  145. //String nameIn, char genderIn, String typeIn, int ageIn, String locationIn, boolean fliesIn
  146. boolean canFly = rs.getBoolean("canFly");
  147. Bird loadedBird = new Bird(animalName, gender, type, age, location, animalId,canFly, availability, cost);
  148. animals.put(animalId, loadedBird);
  149. }
  150. }
  151. conn.close();
  152.  
  153.  
  154. }
  155. catch (Exception ex)
  156. {
  157. String message = ex.getMessage();
  158. }
  159. finally
  160. {
  161. return animals;
  162. }
  163. }
  164.  
  165.  
  166.  
  167.  
  168. public void addAnimal(Animal newAnimal)
  169. {
  170. String waterType = "";
  171. String canFly = "NULL";
  172.  
  173. if(newAnimal.getClass().getName().equals("models.Fish"))
  174. {
  175. Fish newFish = (Fish)newAnimal;
  176. waterType = newFish.getWaterType();
  177. }
  178. else
  179. {
  180. Bird newBird = (Bird)newAnimal;
  181. canFly = String.valueOf(newBird.getFlies());
  182. }
  183.  
  184. try
  185. {
  186. Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
  187. Connection conn = DriverManager.getConnection("jdbc:ucanaccess://H:\\James hood object oriented programing\\Example project\\ZooSystem james version\\Data\\Database1.accdb");
  188. Statement stmt = conn.createStatement();
  189. stmt.executeUpdate("INSERT INTO Animals (AnimalName, Gender, Type, Age, Location, WaterType, CanFly) VALUES " +
  190. "('" + newAnimal.getName() + "','" + newAnimal.getGender() + "','" + newAnimal.getType() + "','" + newAnimal.getAge() +
  191. "','" + newAnimal.getLocation() + "','" + waterType + "'," + canFly + ")");
  192. conn.close();
  193.  
  194.  
  195. }
  196. catch(Exception ex)
  197. {
  198. String message = ex.getMessage();
  199. }
  200. }
  201.  
  202.  
  203.  
  204.  
  205. public boolean registerZooKeeper(ZooKeeper newKeeper)
  206. {
  207. try
  208. {
  209. Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
  210. Connection conn = DriverManager.getConnection("jdbc:ucanaccess://H:\\James hood object oriented programing\\Example project\\ZooSystem james version\\Data\\Database1.accdb");
  211. Statement stmt = conn.createStatement();
  212. stmt.executeUpdate("INSERT INTO ZooKeepers (EmailAddress, PersonName, Age, Password, DateHired, Salary, TelephoneNo) VALUES " +
  213. "('" + newKeeper.getEmailAddress() + "','" + newKeeper.getName() + "','" + newKeeper.getAge() + "','" + newKeeper.getPassword() +
  214. "','" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newKeeper.getDateHired()) + "','" + newKeeper.getSalary() + "','" + newKeeper.getTelephoneNo() + "')" );
  215. conn.close();
  216. return true;
  217.  
  218. }
  219. catch(Exception ex)
  220. {
  221. String message = ex.getMessage();
  222. return false;
  223.  
  224. }
  225. }
  226.  
  227.  
  228.  
  229. public Admin adminLogin(String emailAddress, String password){
  230.  
  231. HashMap<String, Admin> admins = loadAdmins();
  232.  
  233. if(admins.containsKey(emailAddress)){
  234.  
  235. Admin foundAdmin = admins.get(emailAddress);
  236.  
  237. if (foundAdmin.getPassword().equals(password)){
  238.  
  239. return foundAdmin;
  240. }
  241. }
  242. //if not found return null
  243. return null;
  244. }
  245.  
  246. public ZooKeeper zooKeeperLogin(String emailAddress, String password){
  247.  
  248. HashMap<String, ZooKeeper> zooKeepers = loadZooKeepers();
  249.  
  250. if(zooKeepers.containsKey(emailAddress)){
  251.  
  252. ZooKeeper foundZooKeeper = zooKeepers.get(emailAddress);
  253.  
  254. if (foundZooKeeper.getPassword().equals(password)){
  255.  
  256. return foundZooKeeper;
  257. }
  258. }
  259. //if not found return null
  260. return null;
  261. }
  262.  
  263.  
  264.  
  265.  
  266. public HashMap<String, ZooKeeper> loadZooKeepers() {
  267.  
  268. HashMap<String, ZooKeeper> zooKeepers = new HashMap<>();
  269.  
  270. try {
  271. Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
  272. Connection conn = DriverManager.getConnection("jdbc:ucanaccess://H:\\James hood object oriented programing\\Example project\\ZooSystem james version\\Data\\Database1.accdb");
  273. Statement stmt = conn.createStatement();
  274. ResultSet rs = stmt.executeQuery("SELECT * FROM ZooKeepers");
  275.  
  276. while (rs.next()) {
  277.  
  278. String emailAddress = rs.getString("EmailAddress");
  279. String personName = rs.getString("PersonName");
  280. int age = rs.getInt("age");
  281. String password = rs.getString("Password");
  282. Date dateHired = rs.getDate("DateHired");
  283. double salary = rs.getDouble("Salary");
  284. String telephoneNo = rs.getString("TelephoneNo");
  285.  
  286. ZooKeeper loadedZooKeeper = new ZooKeeper(personName, emailAddress, age, password, dateHired, salary, telephoneNo);
  287.  
  288. zooKeepers.put(emailAddress, loadedZooKeeper);
  289.  
  290. }
  291. conn.close();
  292. }
  293. catch (Exception ex)
  294. {
  295. String message = ex.getMessage();
  296.  
  297. }
  298.  
  299. finally
  300. {
  301. return zooKeepers;
  302. }
  303. }
  304.  
  305. public HashMap<String, Admin> loadAdmins() {
  306.  
  307. HashMap<String, Admin> admins = new HashMap<>();
  308.  
  309. try {
  310. Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
  311. Connection conn = DriverManager.getConnection("jdbc:ucanaccess://H:\\James hood object oriented programing\\Example project\\ZooSystem james version\\Data\\Database1.accdb");
  312. Statement stmt = conn.createStatement();
  313. ResultSet rs = stmt.executeQuery("SELECT * FROM Admins");
  314.  
  315. while (rs.next()) {
  316.  
  317. String emailAddress = rs.getString("EmailAddress");
  318. String personName = rs.getString("PersonName");
  319. int age = rs.getInt("age");
  320. String password = rs.getString("Password");
  321. Date dateHired = rs.getDate("DateHired");
  322. double salary = rs.getDouble("Salary");
  323. boolean superUser = rs.getBoolean("SuperUser");
  324.  
  325. Admin loadedAdmin = new Admin(personName, emailAddress, age, password, dateHired, salary, superUser);
  326.  
  327. admins.put(emailAddress, loadedAdmin);
  328.  
  329. }
  330. conn.close();
  331. }
  332. catch (Exception ex)
  333. {
  334. String message = ex.getMessage();
  335.  
  336. }
  337.  
  338. finally
  339. {
  340. return admins;
  341. }
  342. }
  343.  
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement