Guest User

StudentDB.java

a guest
Sep 12th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.98 KB | None | 0 0
  1. package com.registration.bean;
  2.  
  3. /**
  4.  *
  5.  * @author isaac
  6.  */
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import java.util.ArrayList;
  15. import java.util.Map;
  16. import javax.faces.bean.ApplicationScoped;
  17. import javax.faces.context.FacesContext;
  18. @ApplicationScoped
  19. public class StudentDB {
  20. public static Connection connection;//manages connection
  21. public static Statement statement; //query statement
  22. public static ResultSet resultSet; //manage results//manage results
  23. public static PreparedStatement ps;
  24.  
  25. public static StudentBean studentBean;
  26.  
  27. // method to connect to the database
  28. public static Connection getDBConnection() {
  29.     //any attempts on database must be enclosed in a try...catch block
  30.     //throws SQLException
  31.     try {
  32.         //getting the driver
  33.         Class.forName("com.mysql.jdbc.Driver");
  34.         //database parameters
  35.         final String url = "jdbc:derby://localhost:1527/Publishers";
  36.         final String user = "root";
  37.         final String password = "";
  38.         //establishing database connection
  39.         connection = DriverManager.getConnection(url,user,password);
  40.         System.out.println("Connection established succesfully");
  41.     } catch (ClassNotFoundException ex) {
  42.         ex.printStackTrace();
  43.     } catch(SQLException sqlException) {
  44.         sqlException.printStackTrace();
  45.     }
  46.    return connection;
  47. } //end of method to get database connection
  48. //method to retrieve Students list  from database
  49. public static ArrayList getStudentsList() {
  50.     //create a arraylist object to  store the students
  51.     ArrayList studentList = new ArrayList();
  52.     //any attempts on a database must be  enclosed in try...catch block
  53.     try {
  54.         statement = getDBConnection().createStatement();
  55.         resultSet = statement.executeQuery("SELECT * FROM student_record");
  56.         while (resultSet.next()) {
  57.             StudentBean student = new StudentBean(); //new student object
  58.             student.setId(resultSet.getInt("student_id"));
  59.             student.setName(resultSet.getString("student_name"));
  60.             student.setEmail(resultSet.getString("student_email"));
  61.             student.setPassword(resultSet.getString("student_password"));
  62.             student.setGender(resultSet.getString("student_gender"));
  63.             student.setAddress(resultSet.getString("student_address"));
  64.             //store retrieved objects into arrayList
  65.             studentList.add(student);
  66.         }
  67.         System.out.println("Total students registered: " + studentList.size());
  68.     } catch (SQLException sqlException) {
  69.         sqlException.printStackTrace();
  70.     } finally {
  71.         //destroy objects once  done
  72.         try {
  73.             resultSet.close();
  74.             statement.close();
  75.             connection.close();
  76.         } catch (Exception ex) {
  77.             ex.printStackTrace();
  78.         }
  79.     } //end finally
  80.     return studentList;
  81. }
  82. //method to add new students into the database
  83. public static String addStudents(StudentBean student) {
  84.    int result = 0 ;
  85.    String navigationResult = "";
  86.    try {
  87.        ps = getDBConnection().prepareStatement("INSERT INTO student_record(student_id,student_name,student_email,student_password,student_gender,student_address) VALUES(?,?,?,?,?,?)");
  88.        ps.setInt(1, student.getId());
  89.        ps.setString(2, student.getName());
  90.        ps.setString(3, student.getEmail());
  91.        ps.setString(4, student.getPassword());
  92.        ps.setString(5, student.getGender());
  93.        ps.setString(6, student.getAddress());
  94.        ps.executeUpdate();
  95.        result = 1;
  96.    } catch(SQLException sqlException) {
  97.        sqlException.printStackTrace();
  98.    } finally {
  99.        try{
  100.        connection.close(); //close database connection
  101.        }catch(Exception ex) {
  102.            ex.printStackTrace();
  103.        }
  104.    }//end try..catch..finally block
  105.    if (result != 0){
  106.        navigationResult = "studentList.xhtml?faces-redirect=true";
  107.    }else {
  108.        navigationResult = "createStudent.xhtml?faces-redirect=true";
  109.    }
  110.    return navigationResult;
  111. }
  112. //method to edit the student list
  113. public static String editStudentsRecords(int student_id) {
  114.     StudentBean editRecords = null;
  115.    
  116.     System.out.println("editStudentsRecords(): Student Id: " + student_id);
  117.     //Setting a particular students details in session
  118.     Map<String,Object> sessionMapObject = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
  119.     try {
  120.         statement = getDBConnection().createStatement();
  121.         resultSet = statement.executeQuery("SELECT * FROM student_record WHERE student_id = " + student_id);
  122.         if (resultSet != null) {
  123.             resultSet.next();
  124.            editRecords = new StudentBean();
  125.             editRecords.setId(resultSet.getInt("student_id"));
  126.             editRecords.setName(resultSet.getString("student_name"));
  127.             editRecords.setEmail(resultSet.getString("student_email"));
  128.             editRecords.setPassword(resultSet.getString("student_password"));
  129.             editRecords.setGender(resultSet.getString("student_gender"));
  130.             editRecords.setAddress(resultSet.getString("student_gender"));
  131.         }
  132.         sessionMapObject.put("editRecords", editRecords);
  133.     } catch(SQLException sqlException) {
  134.         sqlException.printStackTrace();
  135.     }
  136.     return "EditStudentRecord.xhtml?faces-redirect=true";
  137. }
  138. //method to update student  records  in Database
  139. public static String updateStudentRecords(StudentBean updateStudent) {
  140. try {
  141. ps = getDBConnection().prepareStatement("UPDATE student_record SET student_id = ?,student_name = ?,student_email = ?,student_password = ?,student_gender = ?,student_address = ? WHERE student_id = ?");
  142. ps.setInt(1, updateStudent.getId());
  143. ps.setString(2, updateStudent.getName());
  144. ps.setString(3, updateStudent.getEmail());
  145. ps.setString(4, updateStudent.getPassword());
  146. ps.setString(5, updateStudent.getGender());
  147. ps.setString(6, updateStudent.getAddress());
  148. ps.executeUpdate();
  149. } catch(SQLException sqlException) {
  150.     sqlException.printStackTrace();
  151. } finally {
  152.     try {
  153.         connection.close();
  154.     } catch(Exception ex) {
  155.         ex.printStackTrace();
  156.     }
  157. }
  158. return  "/studentList.xhtml?faces-redirect=true";
  159. } //end of method update
  160. //method to delete students
  161. public static String deleteStudentRecords(int studentId) {
  162.     System.out.println("deleteStudentRecords() : Student Id: " + studentId);
  163.     try {
  164.         ps = getDBConnection().prepareStatement("DELETE from student_record WHERE student_id = " + studentId);
  165.         ps.executeUpdate();
  166.     } catch(SQLException sqlException) {
  167.         sqlException.printStackTrace();
  168.     } finally {
  169.         try {
  170.             connection.close();
  171.         } catch (SQLException ex) {
  172.             ex.printStackTrace();
  173.         }
  174.     } //end try...catch...finally
  175.     return "/studentList.xhtml?faces-redirect=true";
  176. } //end delete method
  177. } //end of studentDB Class
Add Comment
Please, Sign In to add comment