📄 accountdaoimpl.java~
字号:
/* * $Id: AccountDAOImpl.java~,v 1.1 2002/01/04 21:04:48 jc123804 Exp $ * Copyright 2001 Sun Microsystems, Inc. All rights reserved. * Copyright 2001 Sun Microsystems, Inc. Tous droits r閟erv閟. */package com.sun.j2ee.blueprints.customer.account.dao;import java.sql.Connection;//import java.sql.Statement;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Iterator;import javax.sql.DataSource;import javax.naming.InitialContext;import javax.naming.Context;import javax.naming.NamingException;import com.sun.j2ee.blueprints.customer.util.JNDINames;import com.sun.j2ee.blueprints.customer.account.dao.AccountDAO;import com.sun.j2ee.blueprints.customer.util.DatabaseNames;import com.sun.j2ee.blueprints.customer.util.ContactInformation;import com.sun.j2ee.blueprints.customer.util.Address;import com.sun.j2ee.blueprints.customer.account.model.AccountModel;import com.sun.j2ee.blueprints.customer.account.exceptions.AccountDAOSysException;import com.sun.j2ee.blueprints.customer.account.exceptions.AccountDAOAppException;import com.sun.j2ee.blueprints.customer.account.exceptions.AccountDAODBUpdateException;import com.sun.j2ee.blueprints.customer.account.exceptions.AccountDAOFinderException;import com.sun.j2ee.blueprints.customer.account.exceptions.AccountDAODupKeyException;import com.sun.j2ee.blueprints.util.tracer.Debug;/** * This class implements AccountDAO for Oracle, Sybase and cloudscape databases * This class encapsulates all the JDBC calls made by the AccountEJB. * Actual logic of inserting/fetching/updating/deleting the data in * relational database tables to mirror the state of AccountEJB is * implemented here. */public class AccountDAOImpl implements AccountDAO { private transient Connection dbConnection = null; private transient DataSource datasource = null; public AccountDAOImpl() throws AccountDAOSysException { try { InitialContext ic = new InitialContext(); datasource = (DataSource) ic.lookup(JNDINames.ESTORE_DATASOURCE); } catch (NamingException ne) { throw new AccountDAOSysException("Naming Exception while looking " + " up DataSource Connection " + JNDINames.ESTORE_DATASOURCE + ": \n" + ne.getMessage()); } } public void create(AccountModel details) throws AccountDAOSysException, AccountDAODupKeyException, AccountDAODBUpdateException, AccountDAOAppException { insertAccount(details); } public AccountModel load(String id) throws AccountDAOSysException, AccountDAOFinderException { return(selectAccount(id)); } public void store(AccountModel details) throws AccountDAODBUpdateException, AccountDAOAppException, AccountDAOSysException { updateAccount(details); } public void remove(String id) throws AccountDAODBUpdateException, AccountDAOSysException { deleteAccount(id); } public String findByPrimaryKey(String userId) throws AccountDAOFinderException, AccountDAOSysException { if (userExists(userId)) return (userId); throw new AccountDAOFinderException("primary key not found :"+userId); } private boolean userExists (String userId) throws AccountDAOSysException { PreparedStatement stmt = null; ResultSet result = null; boolean returnValue = false; String queryStr ="SELECT userid FROM " + DatabaseNames.ACCOUNT_TABLE + " WHERE userid = " + "'" + userId.trim() + "'"; Debug.println("queryString is: "+ queryStr); try { getDBConnection(); stmt = createPreparedStatement(dbConnection, queryStr); result = stmt.executeQuery(); if ( !result.next() ) { returnValue = false; } else { userId = result.getString(1); returnValue = true; } } catch(SQLException se) { throw new AccountDAOSysException( "SQLException while checking for an" + " existing user - id -> " + userId + " :\n" + se); } finally { closeResultSet(result); closeStatement(stmt); closeConnection(); } return returnValue; } private boolean isValidData(String userId, ContactInformation info) { if ( (userId == null) || ( info.getEMail() == null) || (info.getGivenName() == null) || (info.getFamilyName() == null) || (info.getAddress().getStreetName1() == null) || (info.getAddress().getCity() == null) || (info.getAddress().getState() == null) || (info.getAddress().getZipCode() == null) || (info.getAddress().getCountry() == null) || (info.getTelephone() == null) ) return (false); else return (true); } private void insertAccount(AccountModel details) throws AccountDAOSysException, AccountDAODupKeyException, AccountDAODBUpdateException, AccountDAOAppException { if (!isValidData(details.getUserId(), details.getContactInformation())) throw new AccountDAOAppException("Illegal data values for insert"); if (userExists(details.getUserId())) throw new AccountDAODupKeyException("Account exists for "+ details.getUserId()); PreparedStatement stmt = null; ContactInformation info = details.getContactInformation(); String queryStr = "INSERT INTO " + DatabaseNames.ACCOUNT_TABLE + "(userid,email,firstname,lastname,status," + "addr1,addr2,city,state,zip,country," + "phone)" + "VALUES (" + "'" + details.getUserId().trim() + "'," + "'" + info.getEMail().trim() + "'," + "'" + info.getGivenName().trim() + "'," + "'" + info.getFamilyName().trim() + "'," + "'" + details.getStatus().trim() + "'," + "'" + info.getAddress().getStreetName1().trim() +"',"; if (info.getAddress().getStreetName2() != null) queryStr += "'"+info.getAddress().getStreetName2().trim() +"',"; else queryStr += "' ',"; queryStr += "'" + info.getAddress().getCity().trim() + "'," + "'" + info.getAddress().getState().trim() + "'," + "'" + info.getAddress().getZipCode().trim() + "'," + "'" + info.getAddress().getCountry().trim() + "'," + "'" + info.getTelephone().trim() + "' )"; Debug.println("queryString is: "+ queryStr); try { getDBConnection(); stmt = createPreparedStatement(dbConnection, queryStr); int resultCount = stmt.executeUpdate(); if ( resultCount != 1 ) { throw new AccountDAODBUpdateException( "ERROR in ACCOUNT_TABLE INSERT !! resultCount = " + resultCount); } } catch(SQLException ae) { throw new AccountDAOSysException( "SQLException while inserting new " + "account; id = " + details.getUserId() + " :\n" + ae); } finally { closeStatement(stmt); closeConnection(); } } private AccountModel selectAccount(String userId) throws AccountDAOSysException, AccountDAOFinderException { PreparedStatement stmt = null; ResultSet result = null; String queryStr = "SELECT "+ "userid,status,email,firstname,lastname,"+ "addr1,addr2,city,state,zip,country,phone"+ " FROM " + DatabaseNames.ACCOUNT_TABLE + " WHERE userid = " + "'" + userId.trim() + "'"; Debug.println("queryString is: "+ queryStr);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -