You are on page 1of 6

EmployeeBean.

java
package employee;
import java.io.Serializable;
public class EmployeeBean implements Serializable{
private String empId=null;
private String empName=null;
private String empAddr=null;
private String empEmail=null;
private String empPhone=null;
public EmployeeBean()
{
}
public EmployeeBean(String empId,String empName,String empAddr,String empEmail,
String empPhone)
{
this.empId=empId;
this.empName=empName;
this.empAddr=empAddr;
this.empEmail=empEmail;
this.empPhone=empPhone;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpAddr() {
return empAddr;
}
public void setEmpAddr(String empAddr) {
this.empAddr = empAddr;
}
public String getEmpEmail() {
return empEmail;
}
public void setEmpEmail(String empEmail) {
this.empEmail = empEmail;
}
public String getEmpPhone() {
return empPhone;
}
public void setEmpPhone(String empPhone) {
this.empPhone = empPhone;
}
}
EmployeeJdbcDAO.java
package employee;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import factory.ConnectionFactory;
public class EmployeeJdbcDAO
{
Connection con=null;
PreparedStatement ptmt=null;
ResultSet rs=null;
public EmployeeJdbcDAO()
{
}
private Connection getConnection() throws SQLException
{
Connection conn;
conn=ConnectionFactory.getInstance().getConnection();
return conn;
}
public void add(EmployeeBean employeeBean)
{
try
{
String querystring= INSERT INTO EMPLOYEE VALUES(?,?,?,?,?) ;
con=getConnection();
ptmt=con.prepareStatement(querystring);
ptmt.setString(1, String.valueOf(System.currentTimeMillis()));
ptmt.setString(2, employeeBean.getEmpName());
ptmt.setString(3, employeeBean.getEmpAddr());
ptmt.setString(4, employeeBean.getEmpEmail());
ptmt.setString(5, employeeBean.getEmpPhone());
ptmt.executeUpdate();
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(rs!=null)
rs.close();
if(ptmt!=null)
ptmt.close();
if(con!=null)
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public void update(EmployeeBean employeeBean)
{
try
{
String querystring= UPDATE EMPLOYEE SET EMP_NAME=?,EMP_ADDR=?, +
EMP_EMAIL=?,EMP_PHONE=? WHERE EMP_ID=? ;
con=getConnection();
ptmt=con.prepareStatement(querystring);
ptmt.setString(1, employeeBean.getEmpName());
ptmt.setString(2, employeeBean.getEmpAddr());
ptmt.setString(3, employeeBean.getEmpEmail());
ptmt.setString(4, employeeBean.getEmpPhone());
ptmt.setString(5, employeeBean.getEmpId());
ptmt.executeUpdate();
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(rs!=null)
rs.close();
if(ptmt!=null)
ptmt.close();
if(con!=null)
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public void delete(String employeeId)
{
try
{
String querystring= DELETE FROM EMPLOYEE WHERE EMPID=? ;
con=getConnection();
ptmt=con.prepareStatement(querystring);
ptmt.setString(1, employeeId);
ptmt.executeUpdate();
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(rs!=null)
rs.close();
if(ptmt!=null)
ptmt.close();
if(con!=null)
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public List findAll()
{
List employees=new ArrayList();
EmployeeBean employeeBean=null;
try
{
String querystring= SELECT * FROM EMPLOYEE ;
con=getConnection();
ptmt=con.prepareStatement(querystring);
rs=ptmt.executeQuery();
while(rs.next())
{
employeeBean=new EmployeeBean();
employeeBean.setEmpId(rs.getString(1));
employeeBean.setEmpName(rs.getString(2));
employeeBean.setEmpAddr(rs.getString(3));
employeeBean.setEmpEmail(rs.getString(4));
employeeBean.setEmpPhone(rs.getString(5));
employees.add(employeeBean);
}
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(rs!=null)
rs.close();
if(ptmt!=null)
ptmt.close();
if(con!=null)
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
return employees;
}
public EmployeeBean findByPrimaryKey(String empId)
{
EmployeeBean employeeBean=null;
try
{
String querystring= SELECT * FROM EMPLOYEE WHERE EMP_ID=? ;
con=getConnection();
ptmt=con.prepareStatement(querystring);
ptmt.setString(1, empId);
rs=ptmt.executeQuery();
if(rs.next())
{
employeeBean=new EmployeeBean();
employeeBean.setEmpId(rs.getString(1));
employeeBean.setEmpName(rs.getString(2));
employeeBean.setEmpAddr(rs.getString(3));
employeeBean.setEmpEmail(rs.getString(4));
employeeBean.setEmpPhone(rs.getString(5));

}
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(rs!=null)
rs.close();
if(ptmt!=null)
ptmt.close();
if(con!=null)
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
return employeeBean;
}
public static void main(String[] args) {

EmployeeJdbcDAO employeeDAO=new EmployeeJdbcDAO();


System.out.println(employeeDAO.findAll().size());
}
}

Comments
Leave a comment
Trackback
Written by Kamlesh about 10 months ago.Log in to Reply
Really nice to get understand the connection for all kind
Leave a Comment
You must be logged in to post a comment.
More.....
How to create DSN of Ms Access
How Class.forName method Works
Connection Pooling with eclipse
Jdbc DAO Example
Jdbc

You might also like