Advertisement
Guest User

CYKABLYETAFSIUAFSIUJGFNSIUFNS

a guest
Feb 5th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.67 KB | None | 0 0
  1. package jtm.activity13;
  2. import java.sql.*;
  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. import org.apache.log4j.Logger;
  12.  
  13. public class TeacherManager {
  14.  
  15.     protected Connection conn;
  16.  
  17.  
  18.     public TeacherManager() {
  19.         try {
  20.             Class.forName("com.mysql.jdbc.Driver"); // Load the driver class.
  21.             conn = DriverManager.getConnection("jdbc:mysql://localhost/?autoReconnect=true&useSSL=false","root", "abcd1234");
  22.             conn.setAutoCommit(false);
  23.         }catch (Exception e){
  24.             e.printStackTrace();
  25.         }
  26.  
  27.     }
  28.  
  29.     /**
  30.      * Returns a Teacher instance represented by the specified ID.
  31.      *
  32.      * @param id
  33.      *            the ID of teacher
  34.      * @return a Teacher object
  35.      */
  36.     public Teacher findTeacher(int id) {
  37.         Teacher a = new Teacher (0,null,null);
  38.         Statement st;
  39.         try {
  40.             st = conn.createStatement();
  41.             ResultSet rs = st.executeQuery("SELECT * FROM database_activity.Teacher WHERE id = "+id);
  42.             if (rs.next()){
  43.  
  44.                 a.setFirstName(rs.getString(2));
  45.                 a.setLastName(rs.getString(3));
  46.                 a.setID(id);
  47.             }
  48.         } catch (SQLException e) {
  49.             // TODO Auto-generated catch block
  50.             e.printStackTrace();
  51.         }
  52.  
  53.         return a;
  54.     }
  55.  
  56.     /**
  57.      * Returns a list of Teacher object that contain the specified first name
  58.      * and last name. This will return an empty List of no match is found.
  59.      *
  60.      * @param firstName
  61.      *            the first name of teacher.
  62.      * @param lastName
  63.      *            the last name of teacher.
  64.      * @return a list of Teacher object.
  65.      */
  66.     public List<Teacher> findTeacher(String firstName, String lastName) {
  67.  
  68.         List <Teacher> list = new ArrayList<>();
  69.         Statement st;
  70.         try {
  71.             st = conn.createStatement();
  72.             ResultSet rs = st.executeQuery
  73.                     ("SELECT * FROM database_activity.Teacher WHERE firstname LIKE '%"+firstName+"%' AND lastname LIKE '%"+lastName+"%'");
  74.             while (rs.next()){
  75.                 list.add(new Teacher (rs.getInt("id"), rs.getString("firstname"), rs.getString("lastname")));
  76.  
  77.             }
  78.         } catch (SQLException e) {
  79.  
  80.             e.printStackTrace();
  81.         }
  82.         return list;
  83.  
  84.     }
  85.         /**
  86.      * Insert an new teacher (first name and last name) into the repository."SELECT * FROM database_activity.Teacher WHERE
  87.      *
  88.      * @param firstName
  89.      *            the first name of teacher
  90.      * @param lastName
  91.      *            the last name of teacher
  92.      * @return true if success, else false.
  93.      */
  94.  
  95.     public boolean insertTeacher(String firstName, String lastName) {
  96.         Statement st;
  97.         try {
  98.             st = conn.createStatement();
  99.             String sql = "insert into database_activity.Teacher"
  100.                     +"(firstname, lastname) values"
  101.                     +"(?,?)";
  102.             PreparedStatement preparedStatement = conn.prepareStatement(sql);
  103.             preparedStatement.setString((1), firstName);
  104.             preparedStatement.setString((2), lastName);
  105.             int rowsAffected = preparedStatement.executeUpdate();
  106.  
  107.             if (rowsAffected == 1){
  108.                 conn.commit();
  109.                 return true;
  110.             }else{
  111.                 return false;
  112.             }
  113.  
  114.         } catch (SQLException e) {
  115.             // TODO Auto-generated catch block
  116.             e.printStackTrace();
  117.         }
  118.         return false;
  119.     }
  120.  
  121.     /**
  122.      * Insert teacher object into database
  123.      *
  124.      * @param teacher
  125.      * @return true on success, false on error (e.g. non-unique id)
  126.      */
  127.     public boolean insertTeacher(Teacher teacher) {
  128.         Statement st;
  129.         try {
  130.             st = conn.createStatement();
  131.             String sql = "insert into database_activity.Teacher"
  132.                     +"(id,firstname, lastname) values"
  133.                     +"(?,?,?)";
  134.             PreparedStatement preparedStatement = conn.prepareStatement(sql);
  135.             preparedStatement.setInt((1), teacher.getID());
  136.             preparedStatement.setString((2), teacher.getFirstName());
  137.             preparedStatement.setString((3), teacher.getLastName());
  138.             int rowsAffected = preparedStatement.executeUpdate();
  139.  
  140.             if (rowsAffected == 1){
  141.                 conn.commit();
  142.                 return true;
  143.             }else{
  144.                 return false;
  145.             }
  146.  
  147.         } catch (SQLException e) {
  148.            
  149.             e.printStackTrace();
  150.         }
  151.         return false;
  152.     }
  153.  
  154.     /**
  155.      * Updates an existing Teacher in the repository with the values represented
  156.      * by the Teacher object.
  157.      *
  158.      * @param teacher
  159.      *            a Teacher object, which contain information for updating.
  160.      * @return true if row was updated.
  161.      */
  162.     public boolean updateTeacher(Teacher teacher) {
  163.         boolean status = false;
  164.         Statement st;
  165.         int id = teacher.getID();
  166.         try {
  167.             st = conn.createStatement();
  168.             String name =teacher.getFirstName();
  169.             String surname = teacher.getLastName();
  170.             String sql = "UPDATE database_activity.Teacher SET firstname=?, lastname=? WHERE id="+id;
  171.             PreparedStatement preparedStatement = conn.prepareStatement(sql);
  172.             preparedStatement.setString((1), name);
  173.             preparedStatement.setString((2), surname);
  174.             int rowsAffected = preparedStatement.executeUpdate();
  175.  
  176.             if (rowsAffected == 1){
  177.                 status = true;
  178.                 conn.commit();
  179.                 return true;
  180.             }else{
  181.                 return false;
  182.             }
  183.  
  184.         } catch (SQLException e) {
  185.             e.printStackTrace();
  186.         }
  187.         return status;
  188.     }
  189.  
  190.    
  191.     public boolean deleteTeacher(int id) {
  192.         boolean status =false;
  193.         Statement st;
  194.         try {
  195.             st = conn.createStatement();
  196.             String sql = "DELETE FROM database_activity.Teacher WHERE id="+id;
  197.             PreparedStatement preparedStatement = conn.prepareStatement(sql);
  198.             int rowsAffected = preparedStatement.executeUpdate();
  199.             if (rowsAffected == 1){
  200.                 status = true;
  201.                 conn.commit();
  202.                 return true;
  203.             }else{
  204.                 return false;
  205.             }
  206.  
  207.         } catch (SQLException e) {
  208.             e.printStackTrace();
  209.         }
  210.        
  211.         return status;
  212.     }
  213.  
  214.     public void closeConnecion() {
  215.         try {
  216.             conn.commit();
  217.             conn.close();
  218.             conn = null;
  219.         } catch (SQLException e) {
  220.             e.printStackTrace();
  221.         }
  222.  
  223.     }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement