Advertisement
Guest User

DAL

a guest
Sep 27th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 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, c.getStudentID);
  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 / (SELECT COUNT(*) FROM HasStudied))" +
  193. "FROM HasStudied" +
  194. "WHERE grade = 'A' AND courseID = '?'" +
  195. "GROUP BY Grade";
  196.  
  197. try {
  198. Connection con = this.getConnection();
  199. PreparedStatement pstmt = con.prepareStatement(query);
  200. pstmt.setString(1, c.getcourseID);
  201. ResultSet rs = pstmt.executeQuery();
  202. while(rs.next()) {
  203. System.out.println(rs.getString(1) + rs.getString(2));
  204. }
  205. }
  206. catch(SQLException e) {
  207. e.printStackTrace();
  208.  
  209. }
  210. }
  211.  
  212. //Find
  213.  
  214.  
  215.  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement