Advertisement
Guest User

activity13

a guest
Feb 8th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.65 KB | None | 0 0
  1. package jtm.activity13;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11.  
  12. public class TeacherManager {
  13.  
  14.     protected Connection conn;
  15.  
  16.  
  17.     public TeacherManager() {
  18.         try {  
  19.             Class.forName("com.mysql.jdbc.Driver");        
  20.             conn = DriverManager.getConnection("jdbc:mysql://localhost/?autoReconnect=true&useSSL=false","root", "abcd1234");
  21.             conn.setAutoCommit(false);
  22.         } catch (SQLException | ClassNotFoundException e) {
  23.             // TODO Auto-generated catch block
  24.             e.printStackTrace();
  25.         }
  26.     }
  27.  
  28.     /**
  29.      * Returns a Teacher instance represented by the specified ID.
  30.      *
  31.      * @param id
  32.      *            the ID of teacher
  33.      * @return a Teacher object
  34.      * @throws SQLException
  35.      */
  36.     public Teacher findTeacher(int id) {
  37.        
  38.         Teacher teacher = new Teacher(0, null, null);
  39.        
  40.         try {
  41.             PreparedStatement statement = conn.prepareStatement("select * from database_activity.Teacher where id = ?");    
  42.             statement.setString(1, String.valueOf(id));
  43.             ResultSet rs = statement.executeQuery();           
  44.             if (rs.next())teacher = new Teacher(Integer.parseInt(rs.getString("id")), rs.getString("firstname"),rs.getString("lastname"));
  45.         } catch (SQLException e) {}
  46.        
  47.         return teacher;
  48.        
  49.     }
  50.  
  51.     /**
  52.      * Returns a list of Teacher object that contain the specified first name
  53.      * and last name. This will return an empty List of no match is found.
  54.      *
  55.      * @param firstName
  56.      *            the first name of teacher.
  57.      * @param lastName
  58.      *            the last name of teacher.
  59.      * @return a list of Teacher object.
  60.      */
  61.     public List<Teacher> findTeacher(String firstName, String lastName) {
  62.        
  63.         List<Teacher> teacherList = new ArrayList<Teacher>();
  64.        
  65.         try {
  66.             PreparedStatement statement = conn.prepareStatement("select * from database_activity.Teacher where firstname like ? and lastname like ?");    
  67.             statement.setString(1, "%" + firstName +"%");
  68.             statement.setString(2, "%" + lastName +"%");
  69.             ResultSet rs = statement.executeQuery();
  70.             while (rs.next()){
  71.                 Teacher teacher =  new Teacher(Integer.parseInt(rs.getString("id")), rs.getString("firstname"),rs.getString("lastname"));
  72.                 teacherList.add(teacher);
  73.             }
  74.         } catch (SQLException e) {}
  75.        
  76.         return teacherList;
  77.  
  78.     }
  79.  
  80.     /**
  81.      * Insert an new teacher (first name and last name) into the repository.
  82.      *
  83.      * @param firstName
  84.      *            the first name of teacher
  85.      * @param lastName
  86.      *            the last name of teacher
  87.      * @return true if success, else false.
  88.      */
  89.  
  90.     public boolean insertTeacher(String firstName, String lastName) {
  91.        
  92.        
  93.         if (firstName.length() > 44 || lastName.length() > 44) return false;
  94.        
  95.         try {
  96.             PreparedStatement statement = conn.prepareStatement("insert into database_activity.Teacher (firstname, lastname) values (?,?)");    
  97.             statement.setString(1, firstName);
  98.             statement.setString(2, lastName);
  99.             statement.executeUpdate();
  100.             if (statement.getUpdateCount() >0)conn.commit();
  101.         } catch (SQLException e) {}
  102.         return true;
  103.     }
  104.  
  105.     /**
  106.      * Insert teacher object into database
  107.      *
  108.      * @param teacher
  109.      * @return true on success, false on error (e.g. non-unique id)
  110.      */
  111.     public boolean insertTeacher(Teacher teacher) {    
  112.        
  113.         int id = teacher.getID();
  114.         Teacher teacher2 = findTeacher(id);
  115.         if (teacher2.getID() != 0) return false;
  116.        
  117.         String firstName = teacher.getFirstName();
  118.         String lastName = teacher.getLastName();
  119.        
  120.         try {
  121.             PreparedStatement statement = conn.prepareStatement("insert into database_activity.Teacher values (?,?,?)");    
  122.             statement.setString(1, String.valueOf(id));
  123.             statement.setString(2, firstName);
  124.             statement.setString(3, lastName);
  125.             statement.executeUpdate();
  126.             if (statement.getUpdateCount() >0)conn.commit();
  127.         } catch (SQLException e) {}
  128.         return true;
  129.     }
  130.  
  131.     /**
  132.      * Updates an existing Teacher in the repository with the values represented
  133.      * by the Teacher object.
  134.      *
  135.      * @param teacher
  136.      *            a Teacher object, which contain information for updating.
  137.      * @return true if row was updated.
  138.      */
  139.     public boolean updateTeacher(Teacher teacher) {
  140.        
  141.         int id = teacher.getID();      
  142.         Teacher teacher2 = findTeacher(id);
  143.         if (teacher2.getID() == 0) return false;
  144.        
  145.        
  146.         String firstName = teacher.getFirstName();
  147.         String lastName = teacher.getLastName();
  148.         if (teacher2.getFirstName() == firstName && teacher2.getLastName() == lastName) return false;
  149.        
  150.         try {
  151.             PreparedStatement statement = conn.prepareStatement("update database_activity.Teacher set firstname = ? , lastname = ? where id = ?");    
  152.             statement.setString(1, firstName);
  153.             statement.setString(2, lastName);
  154.             statement.setString(3, String.valueOf(id));
  155.             statement.execute();
  156.             if (statement.getUpdateCount() >0)conn.commit();
  157.             statement.close();
  158.         } catch (SQLException e) {}
  159.        
  160.         return true;
  161.     }
  162.  
  163.     /**
  164.      * Delete an existing Teacher in the repository with the values represented
  165.      * by the ID.
  166.      *
  167.      * @param id
  168.      *            the ID of teacher.
  169.      * @return true if row was deleted.
  170.      */
  171.     public boolean deleteTeacher(int id) {
  172.        
  173.         Teacher teacher = findTeacher(id);
  174.        
  175.         if (teacher.getID() == 0) return false;
  176.        
  177.         try {
  178.             PreparedStatement statement = conn.prepareStatement("delete from database_activity.Teacher where id= ? ");
  179.             statement.setString(1, String.valueOf(id));
  180.             statement.executeUpdate();
  181.             if (statement.getUpdateCount() >0)conn.commit();
  182.         } catch (SQLException e) {}
  183.         return true;
  184.     }
  185.  
  186.     public void closeConnecion() {
  187.         try {
  188.             conn.close();
  189.             conn = null;
  190.         } catch (SQLException e) {}
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement