Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6.  
  7.  
  8. /*
  9. * This class sets up a connection with the database storing information about
  10. * the gym. This is the class where all database interaction occurs.
  11. * This is the model part of the program.
  12. *
  13. */
  14. public class Database {
  15.  
  16. private Connection connection = null; //?
  17.  
  18. public Database(){
  19.  
  20. }
  21.  
  22. public void connection(){
  23.  
  24. String dbname = "m_16_1102394m"; //Database name.
  25. String username = "m_16_1102394m"; //My username.
  26. String password = "1102394m"; //My password.
  27.  
  28. try
  29. {
  30. //Use DriverMaanger class to get a connection then pass the details for the connection.
  31. connection = DriverManager.getConnection("jdbc:postgresql://yacata.dcs.gla.ac.uk:5432/"+dbname,username,password);
  32.  
  33. }
  34. catch(SQLException e)
  35. {
  36. System.err.println("Connection Failed!");
  37. e.printStackTrace();
  38. return;
  39. }
  40. //Check connection is not null as this won't be caught in catch block.
  41. if(connection != null)
  42. {
  43. System.out.println("Connection successful");
  44. }
  45. else
  46. {
  47. System.err.println("Failed to make connection");
  48. }
  49. }
  50.  
  51.  
  52. /*
  53. * Method to get the names of all the gym courses.
  54. *
  55. * @return An array containing the names of the gym course.
  56. */
  57. public String[] getGymCourse()
  58. {
  59. String[] list = new String[10];
  60. Statement stmt = null;
  61. String query = "SELECT * FROM gymcourse";
  62. try
  63. {
  64. stmt = connection.createStatement();
  65. ResultSet rs = stmt.executeQuery(query);
  66.  
  67. int i=0;
  68. while (rs.next())
  69. {
  70. String course_name = rs.getString("name");
  71. list[i] = course_name;
  72. i++;
  73. }
  74. }
  75. catch (SQLException e)
  76. {
  77. e.printStackTrace();
  78. System.err.println("error executing query " + query);
  79. }
  80. return list;
  81. }
  82.  
  83.  
  84. public int getBookings(String course)
  85. {
  86. Statement stmt = null;
  87. int count=0;
  88. //String query = "UPDATE instructors SET fname='finding', lname='nemo' WHERE instructorid=2";
  89. String query = "select * from gymcourse as g inner join coursebookings as c on g.courseid = c.course where name='"+course+"'";
  90. try{
  91. stmt = connection.createStatement();
  92. ResultSet rs = stmt.executeQuery(query);
  93.  
  94. //the next method of ResultSet allows you to iterate through the results
  95. while (rs.next())
  96. {
  97. int book = rs.getInt("member");
  98. count+=1;
  99. }
  100. }
  101.  
  102. catch (SQLException e )
  103. {
  104. e.printStackTrace();
  105. System.err.println("error executing query " + query);
  106. }
  107. return count;
  108. }
  109.  
  110. public int getCapacity(String course)
  111. {
  112. Statement stmt = null;
  113. int c=-1;
  114. //String query = "UPDATE instructors SET fname='finding', lname='nemo' WHERE instructorid=2";
  115. String query = "SELECT capacity FROM gymcourse WHERE name='"+course+"'";
  116. try{
  117. stmt = connection.createStatement();
  118. ResultSet rs = stmt.executeQuery(query);
  119.  
  120. //the next method of ResultSet allows you to iterate through the results
  121. while (rs.next())
  122. {
  123. c = rs.getInt("capacity");
  124. }
  125. }
  126.  
  127. catch (SQLException e )
  128. {
  129. e.printStackTrace();
  130. System.err.println("error executing query " + query);
  131. }
  132. return c;
  133. }
  134.  
  135. public String[][] viewMembers(String course)
  136. {
  137.  
  138. String[][] bookings = new String[20][2];
  139. Statement stmt = null;
  140. int count = 0;
  141. //String query = "UPDATE instructors SET fname='finding', lname='nemo' WHERE instructorid=2";
  142. String query = "SELECT * FROM gymcourse AS g INNER JOIN coursebookings AS c ON c.course=g.courseid INNER JOIN members AS m ON c.member=m.memberid WHERE name='"+course+"'";;
  143. try{
  144. stmt = connection.createStatement();
  145. ResultSet rs = stmt.executeQuery(query);
  146.  
  147. //the next method of ResultSet allows you to iterate through the results
  148. while (rs.next())
  149. {
  150. String member_name = rs.getString("fname");
  151. String member_lname = rs.getString("lname");
  152. String member = member_name+" "+member_lname;
  153. int id = rs.getInt("memberid");
  154. bookings[count][0]= member;
  155. bookings[count][1]=""+id;
  156. System.out.println(member);
  157. count++;
  158.  
  159. }
  160. }
  161.  
  162. catch (SQLException e )
  163. {
  164. e.printStackTrace();
  165. System.err.println("error executing query " + query);
  166. }
  167. return bookings;
  168.  
  169. }
  170.  
  171. public int getCourseID(String course)
  172. {
  173. Statement stmt = null;
  174. int courseid = -1;
  175. //String query = "UPDATE instructors SET fname='finding', lname='nemo' WHERE instructorid=2";
  176. String query = "SELECT * FROM gymcourse WHERE name='"+course+"'";;
  177. try{
  178. stmt = connection.createStatement();
  179. ResultSet rs = stmt.executeQuery(query);
  180.  
  181. //the next method of ResultSet allows you to iterate through the results
  182. while (rs.next())
  183. {
  184. courseid = rs.getInt("courseid");
  185.  
  186. }
  187. }
  188.  
  189. catch (SQLException e )
  190. {
  191. e.printStackTrace();
  192. System.err.println("error executing query " + query);
  193. }
  194. return courseid;
  195. }
  196.  
  197. public void addBooking(int courseid, String id)
  198. {
  199. int memid = Integer.valueOf(id);
  200. Statement stmt = null;
  201. String instructor ="";
  202. //String query = "UPDATE instructors SET fname='finding', lname='nemo' WHERE instructorid=2";
  203. String update = "INSERT INTO coursebookings VALUES("+memid+", "+courseid+")";
  204. try{
  205. stmt = connection.createStatement();
  206. int rs = stmt.executeUpdate(update);
  207.  
  208. //the next method of ResultSet allows you to iterate through the results
  209.  
  210. }
  211. catch (SQLException e )
  212. {
  213. e.printStackTrace();
  214. System.err.println("error executing update " + update);
  215. }
  216.  
  217. }
  218.  
  219. public boolean checkCapacity(String course)
  220. {
  221. Statement stmt = null;
  222. int count = 0;
  223. //String query = "UPDATE instructors SET fname='finding', lname='nemo' WHERE instructorid=2";
  224. String query = "SELECT member FROM gymcourse AS g INNER JOIN coursebookings AS c ON c.course=g.courseid INNER JOIN members AS m ON c.member=m.memberid WHERE name='"+course+"'";
  225. try{
  226. stmt = connection.createStatement();
  227. ResultSet rs = stmt.executeQuery(query);
  228.  
  229. //the next method of ResultSet allows you to iterate through the results
  230. while (rs.next())
  231. {
  232. count+=1;
  233.  
  234.  
  235. }
  236. }
  237.  
  238. catch (SQLException e )
  239. {
  240. e.printStackTrace();
  241. System.err.println("error executing query " + query);
  242. }
  243. if(count == getCapacity(course))
  244. return false;
  245. else
  246. return true;
  247. }
  248.  
  249.  
  250. public boolean checkAlreadyBooked(String id, String course)
  251. {
  252. int check = Integer.valueOf(id);
  253. Statement stmt = null;
  254. int count = 0;
  255. //String query = "UPDATE instructors SET fname='finding', lname='nemo' WHERE instructorid=2";
  256. String query = "SELECT member FROM gymcourse AS g INNER JOIN coursebookings AS c ON c.course=g.courseid INNER JOIN members AS m ON c.member=m.memberid WHERE name='"+course+"'";
  257. try{
  258. stmt = connection.createStatement();
  259. ResultSet rs = stmt.executeQuery(query);
  260.  
  261. //the next method of ResultSet allows you to iterate through the results
  262. while (rs.next())
  263. {
  264. if(check == rs.getInt("member"))
  265. return false;
  266.  
  267.  
  268. }
  269. }
  270.  
  271. catch (SQLException e )
  272. {
  273. e.printStackTrace();
  274. System.err.println("error executing query " + query);
  275. }
  276.  
  277. return true;
  278. }
  279.  
  280. public boolean checkValidMember(String member)
  281. {
  282. int id = Integer.valueOf(member);
  283. Statement stmt = null;
  284. //String query = "UPDATE instructors SET fname='finding', lname='nemo' WHERE instructorid=2";
  285. String query = "SELECT memberid FROM members";
  286. try{
  287. stmt = connection.createStatement();
  288. ResultSet rs = stmt.executeQuery(query);
  289.  
  290. //the next method of ResultSet allows you to iterate through the results
  291. while (rs.next())
  292. {
  293. if(id == rs.getInt("memberid"))
  294. return true;
  295.  
  296.  
  297. }
  298. }
  299.  
  300. catch (SQLException e )
  301. {
  302. e.printStackTrace();
  303. System.err.println("error executing query " + query);
  304. }
  305.  
  306. return false;
  307. }
  308.  
  309.  
  310. public String getInstructor(String course)
  311. {
  312. Statement stmt = null;
  313. String instructor ="";
  314. //String query = "UPDATE instructors SET fname='finding', lname='nemo' WHERE instructorid=2";
  315. String query = "SELECT fname, lname FROM instructors AS i INNER JOIN gymcourse AS g ON i.instructorid=g.instructor WHERE name='"+course+"'";
  316. try{
  317. stmt = connection.createStatement();
  318. ResultSet rs = stmt.executeQuery(query);
  319.  
  320. //the next method of ResultSet allows you to iterate through the results
  321. while (rs.next())
  322. {
  323. String instructor_name = rs.getString("fname");
  324. String instructor_lname = rs.getString("lname");
  325. instructor = instructor_name+" "+instructor_lname;
  326. }
  327. }
  328.  
  329. catch (SQLException e )
  330. {
  331. e.printStackTrace();
  332. System.err.println("error executing query " + query);
  333. }
  334. return instructor;
  335. }
  336.  
  337. public void close()
  338. {
  339. try
  340. {
  341. connection.close();
  342. System.out.println("Connection closed");
  343. }
  344. catch(SQLException e)
  345. {
  346. e.printStackTrace();
  347. System.out.println("Connection could not be closed - SQL exception");
  348. }
  349. }
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement