CustomerDAOImpl.java
资源名称:MyStore.rar [点击查看]
上传用户:hgs128
上传日期:2007-02-03
资源大小:166k
文件大小:6k
源码类别:
百货/超市行业
开发平台:
WINDOWS
- /*
- * Created on 2004-4-1
- *
- * To change the template for this generated file go to
- * Window - Preferences - Java - Code Generation - Code and Comments
- */
- package dao;
- import javax.ejb.CreateException;
- import javax.ejb.EJBException;
- import javax.ejb.FinderException;
- import javax.ejb.RemoveException;
- import bmp.CustomerBean;
- import bmp.CustomerPK;
- import javax.naming.InitialContext;
- import javax.sql.DataSource;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- /**
- * @author 28-9
- *
- * To change the template for this generated type comment go to
- * Window - Preferences - Java - Code Generation - Code and Comments
- */
- public class CustomerDAOImpl implements CustomerDAO{
- private DataSource jdbcFactory;
- public CustomerDAOImpl(){
- super();
- }
- /* (non-Javadoc)
- * @see bmp.CustomerDAO#init()
- */
- public void init() {
- System.out.println("Entering CustomerDAOImpl.Init()");
- InitialContext c=null;
- if(this.jdbcFactory==null){
- try {
- c=new InitialContext();
- this.jdbcFactory=(DataSource)c.lookup("java:comp/env/jdbc/OracleDS");
- } catch (Exception e) {
- System.out.println("Error in CustomerDAOImpl.Init()");
- }
- }
- System.out.println("Leaving CustomerDAOImple.Init()");
- }
- /* (non-Javadoc)
- * @see bmp.CustomerDAO#load(bmp.CustomerPK, bmp.CustomerBean)
- */
- public void load(CustomerPK pk, CustomerBean ejb) throws EJBException {
- System.out.println("Entering CustomerDAOImpl.load()");
- Connection conn=null;
- PreparedStatement ps=null;
- ResultSet rs=null;
- try {
- conn=jdbcFactory.getConnection();
- String queryString="select customerid,userid,firstname,lastname,address,phone,"+
- "shareholder_stat from customer where customerid=?";
- ps=conn.prepareStatement(queryString);
- ps.setString(1, pk.getCustomerID());
- rs=ps.executeQuery();
- System.out.println("QueryString is "+queryString);
- if(rs.next()){
- int count=1;
- ejb.setCustomerID((rs.getString(count++)).trim());
- ejb.setUserID((rs.getString(count++)).trim());
- ejb.setFirstName((rs.getString(count++)).trim());
- ejb.setLastName((rs.getString(count++)).trim());
- ejb.setAddress((rs.getString(count++)).trim());
- ejb.setPhone((rs.getString(count++)).trim());
- ejb.setShareholderStatus((rs.getString(count++)).trim());
- }
- } catch (SQLException e) {
- throw new EJBException("Row for id "+pk.getCustomerID()+"not found in database"+e);
- }finally{
- try{
- rs.close();
- ps.close();
- conn.close();
- }catch(Exception e){
- }
- }
- System.out.println("Leaving CustomerDAOImpl.load()");
- }
- /* (non-Javadoc)
- * @see bmp.CustomerDAO#store(bmp.CustomerBean)
- */
- public void store(CustomerBean ejb) throws EJBException {
- System.out.println("Entering CustomerDAOImpl.store()");
- Connection conn=null;
- PreparedStatement ps=null;
- ResultSet rs=null;
- try {
- conn=jdbcFactory.getConnection();
- String updateString="update customer set userid=?,firstname=?,lastname=?,"+
- "address=?,phone=?,shareholder_stat=? where customerid=?";
- 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.getPhone().trim());
- ps.setString(6, ejb.getShareholderStatus().trim());
- 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 CustomerDAOImpl.store()");
- }
- /* (non-Javadoc)
- * @see bmp.CustomerDAO#remove(bmp.CustomerPK)
- */
- public void remove(CustomerPK pk) throws RemoveException, EJBException {
- }
- /* (non-Javadoc)
- * @see bmp.CustomerDAO#create(bmp.CustomerBean)
- */
- public CustomerPK create(CustomerBean ejb) throws CreateException, EJBException {
- return null;
- }
- /* (non-Javadoc)
- * @see bmp.CustomerDAO#findByPrimaryKey(bmp.CustomerPK)
- */
- public CustomerPK findByPrimaryKey(CustomerPK pk) throws FinderException {
- System.out.println("Entering CustomerDAOImpl.findByPrimaryKey()");
- Connection conn=null;
- PreparedStatement ps=null;
- ResultSet rs=null;
- try {
- conn=jdbcFactory.getConnection();
- String queryString="select customerid from customer where customerid=?";
- ps=conn.prepareStatement(queryString);
- String key=pk.getCustomerID();
- 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 CustomerDAOImpl.findByPrimaryKey()"+
- "following primarykey "+pk.getCustomerID()+" not found.");
- }finally{
- try {
- rs.close();
- ps.close();
- conn.close();
- } catch (Exception e) {
- }
- }
- System.out.println("Leaving CustomerDAOImpl.findByPrimaryKey()"+pk.getCustomerID());
- return pk;
- }
- /* (non-Javadoc)
- * @see bmp.CustomerDAO#findByUserID(java.lang.String)
- */
- public CustomerPK findByUserID(String userID) throws FinderException {
- System.out.println("Entering CustomerDAOImpl.findByUserID()");
- Connection conn=null;
- PreparedStatement ps=null;
- ResultSet rs=null;
- CustomerPK pk=new CustomerPK();
- try {
- conn=jdbcFactory.getConnection();
- String queryString="select customerid from customer where userid=?";
- ps=conn.prepareStatement(queryString);
- ps.setString(1, userID);
- rs=ps.executeQuery();
- boolean result=rs.next();
- if(result){
- pk.setCustomerID(rs.getString(1));
- System.out.println("Primary Key found: "+pk.getCustomerID());
- }
- } catch (Exception e) {
- e.printStackTrace();
- throw new FinderException("Inside CustomerDAOImpl.findbyUserID()");
- }finally{
- try {
- rs.close();
- ps.close();
- conn.close();
- } catch (Exception e) {
- }
- }
- System.out.println("Leaving CustomerDAOImpl.findByUserID() with key "+pk.getCustomerID());
- return pk;
- }
- }