Advertisement
Guest User

DAL TISDAG

a guest
Sep 27th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.62 KB | None | 0 0
  1. // DAL ------------------------------------------------
  2.  
  3. package DB2package;
  4.  
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.ResultSetMetaData;
  10. //import java.util.ArrayList;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13.  
  14. public class DAL {
  15.  
  16.     private Course course;
  17.     private String ccode;
  18.     private String cname;
  19.     private String caddress;
  20.     private Student student;
  21.     private String pnr;
  22.     private String sname;
  23.     private String saddress;
  24.     private Studied studied;
  25.     private String grade;
  26.     private Studies studies;
  27.     private String SQL;
  28.  
  29.     // ------------------Skapa koppling -----------------//
  30.     public static Connection getConnection() {
  31.         Connection con = null;
  32.         try {
  33.             String host = "jdbc:sqlserver://localhost;databaseName=IS";
  34.             String uName = "sa";
  35.             String uPass = "molTaz36";
  36.             con = DriverManager.getConnection(host, uName, uPass);
  37.         } catch (Exception e) {
  38.             System.out.println("Kunde inte koppla" + e);
  39.         }
  40.         return con;
  41.     }
  42.  
  43.     // -----------------Skapa -------------------------//
  44.  
  45.     public void createStudent(String pnr, String sname, String saddress) throws SQLException {
  46.  
  47.             Connection con = getConnection();
  48.             Statement stmt  = con.createStatement();
  49.            
  50.             SQL = "insert into student values ('" + pnr + "' ,'" + sname + "','" + saddress + "');";
  51.            
  52.             stmt.executeUpdate(SQL);
  53.             stmt.close();
  54.            
  55.     }
  56.  
  57.     public Course createCourse(String ccode, String cname, String caddress) {
  58.  
  59.         try {
  60.             Connection con = getConnection();
  61.             Statement stmt = con.createStatement();
  62.  
  63.             String SQL = "insert into course values ('" + ccode + "', '" + cname + "', '" + caddress + "');";
  64.  
  65.             stmt.executeUpdate(SQL);
  66.             stmt.close();
  67.             con.close();
  68.  
  69.         }
  70.  
  71.         catch (Exception e) {
  72.             System.out.println(e);
  73.         }
  74.         return course;
  75.     }
  76.  
  77.     // -------------------Söka ------------------------ //
  78.     public Student findStudentByPnr(String pnr) {
  79.  
  80.         try {
  81.             Connection con = getConnection();
  82.             String SQL = "select * from student where pnr = ?";
  83.             PreparedStatement stmt = con.prepareStatement(SQL);
  84.             stmt.setString(1, pnr);
  85.             ResultSet rs = stmt.executeQuery();
  86.             while (rs.next()) {
  87.                 pnr = rs.getString("pnr");
  88.                 sname = rs.getString("sname");
  89.                 saddress = rs.getString("saddress");
  90.                 student = new Student();
  91.                 student.setSname(sname);
  92.                 student.setSaddress(saddress);
  93.             }
  94.             stmt.close();
  95.             con.close();
  96.             return student;
  97.         } catch (Exception e) {
  98.             System.out.println(e);
  99.         }
  100.         return student;
  101.     }
  102.  
  103.     public Course findCourseByccode(String ccode) {
  104.         try {
  105.             Connection con = getConnection();
  106.             String SQL = "select * from course where ccode = ?";
  107.             PreparedStatement stmt = con.prepareStatement(SQL);
  108.             stmt.setString(1, ccode); // använd detta
  109.             ResultSet rs = stmt.executeQuery();
  110.             while (rs.next()) {
  111.                 ccode = rs.getString("ccode");
  112.                 cname = rs.getString("cname");
  113.                 caddress = rs.getString("caddress");
  114.                 System.out.println(ccode + cname + caddress);
  115.                 course = new Course();
  116.                 course.setCcode(ccode);
  117.                 course.setCaddress(caddress);
  118.                 course.setCname(cname);
  119.             }
  120.            
  121.             stmt.close();
  122.             con.close();
  123.             return course;
  124.         } catch (Exception e) {
  125.             System.out.println(e);
  126.         }
  127.         return course;
  128.        
  129.  
  130.     }
  131.  
  132.     public Studied getCourseResults(String ccode) { // Alla studenters betyg
  133.                                                     // från en kurs
  134.         try {
  135.             Connection con = getConnection();
  136.             SQL = "select grade from studied where ccode = ?";
  137.             PreparedStatement stmt = con.prepareStatement(SQL);
  138.             ResultSet rs = stmt.executeQuery();
  139.             stmt.setString(1, ccode);
  140.             while (rs.next()) {
  141.                 ccode = rs.getString("ccode");
  142.                 cname = rs.getString("cname");
  143.                 caddress = rs.getString("caddress");
  144.                 System.out.println(ccode + cname + caddress);
  145.             }
  146.             stmt.close();
  147.             con.close();
  148.         } catch (Exception e) {
  149.             System.out.println(e);
  150.         }
  151.         return studied;
  152.     }
  153.  
  154.     public Studied getStudentResult(String pnr, String ccode) { // En students
  155.                                                                 // betyg för en
  156.                                                                 // viss kurs
  157.         try {
  158.             Connection con = getConnection();
  159.             SQL = "select grade from studied where pnr = ? and ccode = ?";
  160.             PreparedStatement stmt = con.prepareStatement(SQL);
  161.             ResultSet rs = stmt.executeQuery();
  162.             stmt.setString(1, pnr);
  163.             stmt.setString(2, ccode);
  164.             while (rs.next()) {
  165.                 grade = rs.getString("grade");
  166.                 pnr = rs.getString("pnr");
  167.                 ccode = rs.getString("ccode");
  168.                 System.out.println(grade + pnr + ccode);
  169.             }
  170.             stmt.close();
  171.             con.close();
  172.         } catch (Exception e) {
  173.  
  174.         }
  175.         return studied;
  176.     }
  177.  
  178.     public Student findAllStudentsByPnr(String pnr) {
  179.         try {
  180.             Connection con = getConnection();
  181.             SQL = "select * from student order by pnr ";
  182.             PreparedStatement stmt = con.prepareStatement(SQL);
  183.             ResultSet rs = stmt.executeQuery();
  184.             while (rs.next()) {
  185.                 pnr = rs.getString("pnr");
  186.                 sname = rs.getString("sname");
  187.                 saddress = rs.getString("saddress");
  188.                 System.out.println(pnr + sname + saddress);
  189.             }
  190.             stmt.close();
  191.             con.close();
  192.         } catch (Exception e) {
  193.             System.out.println(e);
  194.         }
  195.         return student;
  196.     }
  197.  
  198.     public Course findAllCourseByccode(String ccode) {
  199.         try {
  200.             Connection con = getConnection();
  201.             SQL = "select * from course order by cname";
  202.             PreparedStatement stmt = con.prepareStatement(SQL);
  203.             ResultSet rs = stmt.executeQuery();
  204.             while (rs.next()) {
  205.                 ccode = rs.getString("ccode");
  206.                 cname = rs.getString("cname");
  207.                 caddress = rs.getString("caddress");
  208.             }
  209.             stmt.close();
  210.             con.close();
  211.         } catch (Exception e) {
  212.             System.out.println(e);
  213.         }
  214.         return course;
  215.     }
  216.  
  217.     // -------------Registera --------------///
  218.     public void addStudentToStudied(String pnr, String ccode, String grade) {
  219.         try {
  220.             Connection con = getConnection();
  221.             SQL = "insert into studied values (?, ?, ?)"; // pnr, ccode, grade
  222. //                    + "delete from studies where pnr = ?" // pnr
  223. //                    + "and ccode = ? "; // ccode
  224.             PreparedStatement stmt = con.prepareStatement(SQL);
  225.             stmt.setString(1, pnr);
  226.             stmt.setString(2, ccode);
  227.             stmt.setString(3, grade);
  228. //            stmt.setString(4, pnr);
  229. //            stmt.setString(5, ccode);
  230.             stmt.executeUpdate();
  231.        
  232.            
  233.             stmt.close();
  234.             con.close();
  235.         } catch (Exception e) {
  236.             e.printStackTrace();
  237.             System.out.println(e);
  238.         }
  239.     }
  240.  
  241.    
  242.     public void addStudentToStudies(String pnr, String ccode) {
  243.         try {
  244.             Connection con = getConnection();
  245.             SQL = "insert into studies values ('" + pnr + "','" + ccode +"');";
  246.             Statement stmt = con.createStatement();
  247.             ResultSet rs = stmt.executeQuery(SQL);
  248.             while(rs.next()){
  249.                 pnr = rs.getString("pnr");
  250.                 ccode = rs.getString("ccode");
  251.                 System.out.println(pnr + ccode);
  252.             }
  253.             stmt.close();
  254.             con.close();
  255.         } catch (Exception e) {
  256.             System.out.println(e);
  257.  
  258.         }
  259.        
  260.     }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement