Advertisement
Guest User

DAL

a guest
Sep 27th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.71 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6.  
  7. public class DAL {
  8. private Connection con = null;
  9. public Connection getConnection() throws SQLException {
  10. //String driver = ("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  11. String url ="jdbc:sqlserver://localhost:1433;Database=Assignment1;";
  12. String user = "assignment1";
  13. String pass = "uppgift1";
  14.  
  15. return DriverManager.getConnection(url, user, pass);
  16.  
  17. }
  18. //method register student
  19. public DAL insertStudent(String studentID, String firstName, String lastName, int credits, String address) {
  20. String sql = "INSERT INTO Student(studentID, fristName, lastName, creadits, address) VALUES(?, ?, ?, ?, ?)";
  21.  
  22. try (Connection con = this.getConnection();
  23. PreparedStatement pstmt = con.prepareStatement(sql)) {
  24. pstmt.setString(1, c.getStudentID);
  25. pstmt.setString(2, c.getFirstName);
  26. pstmt.setString(3, c.getLastName);
  27. pstmt.setString(4, c.getAddress);
  28. pstmt.setInt(5, c.getCredits);
  29. pstmt.executeUpdate();
  30. }
  31. catch (SQLException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. //method register course
  37. public void insertCourse(String courseID, String courseName, int credits) {
  38. String sql = "INSERT INTO Course(studentID, courseName, creadits) VALUES(?, ?, ?)";
  39.  
  40. try (Connection con = this.getConnection();
  41. PreparedStatement pstmt = con.prepareStatement(sql)) {
  42. pstmt.setString(1, c.getCourseID);
  43. pstmt.setString(2, c.getCourseName);
  44. pstmt.setInt(3, c.getCredits);
  45. pstmt.executeUpdate();
  46. }
  47. catch (SQLException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51.  
  52. //method register studies relation
  53. public void insertStudies(String studentID, String courseID, int credits) {
  54. String sql = "INSERT INTO Studies(studentID, courseID , creadits) VALUES(?, ?, ?)";
  55.  
  56. try (Connection con = this.getConnection();
  57. PreparedStatement pstmt = con.prepareStatement(sql)) {
  58. pstmt.setString(1, c.getStudentID);
  59. pstmt.setString(2, c.getCourseID);
  60. pstmt.setInt(3, c.getCredits);
  61. pstmt.executeUpdate();
  62. }
  63. catch (SQLException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67.  
  68. //method register hasStudied relation
  69. public void insertHasStudied(String studentID, String courseID, String grade) {
  70. String sql = "INSERT INTO HasStudied(studentID, courseID , grade) VALUES(?, ?, ?)";
  71.  
  72. try (
  73. Connection con = this.getConnection();
  74. PreparedStatement pstmt = con.prepareStatement(sql)) {
  75. pstmt.setString(1, c.getStudentID);
  76. pstmt.setString(2, c.getCourseID);
  77. pstmt.setString(3, c.getGrade);
  78. pstmt.executeUpdate();
  79. }
  80. catch (SQLException e) {
  81. e.printStackTrace();
  82. }
  83. }
  84.  
  85.  
  86. //Find Student
  87. public void findStudent(String studentID) {
  88. String sql = "SELECT * FROM Student WHERE studentID = '?'";
  89.  
  90. try {
  91. Connection con = this.getConnection();
  92. PreparedStatement pstmt = con.prepareStatement(sql);
  93. pstmt.setString(1,studentID);
  94. ResultSet rs = pstmt.executeQuery();
  95. while (rs.next()) {
  96. System.out.println(rs.getString("studentID") + rs.getString("studentName") +
  97. rs.getString("address") + rs.getInt("credits"));
  98. }
  99. }
  100. catch(SQLException e) {
  101. e.printStackTrace();
  102. }
  103. }
  104.  
  105.  
  106. //Find Course
  107. public void findCourse(String courseID) {
  108. String sql = "SELECT * FROM Course WHERE courseID = '?'";
  109.  
  110. try {
  111. Connection con = this.getConnection();
  112. PreparedStatement pstmt = con.prepareStatement(sql);
  113. pstmt.setString(1, c.getCourseID);
  114. ResultSet rs = pstmt.executeQuery();
  115. while (rs.next()) {
  116. System.out.println(rs.getString(1) + rs.getString(2) + rs.getString(3));
  117. }
  118. }
  119. catch(SQLException e) {
  120. e.printStackTrace();
  121. }
  122. }
  123.  
  124.  
  125. //Find Results form a specific course (test for find)
  126. public void findAllStudentResultsFromCourse(String courseID) {
  127. String query = "SELECT s.studentID, s.firstName, s.lastName, hs,grade FROM hasStudied hs JOIN Studies s "
  128. + "ON hs.studentID = s.studentID WHERE hs.courseID = '?'";
  129.  
  130. try {
  131. Connection con = this.getConnection();
  132. PreparedStatement pstmt = con.prepareStatement(query);
  133. pstmt.setString(1, c.getCourseID);
  134. ResultSet rs = pstmt.executeQuery();
  135. while (rs.next()) {
  136. //change to show resultSet for a user interface
  137. System.out.printf(rs.getString(1), rs.getString(2), rs.getString(3));
  138. }
  139. }
  140. catch(SQLException e) {
  141. e.printStackTrace();
  142. }
  143. }
  144.  
  145. // Find students that has not finished a course
  146.  
  147. public void findStudentThatHaveNotFinishedACourse(String studentID) {
  148. String query = "SELECT s.firstName, s.lastName, st.courseID FROM Studies st JOIN Student s "
  149. + "ON st.studentID = s.studentID WHERE studentID = '?' AND courseID = '?'";
  150.  
  151. try {
  152. Connection con = this.getConnection();
  153. PreparedStatement pstmt = con.prepareStatement(query);
  154. pstmt.setString(1, c.getStudentID);
  155. pstmt.setString(2, c.getcourseID);
  156. ResultSet rs = pstmt.executeQuery();
  157. while(rs.next()) {
  158. System.out.println(rs.getString(1) + ", " + rs.getString(firstName) + rs.getString(3));
  159. }
  160. }
  161. catch(SQLException e) {
  162. e.printStackTrace();
  163. }
  164.  
  165. }
  166.  
  167. //find certain result from a certain student
  168.  
  169. public void findCertainResultForACertainStudent(String studentID, String courseID) {
  170. String query = "SELECT hs.grade, c.courseID FROM HasStudied hs JOIN Course c "
  171. + "ON hs.courseID = c.courseID WHERE studentID = '?' AND courseID = '?'";
  172.  
  173. try {
  174. Connection con = this.getConnection();
  175. PreparedStatement pstmt = con.prepareStatement(query);
  176. pstmt.setString(1, c.getStudentID);
  177. pstmt.setString(2, c.getcourseID);
  178. ResultSet rs = pstmt.executeQuery();
  179. while(rs.next()) {
  180. System.out.println(rs.getString(1) + rs.getString(2));
  181. }
  182. }
  183. catch(SQLException e) {
  184. e.printStackTrace();
  185. }
  186.  
  187. }
  188.  
  189. //Find % of student with an A for a certain course
  190.  
  191. public void procentageForCertainCourseWithGradeA(String courseID) {
  192. String query = "SELECT grade, (COUNT(grade) * 100) / "
  193. + "(SELECT COUNT(grade) FROM HasStudied WHERE courseID = '?')" +
  194. "FROM HasStudied" +
  195. "WHERE grade = 'A' AND courseID = '?'" +
  196. "GROUP BY Grade";
  197.  
  198. try {
  199. Connection con = this.getConnection();
  200. PreparedStatement pstmt = con.prepareStatement(query);
  201. pstmt.setString(1, c.getcourseID);
  202. ResultSet rs = pstmt.executeQuery();
  203. while(rs.next()) {
  204. System.out.println(rs.getString(1) + rs.getString(2));
  205. }
  206. }
  207. catch(SQLException e) {
  208. e.printStackTrace();
  209.  
  210. }
  211. }
  212.  
  213. //Find best Throughput for courses
  214.  
  215. public void FindInfoForCoursesWithBestThroughput() {
  216. String query = "select top 3 upper(courseID) as 'Course Code', (sum(case when grade != 'U'" +
  217. "then 1 else 0 end)*100) / count (courseID) as 'Percent Passed'" +
  218. "from hasStudied" +
  219. "group by courseID" +
  220. "order by 'Percent Passed' desc;";
  221.  
  222. try {
  223. Connection con = this.getConnection();
  224. PreparedStatement pstmt = con.prepareStatement(query);
  225. ResultSet rs = pstmt.executeQuery();
  226. while(rs.next()) {
  227. System.out.println(rs.getString(1) + rs.getString(2));
  228. }
  229. }
  230. catch(SQLException e) {
  231. e.printStackTrace();
  232.  
  233. }
  234. }
  235.  
  236. //Delete Student
  237.  
  238. public void DeleteStudent(String studentID) {
  239. String query = "DELETE FROM Student WHERE studentID = '?'";
  240.  
  241. try {
  242. Connection con = this.getConnection();
  243. PreparedStatement pstmt = con.prepareStatement(query);
  244. pstmt.setString(1,studentID);
  245. ResultSet rs = pstmt.executeQuery();
  246.  
  247. }
  248. catch(SQLException e) {
  249. e.printStackTrace();
  250. }
  251. }
  252.  
  253. //Delete Course
  254.  
  255. public void DeleteCourse(String courseID) {
  256. String query = "DELETE FROM Course WHERE courseID = '?'";
  257.  
  258. try {
  259. Connection con = this.getConnection();
  260. PreparedStatement pstmt = con.prepareStatement(query);
  261. pstmt.setString(1,courseID);
  262. ResultSet rs = pstmt.executeQuery();
  263.  
  264. }
  265. catch(SQLException e) {
  266. e.printStackTrace();
  267. }
  268. }
  269.  
  270. //Delete studies
  271.  
  272. public void DeleteStudiesRelation(String courseID, String studentID) {
  273. String query = "DELETE FROM Studies WHERE courseID = '?' AND studentID = '?'";
  274.  
  275. try {
  276. Connection con = this.getConnection();
  277. PreparedStatement pstmt = con.prepareStatement(query);
  278. pstmt.setString(1,courseID);
  279. pstmt.setString(2,studentID);
  280. ResultSet rs = pstmt.executeQuery();
  281.  
  282. }
  283. catch(SQLException e) {
  284. e.printStackTrace();
  285. }
  286. }
  287.  
  288.  
  289.  
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement