Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.46 KB | None | 0 0
  1. package dal;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class Dal {
  10.  
  11.     private Connection con;
  12.     private Statement stat;
  13.  
  14.     String connectionUrl;
  15.  
  16.     //--------------------SKAPAR KOPPLING TILL DATABASEN--------------------//
  17.  
  18.     public Dal() {
  19.  
  20.         try {
  21.             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  22.             connectionUrl = "jdbc:sqlserver://Ramy\\MSSQLSERVER;" +
  23.                     "database=DB2;" +
  24.                     "user=ramy;" +
  25.                     "password=123456";
  26.  
  27.             con = getConnection();
  28.             System.out.println("Connected.");
  29.  
  30.         } catch (Exception e) {
  31.             e.printStackTrace();
  32.             System.out.println(e.getMessage());
  33.             System.exit(0);
  34.         }
  35.     }
  36.     public Connection getConnection() throws SQLException {
  37.         return con = DriverManager.getConnection(connectionUrl);
  38.     }
  39.  
  40.     //--------------------ADDING TO THE DATABASE--------------------//  
  41.  
  42.     //Add a new student to the database
  43.     public int addStudent(String spnr, String sname) throws SQLException {
  44.         stat = con.createStatement();
  45.         int result = stat.executeUpdate("insert into Student values ('" + spnr + "','" + sname + "');"  );
  46.         return result;
  47.     }
  48.     //Add a new course to the database
  49.     public int addCourse(String ccode, String cname, int cpoint) throws SQLException {
  50.         stat = con.createStatement();
  51.         int result = stat.executeUpdate("insert into Course values ('" + ccode + "','" + cname + "','" + cpoint + "');"  );
  52.         return result;
  53.     }
  54.     //Add a student to a current course in the database (register student)
  55.     public int addStudies(String spnr, String ccode) throws SQLException {
  56.         stat = con.createStatement();
  57.         int result = stat.executeUpdate("insert into Studies values ('" + spnr + "','" + ccode + "');"  );
  58.         return result;
  59.     }
  60.     //Add a student to a finished course in the database (register grade)
  61.  
  62.     public int addStudied(String spnr, String ccode, String grade) throws SQLException {
  63.         stat = con.createStatement();
  64.         int result = stat.executeUpdate("insert into Studied values ('" + spnr + "','" + ccode + "','" + grade + "');"
  65.                 + "delete from studies "
  66.                 + "where spnr='"+ spnr +"' and "
  67.                 + "ccode='"+ ccode +"';");
  68.         return result;
  69.     }
  70.     //--------------------REMOVING IN THE DATABASE--------------------//
  71.  
  72.     //remove course
  73.     public int removeCourse(String ccode) throws SQLException {
  74.         stat = con.createStatement();
  75.         int result = stat.executeUpdate("delete from course where ccode='"+ccode+"';" );
  76.         return result;
  77.     }
  78.  
  79.     //delete Student
  80.     public int removeStudent(String spnr) throws SQLException {
  81.         stat = con.createStatement();
  82.         int result = stat.executeUpdate("delete from student where spnr='"+spnr+"';" );
  83.         return result;
  84.     }
  85.  
  86.     //delete student from course
  87.     public int removeStudentFromCourse(String spnr, String ccode) throws SQLException {
  88.         stat = con.createStatement();
  89.         int result = stat.executeUpdate("delete from studies where spnr='"+spnr+ "' "
  90.                 + "and ccode='"+ccode+"';" );
  91.         return result;
  92.     }
  93.  
  94.     //--------------------SEARCHING IN THE DATABASE--------------------//
  95.  
  96.     //Find a student in the database (by spnr)
  97.     public ResultSet getStudent(String spnr) throws SQLException {
  98.         stat = con.createStatement();
  99.         ResultSet result = stat.executeQuery("select s.spnr as 'Identity number', s.sname as Name "
  100.                 + "from Student s "
  101.                 + "where spnr='"+ spnr +"';");
  102.         return result;
  103.     }
  104.  
  105.     //Find a course in the database (by course code)
  106.     public ResultSet getCourse(String ccode) throws SQLException {
  107.         stat = con.createStatement();
  108.         ResultSet result = stat.executeQuery("select c.ccode as Code, c.cname as Name, c.cpoint as Points "
  109.                 + "from Course c "
  110.                 + "where ccode='"+ ccode +"';");
  111.         return result;
  112.     }
  113.  
  114.     //Find result for a course (all students on course and their grade)
  115.     public ResultSet getCourseResult(String ccode) throws SQLException {
  116.         stat = con.createStatement();
  117.         ResultSet result = stat.executeQuery("select s.spnr as 'Identity number', s.sname as Name, st.grade as Grade "
  118.                 + "from Student s join studied st "
  119.                 + "on st.spnr=s.spnr where st.ccode='"+ ccode +"';");
  120.         return result;
  121.     }
  122.  
  123.     //Find the grade for a certain student at a certain course
  124.     public ResultSet getStudentResult(String spnr, String ccode) throws SQLException{
  125.         stat = con.createStatement();
  126.         ResultSet result = stat.executeQuery("select c.cname as 'Course name', st.spnr as 'Identity number', st.grade as Grade "
  127.                 + "from Studied st "                                              
  128.                 + "inner join Course c "                                        
  129.                 + "on c.ccode=st.ccode "
  130.                 + "where st.spnr='"+spnr+"' "
  131.                 + "and st.ccode='"+ccode+"';");
  132.         return result;
  133.     }
  134.  
  135.     //Find all courses in the database                                                                                  //NYTT
  136.     public ResultSet getAllCourses() throws SQLException {
  137.         stat = con.createStatement();
  138.         ResultSet result = stat.executeQuery("select c.ccode as Code, c.cname as Name, c.cpoint as Points "
  139.                 + "from Course c "
  140.                 + "order by cname;");
  141.         return result;
  142.     }
  143.  
  144.     //Find all courses that a certain student is studying
  145.     public ResultSet getCoursesStudentIsStudying(String spnr) throws SQLException {
  146.         stat = con.createStatement();
  147.         ResultSet result = stat.executeQuery("select c.ccode as Code, c.cname as Name, c.cpoint as Points "
  148.                 + "from course c "
  149.                 + "join studies st "
  150.                 + "on st.ccode=c.ccode "           
  151.                 + "where st.spnr='"+ spnr +"';");
  152.         return result;
  153.     }
  154.  
  155.     // Find a list of all students in the database
  156.     public ResultSet getAllStudents() throws SQLException {
  157.         stat = con.createStatement();
  158.         ResultSet result = stat.executeQuery("select s.spnr as 'Identity number', s.sname as Name "
  159.                 + "from Student s "
  160.                 + "order by sname");
  161.         return result;
  162.     }
  163.  
  164.     //--------------------SEARCHES FOR GRADE A AND B--------------------//
  165.  
  166.     //Find the students that are not ready with course a certain course
  167.     public ResultSet getStudentsNotReadyWithCourse(String ccode) throws SQLException {
  168.         stat = con.createStatement();
  169.         ResultSet result = stat.executeQuery("select s.spnr as 'Identity number', s.sname as Name "
  170.                 + "from student s "
  171.                 + "join studies st "
  172.                 + "on s.spnr=st.spnr "
  173.                 + "where st.ccode='"+ ccode +"'"
  174.                 + "order by sname;");
  175.         return result;
  176.     }
  177.  
  178.     // Get the percentage of students that have the grade "A" in a certain course
  179.     public double getProcentageOfA(String ccode) throws SQLException {
  180.         stat = con.createStatement();
  181.  
  182.         ResultSet rs = stat.executeQuery("select count(*) as nr "
  183.                 + "from studied "
  184.                 + "where ccode='"+ ccode +"' "
  185.                 + "and grade ='A' "
  186.                 + "union "
  187.                 + "select count(*) as nr "
  188.                 + "from studied "
  189.                 + "where ccode='"+ccode+"'");            
  190.  
  191.         rs.next();
  192.         if(rs.getInt(1) != 0){
  193.  
  194.             double result1 = rs.getInt("nr");
  195.             rs.next();
  196.             double result2 = rs.getInt("nr");                  
  197.             double result = ((result1/result2)*(100));
  198.             return result;
  199.         }
  200.         else {
  201.             double resultNull = 0;
  202.             return resultNull;
  203.         }
  204.     }
  205.  
  206.     // get the course with highest flow  
  207.     public ResultSet getHighestFlow()throws SQLException {
  208.         stat = con.createStatement();
  209.         ResultSet rs = stat.executeQuery("select top 5 c.ccode 'Course code', c.cname as 'Course name', "
  210.                 + "sum((case when grade != 'u' then 1 else 0 end)) * 100 "
  211.                 + "/(sum(case when grade like '_' then 1 else 0 end)) as [Passed %] "
  212.                 + "from studied s, course c where s.ccode = c.ccode "
  213.                 + "group by c.cname, c.ccode "
  214.                 + "order by [Passed %] DESC");
  215.         return rs;
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement