ManagerDAOImpl.java
资源名称:MyStore.rar [点击查看]
上传用户:hgs128
上传日期:2007-02-03
资源大小:166k
文件大小:6k
源码类别:
百货/超市行业
开发平台:
WINDOWS
- /*
- * Created on 1999-5-17
- */
- package dao;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import javax.ejb.CreateException;
- import javax.ejb.EJBException;
- import javax.ejb.FinderException;
- import javax.ejb.RemoveException;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- import javax.sql.DataSource;
- import bmp.ManagerBean;
- import bmp.ManagerPK;
- /**
- * @author 28-9
- *
- * TODO To change the template for this generated type comment go to
- * Window - Preferences - Java - Code Generation - Code and Comments
- */
- public class ManagerDAOImpl implements ManagerDAO{
- public DataSource jdbcFactory;
- public ManagerDAOImpl() {
- }
- /* (non-Javadoc)
- * @see dao.ManagerDAO#init()
- */
- public void init() {
- InitialContext c=null;
- System.out.println (" Entering ManageDAOImpl.init() ");
- if(this.jdbcFactory==null) {
- try {
- c=new InitialContext();
- this.jdbcFactory=(DataSource)c.lookup("java:comp/env/jdbc/OracleDS");
- } catch (NamingException e) {
- System.out.println ("Error in ManagerDAOImpl.init() ");
- }
- }
- System.out.println (" Leaving ManagerDAOImpl.init() ");
- }
- /* (non-Javadoc)
- * @see dao.ManagerDAO#load(bmp.ManagerPK, bmp.ManagerBean)
- */
- public void load(ManagerPK pk, ManagerBean ejb) throws EJBException {
- System.out.println (" Entering ManagerDAOImpl.load() ");
- Connection conn=null;
- PreparedStatement ps=null;
- ResultSet rs=null;
- try {
- conn=this.jdbcFactory.getConnection();
- String queryString = "select managerid, userid, firstname, lastname, address, message,"+
- "salary from manager where managerid = ?";
- ps=conn.prepareStatement(queryString);
- ps.setString(1, pk.getManagerID());
- rs=ps.executeQuery();
- System.out.println ("QueryString is " + queryString) ;
- if(rs.next()) {
- ejb.setManagerID(rs.getString("managerid").trim());
- ejb.setUserID(rs.getString("userid").trim());
- ejb.setFirstName(rs.getString("firstname").trim());
- ejb.setLastName(rs.getString("lastname").trim());
- ejb.setAddress(rs.getString("address").trim());
- ejb.setMessage(rs.getString("message").trim());
- ejb.setSalary(rs.getFloat("salary"));
- }
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- throw new EJBException("Row for id " + pk.managerID + " not found in database" + e);
- }finally {
- try {
- rs.close();
- ps.close();
- conn.close();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- System.out.println (" Leaving ManagerDAOImpl.load() ") ;
- }
- /* (non-Javadoc)
- * @see dao.ManagerDAO#store(bmp.ManagerBean)
- */
- public void store(ManagerBean ejb) throws EJBException {
- System.out.println (" Entering ManagerDAOImpl.store() ");
- Connection conn = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- conn = jdbcFactory.getConnection();
- String updateString = "update customer set userid = ?, firstname = ?,lastname = ?," +"" +
- "address = ?, message = ?, salary = ? where managerid = ?";
- ps = conn.prepareStatement(updateString);
- ps.setString (1,ejb.getUserID().trim());
- ps.setString(2,ejb.getFirstName().trim());
- ps.setString(3,ejb.getLastName().trim());
- ps.setString(4,ejb.getAddress().trim());
- ps.setString(5,ejb.getMessage().trim());
- ps.setFloat (6, ejb.getSalary());
- int count = ps.executeUpdate();
- System.out.println ("Update String is " + updateString) ;
- }
- catch (Exception e ) {
- e.printStackTrace();
- }
- finally {
- try {
- ps.close();
- rs.close();
- conn.close();
- }catch (Exception e) {
- }
- }
- System.out.println (" Leaving ManagerDAOImpl.store() ");
- }
- /* (non-Javadoc)
- * @see dao.ManagerDAO#remove(bmp.ManagerPK)
- */
- public void remove(ManagerPK pk) throws RemoveException, EJBException {
- }
- /* (non-Javadoc)
- * @see dao.ManagerDAO#create(bmp.ManagerBean)
- */
- public ManagerPK create(ManagerBean ejb) throws CreateException, EJBException {
- return null;
- }
- /* (non-Javadoc)
- * @see dao.ManagerDAO#findByPrimaryKey(bmp.ManagerPK)
- */
- public ManagerPK findByPrimaryKey(ManagerPK pk) throws FinderException {
- System.out.println (" Entering ManagerDAOImpl.findByPrimaryKey() ");
- Connection conn = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- conn = jdbcFactory.getConnection();
- String queryString = "select managerid from manager where managerid = ?";
- ps = conn.prepareStatement(queryString);
- String key = pk.getManagerID();
- ps.setString (1, key);
- rs = ps.executeQuery();
- boolean result =rs .next();
- if ( result) {
- System.out.println (" Primary Key found");
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- throw new FinderException("Inside ManagerDAOImpl.findbyPrimaryKey()" +
- " following primarykey " + pk.getManagerID() + "notfound ");
- }
- finally {
- try {
- rs.close();
- ps.close();
- conn.close();
- }
- catch(Exception e ) {
- }
- }
- System.out.println (" Leaving ManagerDAOImpl.findByPrimaryKey() " + pk.getManagerID());
- return pk;
- }
- /* (non-Javadoc)
- * @see dao.ManagerDAO#findByUserID(java.lang.String)
- */
- public ManagerPK findByUserID(String userID) throws FinderException {
- System.out.println (" Entering ManagerDAOImpl.findByUserID() ");
- Connection conn = null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- ManagerPK pk = new ManagerPK() ;
- try {
- conn = jdbcFactory.getConnection();
- String queryString = "select managerid from manager where userid = ?";
- ps = conn.prepareStatement(queryString);
- ps.setString (1, userID);
- rs = ps.executeQuery();
- boolean result =rs .next();
- if ( result) {
- pk.setManagerID( rs.getString(1) );
- System.out.println (" Primary Key found :" + pk.getManagerID() );
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- throw new FinderException("Inside ManagerDAOImpl.findbyPrimaryKey()" + e);
- }
- finally {
- try {
- rs.close();
- ps.close();
- conn.close();
- }
- catch(Exception e ) {
- }
- }
- System.out.println (" Leaving ManagerDAOImpl.findByUserID() with key " + pk.getManagerID());
- return pk;
- }
- }