Monday 4 May 2009

7-How to Develop Bean-Managed-Persistence Entity Beans

FILE:StudentDAO.java
package bmpex;

import java.util.Collection;

public interface StudentDAO
{
public void create(String id, String firstName,
String lastName, String address)
throws StudentDAOSysException;
public void remove(String studentId)
throws StudentDAOSysException;
public boolean findByPrimaryKey(String studentId)
throws StudentDAOSysException;
public Collection findByLastName(String lastName)
throws StudentDAOSysException;
public int findTotalNumberOfStudents()
throws StudentDAOSysException;
public void store(String id, String firstName,
String lastName, String address)
throws StudentDAOSysException;
public StudentDetails load(String studentId)
throws StudentDAOSysException;
}

FILE: StudenDAOPB.java
package bmpex;

import java.util.*;
import javax.naming.*;
import java.sql.*;
import javax.sql.*;

public class StudentDAOPB implements StudentDAO {
private Connection con;
public StudentDAOPB() {}
public void create(String id, String firstName,
String lastName, String address) throws StudentDAOSysException {
PreparedStatement stmt = null;
try {
getDBConnection();
stmt = con.prepareStatement
("insert into students(student_id, first_name, last_name, "+
" address) values (?, ?, ?, ?)");
stmt.setString(1, id);
stmt.setString(2, firstName);
stmt.setString(3, lastName);
stmt.setString(4, address);
stmt.executeUpdate();
}
catch(SQLException ex) {
throw new StudentDAOSysException("SQLException:"+ ex.getMessage());
}
finally {
closeStatement(stmt);
closeDBConnection();
}
}
public void remove(String studentId)
throws StudentDAOSysException {
PreparedStatement stmt = null;
try {
getDBConnection();
stmt = con.prepareStatement
("delete from students where student_id = ?");
stmt.setString(1,studentId);
stmt.executeUpdate();
stmt.close();
}
catch(SQLException ex) {
throw new StudentDAOSysException("SQLException:"+ ex.getMessage());
}
finally {
closeStatement(stmt);
closeDBConnection();
}
}
public boolean findByPrimaryKey(String studentId)
throws StudentDAOSysException {
boolean result = false;
PreparedStatement stmt = null;
try {
getDBConnection();
stmt = con.prepareStatement
("select student_id from students where student_id = ?");
stmt.setString(1, studentId);
ResultSet rs = stmt.executeQuery();
result = rs.next();
rs.close();
}
catch(SQLException ex) {
throw new StudentDAOSysException("SQLException: "+ ex.getMessage());
}
finally {
closeStatement(stmt);
closeDBConnection();
}
return result;
}
public Collection findByLastName(String lastName)
throws StudentDAOSysException {
Collection students = new ArrayList();
PreparedStatement stmt = null;
try {
getDBConnection();
stmt = con.prepareStatement
("select student_id from students where last_name = ?");
stmt.setString(1, lastName);
ResultSet rs = stmt.executeQuery();
while(rs.next()){
String studentId = rs.getString(1);
students.add(studentId);
}
rs.close();
}
catch(SQLException ex) {
throw new StudentDAOSysException("SQLException: "+ ex.getMessage());
}
finally {
closeStatement(stmt);
closeDBConnection();
}
return students;
}
public int findTotalNumberOfStudents()
throws StudentDAOSysException {
int total = 111;
PreparedStatement stmt = null;
try {
getDBConnection();
stmt = con.prepareStatement
("select count(student_id) from students");
ResultSet rs = stmt.executeQuery();
rs.next();
total = rs.getInt(1);
}
catch(SQLException ex) {
throw new StudentDAOSysException("SQLException:"+ ex.getMessage());
}
finally {
closeStatement(stmt);
closeDBConnection();
}
return total;
}
public StudentDetails load(String studentId)
throws StudentDAOSysException {
StudentDetails student = null;
PreparedStatement stmt = null;
try{
getDBConnection();
stmt = con.prepareStatement ("select first_name, last_name, " +
" address from students where student_id=?");
stmt.setString(1, studentId);
ResultSet rs = stmt.executeQuery();
rs.next();
student = new StudentDetails(studentId,
rs.getString(1),
rs.getString(2),
rs.getString(3));
rs.close();
}
catch(SQLException ex) {
throw new StudentDAOSysException("SQLException:"+ ex.getMessage());
}
finally {
closeStatement(stmt);
closeDBConnection();
}
return student;
}
public void store(String id, String firstName, String lastName,
String address) throws StudentDAOSysException {
PreparedStatement stmt = null;
try{
getDBConnection();
stmt = con.prepareStatement ("update students set "+
"first_name=?,last_name = ?,address = ? where student_id=?");
stmt.setString(1, firstName);
stmt.setString(2,lastName);
stmt.setString(3, address);
stmt.setString(4, id);
stmt.executeUpdate();
stmt.close();
}
catch(SQLException ex) {
throw new StudentDAOSysException("SQLException:"+ ex.getMessage());
}
finally {
closeStatement(stmt);
closeDBConnection();
}
}
private void getDBConnection()
throws StudentDAOSysException {
try {
Context context = new InitialContext();
DataSource ds = (DataSource) context.lookup("java:comp/env/myJDBCDataSource");

// InitialContext initialContext = new InitialContext ();
// Context envContext = (Context) initialContext.lookup ("java:comp/env");
// DataSource ds = (DataSource) envContext.lookup ("jdbc/styejbDB");

con = ds.getConnection();
if(con==null)
System.err.println("Database Connection is null");
}
catch(SQLException ex) {
throw new StudentDAOSysException("\\nSQLException:"+ ex.getMessage() + " " + ex.toString());
}
catch(NamingException ex) {
throw new StudentDAOSysException("\\nNamingException:"
+ ex.getResolvedName()+"-"+ ex.getRemainingName()
+ "-" + ex.toString());

}
}
private void closeDBConnection()
throws StudentDAOSysException {
try {
con.close();
}
catch(SQLException ex) {
throw new StudentDAOSysException("SQLException:"+ ex.getMessage());
}
}
private void closeStatement(PreparedStatement stmt)
throws StudentDAOSysException {
try {
stmt.close();
}
catch(SQLException ex) {
throw new StudentDAOSysException("SQLException:"+ ex.getMessage());
}
}
}

FILE: StudentDAOSysException.java
package bmpex;

public class StudentDAOSysException extends RuntimeException {
public StudentDAOSysException (String str) {
super(str);
}
public StudentDAOSysException () {
super();
}
}