Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- public class EmployeeManagementDAO {
- private String connectionString = "jdbc:oracle:thin:@172.24.137.13:1521:XE";
- private String userName = "JE";
- private String password = "JE";
- private String driver = "oracle.jdbc.driver.OracleDriver";
- private Connection con;
- private PreparedStatement ps;
- public int addEmployee(EmployeeBean e){
- int result = 0;
- try{
- Class.forName(driver);
- con = DriverManager.getConnection(connectionString, userName, password);
- String query = "INSERT INTO TBL_1437159_EMP(EMP_ID, EMP_NAME, AGE, LG_ID) " +
- " VALUES(?,?,?,?)";
- ps = con.prepareStatement(query);
- if(ps!=null){
- ps.setInt(1, e.getEmpId());
- ps.setString(2, e.getEmpName());
- ps.setInt(3, e.getAge());
- ps.setInt(4, e.getLgId());
- }
- result = ps.executeUpdate();
- }catch (ClassNotFoundException ce) {
- result = 0;
- }catch (SQLException se) {
- result = 0;
- }finally{
- try{
- if(con != null){
- con.close();
- }
- if(ps != null){
- ps.close();
- }
- }catch (SQLException se) {
- result = 0;
- }
- }
- return result;
- }
- public ArrayList<EmployeeBean> getEmployeesBasedOnAge(int from, int to){
- ArrayList<EmployeeBean> result = new ArrayList<EmployeeBean>();
- try{
- Class.forName(driver);
- con = DriverManager.getConnection(connectionString, userName, password);
- String query = "SELECT * FROM TBL_1437159_EMP " +
- "WHERE AGE BETWEEN ? AND ?";
- ps = con.prepareStatement(query);
- if(ps!=null){
- ps.setInt(1, from);
- ps.setInt(2, to);
- }
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- EmployeeBean temp = new EmployeeBean();
- temp.setEmpId(rs.getInt("EMP_ID"));
- temp.setEmpName(rs.getString("EMP_NAME"));
- temp.setAge(rs.getInt("AGE"));
- temp.setLgId(rs.getInt("LG_ID"));
- result.add(temp);
- }
- }catch (ClassNotFoundException ce) {
- result = null;
- }catch (SQLException se) {
- result = null;
- }finally{
- try{
- if(con != null){
- con.close();
- }
- if(ps != null){
- ps.close();
- }
- }catch (SQLException se) {
- result = null;
- }
- }
- return result;
- }
- public ArrayList<Integer> getBatchNumbers(){
- ArrayList<Integer> result = new ArrayList<Integer>();
- try{
- Class.forName(driver);
- con = DriverManager.getConnection(connectionString, userName, password);
- String query = "SELECT BATCH_NUM FROM TBL_1437159_LG SORT BY BATCH_NUM DESC";
- ps = con.prepareStatement(query);
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- result.add(rs.getInt("BATCH_NUM"));
- }
- }catch (ClassNotFoundException ce) {
- result = null;
- }catch (SQLException se) {
- result = null;
- }finally{
- try{
- if(con != null){
- con.close();
- }
- if(ps != null){
- ps.close();
- }
- }catch (SQLException se) {
- result = null;
- }
- }
- return result;
- }
- public int getAvrageAge(String criteria){
- int result = 0;
- try{
- Class.forName(driver);
- con = DriverManager.getConnection(connectionString, userName, password);
- String query = "SELECT AVG(AGE) AS AVERAGE_AGE FROM TBL_1437159_EMP " +
- " WHERE EMP_NAME LIKE ?%";
- ps = con.prepareStatement(query);
- if(ps!=null){
- ps.setString(1, criteria);
- }
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- result = rs.getInt("AVERAGE_AGE");
- }
- }catch (ClassNotFoundException ce) {
- result = 0;
- }catch (SQLException se) {
- result = 0;
- }finally{
- try{
- if(con != null){
- con.close();
- }
- if(ps != null){
- ps.close();
- }
- }catch (SQLException se) {
- result = 0;
- }
- }
- return result;
- }
- }
Add Comment
Please, Sign In to add comment