u can use this one also
Code:
public class Dao {
static ConnectionManager c = new ConnectionManager();
static Connection con ;
static Statement st;
static ResultSet rs = null;
static Logger logger = Logger.getLogger(Dao.class);
public static ArrayList<EmpRegBean> Execute1(EmpRegBean empbean) {
PropertyConfigurator.configure(GenericConstants.LOGPATH);
st=null;
String name = empbean.getName();
String role = empbean.getRole();
String phone = empbean.getPhone();
String insertQuery =
"insert into employees values( eid.nextval,'"+ name +"','"+role+"' , '"+phone+ "',eid.nextval,pid.nextval)";
ArrayList<EmpRegBean> empRegList = new ArrayList<EmpRegBean>();
String detailQuery = "select * from employees where employeeid = (select max(employeeid) from employees where ename = '"+name+"' group by ename)";
logger.error("["+DateUtil.getSysDateTime()+"]::Connection Established Successfully :: Dao");
logger.debug("["+DateUtil.getSysDateTime()+"]::Debugging Employee Registration - Execute1 Class");
logger.info("["+DateUtil.getSysDateTime()+"]::Registering Employee & Displaying Registered Details");
try
{
//connect to DB
con = c.getConnection();
st = con.createStatement();
st.executeQuery(insertQuery);
rs = st.executeQuery(detailQuery);
logger.warn("["+DateUtil.getSysDateTime()+"]::Registering Employee - Execute1 Class");
while (rs.next())
{
EmpRegBean b =new EmpRegBean();
String id = rs.getString("employeeid");
String name1 = rs.getString("ename");
String rol = rs.getString("erole");
String ph= rs.getString("ephone");
String uname= rs.getString("username");
String p= rs.getString("pwd");
b.setEmployeeid(id);
b.setRole(rol);
b.setPhone(ph);
b.setName(name1);
b.setUserName(uname);
b.setPwd(p);
empRegList.add(b);
}
logger.warn("["+DateUtil.getSysDateTime()+"]::Displaying Registered Details");
}
catch (SQLException e) {
logger.error("SQLException in Dao :: "+e);
}finally{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
logger.error("SQLException Unable to close connection in Dao :: "+e);
}
}
return empRegList;
}
public static ArrayList<EmpRegBean> EmpSearch(String empId)
{
PropertyConfigurator.configure(GenericConstants.LOGPATH);
ArrayList<EmpRegBean> empViewList = new ArrayList<EmpRegBean>();
st=null;
String searchquery ="select employeeid,ename,erole,ephone from employees where employeeid='"+empId+"'";
String queryfinish="commit";
logger.error("["+DateUtil.getSysDateTime()+"]::Connection Established Successfully :: Dao");
logger.debug("["+DateUtil.getSysDateTime()+"]::Debugging - EmpSearch Class");
logger.info("["+DateUtil.getSysDateTime()+"]::Searching Employee Details from database");
try
{
con = c.getConnection();
st=con.createStatement();
rs = st.executeQuery(searchquery);
logger.warn("["+DateUtil.getSysDateTime()+"]::Searching Employee Details from database - EmpSearch Class");
while (rs.next())
{
EmpRegBean empbean=new EmpRegBean();
String id1 = rs.getString("employeeid");
String sname = rs.getString("ename");
String rol = rs.getString("erole");
String ph= rs.getString("ephone");
empbean.setEmployeeid(id1);
empbean.setRole(rol);
empbean.setPhone(ph);
empbean.setName(sname);
empViewList.add(empbean);
}
logger.warn("["+DateUtil.getSysDateTime()+"]::Displaying Employee Details");
rs = st.executeQuery(queryfinish);
}
catch (SQLException e) {
logger.error("SQLException in Dao :: "+e);
}finally{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
logger.error("SQLException Unable to close connection in Dao :: "+e);
}
}
return empViewList;
}
public static EmpRegBean EmpUpdate(EmpRegBean empbean) {
PropertyConfigurator.configure(GenericConstants.LOGPATH);
st=null;
logger.error("["+DateUtil.getSysDateTime()+"]::Connection Established Successfully :: Dao");
logger.debug("["+DateUtil.getSysDateTime()+"]::Debugging EmpUpdate Class");
logger.info("["+DateUtil.getSysDateTime()+"]::Updating Employee Details in database");
try {
Statement st = null;
con = c.getConnection();
st=con.createStatement();
logger.warn("["+DateUtil.getSysDateTime()+"]::Updating Employee Details in database - EmpUpdate Class");
String name = empbean.getName();
String id = empbean.getEmployeeid();
String role = empbean.getRole();
String pno = empbean.getPhone();
String updateQuery =
"update employees set erole='"+role+"',ephone='"+pno+"',ename='"+name+"' where employeeid="+id+"";
st.executeUpdate(updateQuery);
}
catch (SQLException e) {
logger.error("SQLException in Dao :: "+e);
}finally{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
logger.error("SQLException Unable to close connection in Dao :: "+e);
}
}
return null;
}
public static List<EmpRegBean> EmpViewAll()
{
PropertyConfigurator.configure(GenericConstants.LOGPATH);
ArrayList<EmpRegBean> empAllList = new ArrayList<EmpRegBean>();
st=null;
String viewquery ="select employeeid,ename,erole,ephone from employees order by 1";
String queryfinish="commit";
logger.error("["+DateUtil.getSysDateTime()+"]::Connection Established Successfully :: Dao");
logger.debug("["+DateUtil.getSysDateTime()+"]::Debugging EmpViewAll Class");
logger.info("["+DateUtil.getSysDateTime()+"]::Dispalying All Employees Details");
try
{
con = c.getConnection();
st=con.createStatement();
rs = st.executeQuery(viewquery);
logger.warn("["+DateUtil.getSysDateTime()+"]::Getting Employee details from the Database - EmpViewAll Class");
while (rs.next())
{
EmpRegBean empbean=new EmpRegBean();
String id1 = rs.getString("employeeid");
String sname = rs.getString("ename");
String rol = rs.getString("erole");
String ph= rs.getString("ephone");
empbean.setEmployeeid(id1);
empbean.setRole(rol);
empbean.setPhone(ph);
empbean.setName(sname);
empAllList.add(empbean);
}
logger.warn("["+DateUtil.getSysDateTime()+"]::Dispalying All Employees Details");
rs = st.executeQuery(queryfinish);
}
catch (SQLException e) {
logger.error("SQLException in Dao :: "+e);
}finally{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
logger.error("SQLException Unable to close connection in Dao :: "+e);
}
}
return empAllList;
}
public static EmpRegBean EmpDelete(EmpRegBean empbean) {
PropertyConfigurator.configure(GenericConstants.LOGPATH);
st=null;
String empId = empbean.getEmployeeid();
String deletequery = "delete from employees where employeeid ="+empId+"";
String queryfinish="commit";
logger.error("["+DateUtil.getSysDateTime()+"]::Connection Established Successfully :: Dao");
logger.debug("["+DateUtil.getSysDateTime()+"]::Sample debug message");
logger.info("["+DateUtil.getSysDateTime()+"]::Deleting Employee Details from Database");
try {
con = c.getConnection();
st=con.createStatement();
logger.warn("["+DateUtil.getSysDateTime()+"]::Deleting Employee details from the Database - EmpDelete Class");
st.executeQuery(deletequery);
st.executeQuery(queryfinish);
}
catch (SQLException e) {
logger.error("SQLException in Dao :: "+e);
}finally{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
logger.error("SQLException Unable to close connection in Dao :: "+e);
}
}
return empbean;
}
}