Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.ArrayList;
  3.  
  4. import javax.security.sasl.Sasl;
  5. import javax.swing.JComboBox;
  6. import javax.swing.JOptionPane;
  7. import javax.swing.JTable;
  8.  
  9. public class JDBC_Action
  10. {
  11. //instance variables
  12. private Connection connection = null;
  13. //noOfCourses
  14.  
  15. public JDBC_Action()
  16. {
  17. String dbname = "m_16_2284062p";
  18. String username = "m_16_2284062p";
  19. String password = "2284062p";
  20.  
  21.  
  22.  
  23.  
  24. try
  25. {
  26. connection = DriverManager.getConnection("jdbc:postgresql://yacata.dcs.gla.ac.uk:5432/" + dbname, username, password);
  27. }
  28. catch (SQLException e)
  29. {
  30. System.err.println("Connection Failed!");
  31. e.printStackTrace();
  32. return;
  33. }
  34. if (connection != null)
  35. {
  36. System.out.println("Connection successful");
  37. }
  38. else
  39. {
  40. System.err.println("Failed to make connection!");
  41. }
  42. }
  43.  
  44.  
  45. //getInstructor Method
  46. public String getInstructor(String choice)
  47. {
  48. String course = choice;
  49. String instructor = "";
  50. Statement stmt = null;
  51. String query = " SELECT fname, sname FROM instructors INNER JOIN courses ON instructors.instructornumber = courses.courseinstructor WHERE courses.name = '" + course + "'";
  52.  
  53.  
  54. try
  55. {
  56. stmt = connection.createStatement();
  57. ResultSet rs = stmt.executeQuery(query);
  58. //the next method of ResultSet allows you to iterate through the results
  59. while (rs.next())
  60. {
  61. String f_name = rs.getString("fname");
  62. String s_name = rs.getString("sname");
  63. instructor = f_name + " " + s_name;
  64. }
  65. }
  66. catch (SQLException e)
  67. {
  68. e.printStackTrace();
  69. System.err.println("error executing query " + query);
  70. }
  71. return instructor;
  72. }
  73.  
  74. //getMembers Method
  75.  
  76. //getCourseCapacity Method
  77. public int getCapacity(String choice)
  78. {
  79. String course = choice;
  80. int capacity = 0;
  81. Statement stmt = null;
  82. String query = "SELECT capacity FROM courses WHERE name = '" + course + "'";
  83.  
  84. try
  85. {
  86. stmt = connection.createStatement();
  87. ResultSet rs = stmt.executeQuery(query);
  88.  
  89. while(rs.next())
  90. {
  91. capacity = rs.getInt("capacity");
  92. }
  93. }
  94. catch (SQLException e)
  95. {
  96. e.printStackTrace();
  97. }
  98. return capacity;
  99. }
  100.  
  101. //getPlacesBooked Method
  102. public int getBooked(String choice)
  103. {
  104. String course = choice;
  105. int booked = 0;
  106. Statement stmt = null;
  107. String query = "SELECT COUNT (member) FROM coursebooking INNER JOIN courses ON courses.courseid = coursebooking.course WHERE courses.name = '" + course + "'";
  108.  
  109. try
  110. {
  111. stmt = connection.createStatement();
  112. ResultSet rs = stmt.executeQuery(query);
  113.  
  114. while(rs.next())
  115. {
  116. booked = rs.getInt(1);
  117. }
  118. }
  119. catch (SQLException e)
  120. {
  121. e.printStackTrace();
  122. }
  123. System.err.println(booked);
  124. return booked;
  125. }
  126.  
  127.  
  128.  
  129. //CHeck if Member is already booked on course
  130. public boolean checkDuplicate(int memID, String choice)
  131. {
  132. String course = choice;
  133. int idBook = memID;
  134. Statement stmt = null;
  135. String checkUnique = "SELECT member FROM coursebooking INNER JOIN courses ON courses.courseid = coursebooking.course WHERE courses.name = '" + course + "'";
  136. int [] idOnCourse = new int [20];
  137. int i = 0;
  138. boolean duplicate = false;
  139.  
  140.  
  141. try
  142. {
  143. stmt = connection.createStatement();
  144. ResultSet rs = stmt.executeQuery(checkUnique);
  145.  
  146. while (rs.next())
  147. {
  148. idOnCourse [i] = rs.getInt("member");
  149.  
  150. if (idBook == idOnCourse[i]) //check if member ID is already booked on course
  151. {
  152.  
  153. JOptionPane.showMessageDialog(null, "Member Already Booked on this Course",
  154. "Double Booking Attempt", JOptionPane.ERROR_MESSAGE);
  155. duplicate = true;
  156. }
  157. i++;
  158. }
  159.  
  160. }
  161. catch (SQLException e)
  162. {
  163. e.printStackTrace();
  164. }
  165. System.out.println("book ID" + idBook);
  166. System.err.println(duplicate);
  167. return duplicate;
  168. }
  169.  
  170.  
  171.  
  172. public void BookCourse(int memID, String choice)
  173. {
  174. String course = choice;
  175. int idBook = memID;
  176. Statement stmt = null;
  177. String insertMem = "INSERT INTO coursebooking (member, course) SELECT " + idBook + ", courseid FROM courses WHERE name = '" + course + "'";
  178. try
  179. {
  180. stmt = connection.createStatement();
  181.  
  182. stmt.executeUpdate(insertMem);
  183.  
  184. }
  185. catch (SQLException e)
  186. {
  187. e.printStackTrace();
  188. }
  189. }
  190.  
  191.  
  192.  
  193. //method to get courses from database into jcombobox
  194. public String[] getCourse()
  195. {
  196. Statement stmt = null;
  197. String [] courseName = new String [9];
  198. String query = "SELECT name FROM courses";
  199. int i = 0;
  200.  
  201. try
  202. {
  203. stmt = connection.createStatement();
  204. ResultSet rs = stmt.executeQuery(query);
  205.  
  206. while (rs.next())
  207.  
  208. {
  209. courseName [i] = rs.getString("name");
  210. System.out.println(courseName);
  211. i ++;
  212. }
  213. }
  214. catch (Exception e)
  215. {
  216. JOptionPane.showMessageDialog(null, e);
  217. }
  218. return courseName;
  219. }
  220.  
  221.  
  222. //check if person who attempts booking is a member
  223. public boolean isMember (int memID)
  224. {
  225. int idBook = memID;
  226. boolean member = false;
  227. String query = "SELECT memid FROM members";
  228. int [] gymMembers = new int [12];
  229. int i = 0;
  230. Statement stmt = null;
  231.  
  232.  
  233. try
  234. {
  235. stmt = connection.createStatement();
  236. ResultSet rs = stmt.executeQuery(query);
  237.  
  238. while (rs.next())
  239. {
  240. gymMembers [i] = rs.getInt("memid");
  241.  
  242. if (idBook == gymMembers[i]) //check if member ID is already booked on course
  243. {
  244. member = true;
  245. }
  246. i++;
  247. }
  248.  
  249. }
  250. catch (SQLException e)
  251. {
  252. e.printStackTrace();
  253. }
  254. return member;
  255. }
  256.  
  257. //check if course can still be booked or if it is already full
  258. public boolean emptySpaces (String choice)
  259. {
  260. String course = choice;
  261. boolean available = false;
  262.  
  263. int booked = getBooked(course);
  264. int capacity = getCapacity(course);
  265.  
  266. if (booked < capacity)
  267. {
  268. available = true;
  269. }
  270. return available;
  271. }
  272.  
  273.  
  274. public void close()
  275. {
  276. try
  277. {
  278. connection.close();
  279. }
  280. catch (SQLException e)
  281. {
  282. e.printStackTrace();
  283. }
  284. }
  285.  
  286. //Data for JTable
  287. public String[][] getTable(String choice)
  288. {
  289. Statement stmt = null;
  290. String course = choice;
  291. String query = "SELECT fname, sname, memid FROM members AS m "
  292. + "INNER JOIN coursebooking as cb ON m.memid = cb.member "
  293. + "INNER JOIN courses AS c ON cb.course = c.courseid "
  294. + "WHERE c.name = '" + course + "'";
  295.  
  296. String[][] tabledata = {{}};
  297. int i = 0;
  298. int numMem = 0;
  299.  
  300. try
  301. {
  302. stmt = connection.createStatement();
  303. ResultSet rs = stmt.executeQuery(query);
  304.  
  305. String numOnCourse = "SELECT COUNT (member) FROM coursebooking "
  306. + "INNER JOIN courses ON courses.courseid = coursebooking.course "
  307. + "WHERE courses.name = '" + course + "'";
  308. stmt = connection.createStatement();
  309. ResultSet rs2 = stmt.executeQuery(numOnCourse);
  310.  
  311. while (rs.next())
  312. {
  313. //get number of members on course
  314. numMem = rs2.getInt(1);
  315.  
  316. for (i = 0; i <= numMem; i++)
  317. {
  318. for (int j = 0; j< 3; j++)
  319. {
  320. tabledata [i][j] = rs.getString("fname");
  321. tabledata [i][j] = rs.getString("sname");
  322. tabledata [i][j] = rs.getString("memid");
  323. }
  324. }
  325. }
  326. }
  327. catch (SQLException e)
  328. {
  329. e.printStackTrace();
  330. }
  331.  
  332. return tabledata;
  333. }
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement