Guest User

some random shit

a guest
Aug 1st, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.99 KB | None | 0 0
  1. package com;
  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.  
  10. public class EmployeeManagementDAO {
  11.     private String connectionString = "jdbc:oracle:thin:@172.24.137.13:1521:XE";
  12.     private String userName = "JE";
  13.     private String password = "JE";
  14.     private String driver = "oracle.jdbc.driver.OracleDriver";
  15.     private Connection con;
  16.     private PreparedStatement ps;
  17.    
  18.     public int addEmployee(EmployeeBean e){
  19.         int result = 0;
  20.         try{
  21.             Class.forName(driver);
  22.            
  23.             con = DriverManager.getConnection(connectionString, userName, password);
  24.            
  25.             String query = "INSERT INTO TBL_1437159_EMP(EMP_ID, EMP_NAME, AGE, LG_ID) " +
  26.                                 " VALUES(?,?,?,?)";
  27.            
  28.             ps = con.prepareStatement(query);
  29.            
  30.             if(ps!=null){
  31.                 ps.setInt(1, e.getEmpId());
  32.                 ps.setString(2, e.getEmpName());
  33.                 ps.setInt(3, e.getAge());
  34.                 ps.setInt(4, e.getLgId());
  35.             }
  36.            
  37.             result = ps.executeUpdate();
  38.            
  39.         }catch (ClassNotFoundException ce) {
  40.             result = 0;
  41.         }catch (SQLException se) {
  42.             result = 0;
  43.         }finally{
  44.             try{
  45.                 if(con != null){
  46.                     con.close();
  47.                 }
  48.                 if(ps != null){
  49.                     ps.close();
  50.                 }
  51.             }catch (SQLException se) {
  52.                 result = 0;
  53.             }
  54.         }
  55.         return result;
  56.     }
  57.  
  58.     public ArrayList<EmployeeBean> getEmployeesBasedOnAge(int from, int to){
  59.         ArrayList<EmployeeBean> result = new ArrayList<EmployeeBean>();
  60.        
  61.         try{
  62.             Class.forName(driver);
  63.            
  64.             con = DriverManager.getConnection(connectionString, userName, password);
  65.            
  66.             String query = "SELECT * FROM TBL_1437159_EMP " +
  67.                                 "WHERE AGE BETWEEN ? AND ?";
  68.            
  69.             ps = con.prepareStatement(query);
  70.            
  71.             if(ps!=null){
  72.                 ps.setInt(1, from);
  73.                 ps.setInt(2, to);
  74.             }
  75.            
  76.             ResultSet rs = ps.executeQuery();
  77.            
  78.             while(rs.next()){
  79.                 EmployeeBean temp = new EmployeeBean();
  80.                 temp.setEmpId(rs.getInt("EMP_ID"));
  81.                 temp.setEmpName(rs.getString("EMP_NAME"));
  82.                 temp.setAge(rs.getInt("AGE"));
  83.                 temp.setLgId(rs.getInt("LG_ID"));
  84.                 result.add(temp);
  85.             }
  86.            
  87.         }catch (ClassNotFoundException ce) {
  88.             result = null;
  89.         }catch (SQLException se) {
  90.             result = null;
  91.         }finally{
  92.             try{
  93.                 if(con != null){
  94.                     con.close();
  95.                 }
  96.                 if(ps != null){
  97.                     ps.close();
  98.                 }
  99.             }catch (SQLException se) {
  100.                 result = null;
  101.             }
  102.         }
  103.        
  104.         return result;
  105.     }
  106.  
  107.    
  108.    
  109.     public ArrayList<Integer> getBatchNumbers(){
  110.         ArrayList<Integer> result = new ArrayList<Integer>();
  111.        
  112.         try{
  113.             Class.forName(driver);
  114.            
  115.             con = DriverManager.getConnection(connectionString, userName, password);
  116.            
  117.             String query = "SELECT BATCH_NUM FROM TBL_1437159_LG SORT BY BATCH_NUM DESC";
  118.            
  119.             ps = con.prepareStatement(query);
  120.            
  121.             ResultSet rs = ps.executeQuery();
  122.            
  123.             while(rs.next()){
  124.                 result.add(rs.getInt("BATCH_NUM"));
  125.             }
  126.            
  127.         }catch (ClassNotFoundException ce) {
  128.             result = null;
  129.         }catch (SQLException se) {
  130.             result = null;
  131.         }finally{
  132.             try{
  133.                 if(con != null){
  134.                     con.close();
  135.                 }
  136.                 if(ps != null){
  137.                     ps.close();
  138.                 }
  139.             }catch (SQLException se) {
  140.                 result = null;
  141.             }
  142.         }
  143.         return result;
  144.     }
  145.    
  146.    
  147.     public int getAvrageAge(String criteria){
  148.         int result = 0;
  149.         try{
  150.             Class.forName(driver);
  151.            
  152.             con = DriverManager.getConnection(connectionString, userName, password);
  153.            
  154.             String query = "SELECT AVG(AGE) AS AVERAGE_AGE FROM TBL_1437159_EMP " +
  155.                     " WHERE EMP_NAME LIKE ?%";
  156.            
  157.             ps = con.prepareStatement(query);
  158.            
  159.             if(ps!=null){
  160.                 ps.setString(1, criteria);
  161.             }
  162.            
  163.             ResultSet rs = ps.executeQuery();
  164.            
  165.             while(rs.next()){
  166.                 result = rs.getInt("AVERAGE_AGE");
  167.             }
  168.            
  169.         }catch (ClassNotFoundException ce) {
  170.             result = 0;
  171.         }catch (SQLException se) {
  172.             result = 0;
  173.         }finally{
  174.             try{
  175.                 if(con != null){
  176.                     con.close();
  177.                 }
  178.                 if(ps != null){
  179.                     ps.close();
  180.                 }
  181.             }catch (SQLException se) {
  182.                 result = 0;
  183.             }
  184.         }
  185.         return result;
  186.     }
  187.    
  188. }
Add Comment
Please, Sign In to add comment