jobs4timesLogo jobs4timesLogo

Data Access Object (DAO) Design Pattern

package design.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Logger;

public class ConnectionFactory {

 private static Logger logger;
 
static{
	logger=Logger.getLogger("logfile");
}

public static Connection getConnection(final String driverClassName,final String url,final String userName,final String password) throws SQLException, ClassNotFoundException {
 Connection con=null;

 try{
  Class.forName(driverClassName);
  con=DriverManager.getConnection(url,userName,password);
  con.setAutoCommit(false);
 }catch(SQLException e){
   logger.throwing("ConnectionFactory", "Exception thrown by ConnectionFactory", e);
   throw e;
 }catch(ClassNotFoundException e){
   logger.throwing("ConnectionFactory", "Not able to load driver class", e);
   throw e;
 }

return con;
}

}
package design.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Logger;

public class EmpDao {
	
  //SQL queries
 private final String INSERT_QUERY="insert into emp(emp_id,name) values(?,?)";
 private Logger logger;
 
 public EmpDao(){
  logger=Logger.getLogger("logfile");
 }

public void insert(int id, String name) throws SQLException, ClassNotFoundException {

 boolean isException=false;
 Connection con=null;
 PreparedStatement pstmt=null;

 try{
  con=ConnectionFactory.getConnection("oracle.jdbc.driver.OracleDriver","jdbc:oracle:thin:@localhost:1521:xe","system","tiger");
  pstmt=con.prepareStatement(INSERT_QUERY);
  pstmt.setInt(1, id);
  pstmt.setString(2, name);
  pstmt.executeUpdate();
 }catch(SQLException e){
	 isException=true;
	 logger.throwing("StudentDao", "Unable to get Connection", e);
	 throw e;
 }catch(ClassNotFoundException e) {
	 isException=true;
	 logger.throwing("StudentDao", "Unable to get Connection", e);
	 throw e;
 }finally{
	 
   if(con != null){
	   if(isException==true){
		   con.rollback();
	   }else con.commit();
   
   con.close();   
   }
 
 if(pstmt != null)
	 pstmt.close();
  
	
 }//finally
}
}


/*
create table emp(emp_id number(20),name varchar2(15), sal number(7,2));
*/
package design.dao;

import java.sql.SQLException;

public class EmpController {

public void addEmp(int id, String name) throws ClassNotFoundException, SQLException {

	EmpDao dao=new EmpDao();
	dao.insert(id,name);
}

}
package design.dao;

import java.sql.SQLException;

public class DaoTest {

public static void main(String[] args) throws ClassNotFoundException, SQLException {
	
 EmpController empController=new EmpController();
 empController.addEmp(9,"Ashok");

}

}


BACK