📄 customerdao.java
字号:
package com.jacksonreed;
import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import javax.sql.*;
/**
* This class is an example of data access object (DAO) for the
* customerBean. It wraps all the database calls and insulates
* the business logic implemented in CustomerBean from the underlying
* physical database store.
*
* Part of this insulation is via implementing the DataAccess.java interface
*
* @author Paul Reed, Jackson-Reed, Inc. <prreed@jacksonreed.com>
* @version 1.0
*/
public class CustomerDAO implements Serializable, DataAccess {
private transient TransactionContext globalTran = null;
private transient CustomerValue custVal = null;
/**
* Class constructor
*/
public CustomerDAO(TransactionContext transactionContext) {
globalTran = transactionContext;
}
/**
* Returns an CustomerValue initialized with the information
* found in the database for the specified customer, or null if
* not found.
*/
public Object findByName(String name)
throws DAOSysException, DAOFinderException {
log("CustomerDAO: start findByCustomerNumber");
if (itemExistsByName(name)) {
log("CustomerDAO: returning " + custVal.getCustomerId());
return custVal;
}
throw new DAOFinderException ("Customer Not Found = "
+ name);
}
private boolean itemExistsByName(String customerNumber) throws DAOSysException {
String queryStr ="SELECT customerId, customerNumber, firstName " +
",middleInitial, prefix, suffix, lastName, phone1, phone2 " +
",eMail " +
"FROM T_Customer " +
"WHERE customerNumber = " + "'" + customerNumber.trim() + "'";
return doQuery(queryStr);
}
/**
* Returns an CustomerValue initialized with the information
* found in the database for the specified customer, or null if
* not found.
*/
public Object findByPrimaryKey(Integer id)
throws DAOSysException, DAOFinderException {
if (itemExistsById(id)) {
log("CustomerDAO: returning " + custVal.getCustomerId());
return custVal;
}
throw new DAOFinderException ("Customer Not Found = "
+ id);
}
private boolean itemExistsById(Integer customerId) throws DAOSysException {
String queryStr ="SELECT customerId, customerNumber, firstName " +
",middleInitial, prefix, suffix, lastName, phone1, phone2 " +
",eMail " +
"FROM T_Customer " +
"WHERE customerId = " + customerId;
return doQuery(queryStr);
}
public void deleteObject(Integer id) throws
DAOSysException, DAODBUpdateException {
String queryStr = "DELETE FROM " + "T_Customer" +
" WHERE customerId = "
+ id;
log("queryString is: "+ queryStr);
Statement stmt = null;
try {
stmt = globalTran.getDBConnection().createStatement();
int resultCount = stmt.executeUpdate(queryStr);
if ( resultCount != 1 )
throw new DAODBUpdateException("ERROR deleteing Customer from" + " Customer_TABLE!! resultCount = " + resultCount);
} catch(SQLException se) {
throw new DAOSysException("Unable to delete for item " + id + "\n" + se);
} finally {
globalTran.closeStatement(stmt);
log("CustomerDAO: closeStatement");
}
}
public void updateObject(Object model) throws
DAOSysException, DAODBUpdateException {
CustomerValue custVal = (CustomerValue) model;
PreparedStatement stmt = null;
try {
String queryStr = "UPDATE " + "T_Customer" +
" SET " + "customerNumber = ?, " +
"firstName = ?, " +
"middleInitial = ?, " +
"prefix = ?, " +
"suffix = ?, " +
"lastName = ?, " +
"phone1 = ?, " +
"phone2 = ?, " +
"eMail = ? " +
"WHERE customerId = ?";
log("CustomerDAO: Update String " + queryStr);
stmt = globalTran.getDBConnection().prepareStatement(queryStr);
log("CustomerDAO: Prepare Worked ");
int i = 1;
stmt.setString(i++, custVal.getCustomerNumber());
log("CustomerDAO: Setting items " + custVal.getCustomerNumber());
stmt.setString(i++, custVal.getFirstName());
stmt.setString(i++, custVal.getMiddleInitial());
stmt.setString(i++, custVal.getPrefix());
stmt.setString(i++, custVal.getSuffix());
stmt.setString(i++, custVal.getLastName());
stmt.setString(i++, custVal.getPhone1());
stmt.setString(i++, custVal.getPhone2());
stmt.setString(i++, custVal.getEMail());
stmt.setInt(i++, custVal.getCustomerId().intValue());
log("CustomerDAO: done setting items ");
int resultCount = stmt.executeUpdate();
if ( resultCount != 1 ) {
log("CustomerDAO: update failed");
throw new DAODBUpdateException ("ERROR updating Customer in" + " Customer_TABLE!! resultCount = " + resultCount);
}
log("CustomerDAO: update worked...result count = " + resultCount);
} catch(SQLException se) {
throw new DAOSysException("Unable to update item " + custVal.getCustomerId() + " \n" + se);
} finally {
globalTran.closeStatement(stmt);
log("CustomerDAO: closeStatement");
}
}
public void insertObject(Object model) throws
DAOSysException, DAODBUpdateException {
CustomerValue custVal = (CustomerValue) model;
PreparedStatement stmt = null;
try {
String queryStr = "INSERT INTO " + "T_Customer" + " (" +
"customerId, " +
"customerNumber, " +
"firstName, " +
"middleInitial, " +
"prefix, " +
"suffix, " +
"lastName, " +
"phone1, " +
"phone2, " +
"eMail) " +
"VALUES " +
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
log("CustomerDAO: Insert String " + queryStr);
stmt = globalTran.getDBConnection().
prepareStatement(queryStr);
int i = 1;
stmt.setInt(i++, custVal.getCustomerId().intValue());
stmt.setString(i++, custVal.getCustomerNumber());
stmt.setString(i++, custVal.getFirstName());
stmt.setString(i++, custVal.getMiddleInitial());
stmt.setString(i++, custVal.getPrefix());
stmt.setString(i++, custVal.getSuffix());
stmt.setString(i++, custVal.getLastName());
stmt.setString(i++, custVal.getPhone1());
stmt.setString(i++, custVal.getPhone2());
stmt.setString(i++, custVal.getEMail());
int resultCount = stmt.executeUpdate();
if ( resultCount != 1 )
throw new DAODBUpdateException ("ERROR inserting Customer in" + " Customer_TABLE!! resultCount = " + resultCount);
} catch(SQLException se) {
throw new DAOSysException("Unable to insert item " + custVal.getCustomerId() + " \n" + se);
} finally {
globalTran.closeStatement(stmt);
log("CustomerDAO: closeStatement");
}
}
private boolean doQuery (String qryString) throws DAOSysException {
Statement stmt = null;
ResultSet result = null;
boolean returnValue = false;
try {
log("CustomerDAO: before statement creationg");
stmt = globalTran.getDBConnection().createStatement();
log("CustomerDAO: statement created");
log("CustomerDAO: query is" + qryString);
result = stmt.executeQuery(qryString);
log("CustomerDAO: statement executed");
if ( !result.next() ) {
log("CustomerDAO: customer not found");
returnValue = false;
}
else {
log("CustomerDAO: customer found");
custVal = new CustomerValue();
int i = 1;
custVal.setCustomerId(new Integer(result.getInt(i++)));
custVal.setCustomerNumber(result.getString(i++));
custVal.setFirstName(result.getString(i++));
custVal.setMiddleInitial(result.getString(i++));
custVal.setPrefix(result.getString(i++));
custVal.setSuffix(result.getString(i++));
custVal.setLastName(result.getString(i++));
custVal.setPhone1(result.getString(i++));
custVal.setPhone2(result.getString(i++));
custVal.setEMail(result.getString(i++));
returnValue = true;
}
} catch(SQLException se) {
se.printStackTrace();
throw new DAOSysException("Unable to Query for item " +
"\n" + se);
} finally {
log("CustomerDAO: prior");
globalTran.closeResultSet(result);
log("CustomerDAO: closeResult");
globalTran.closeStatement(stmt);
log("CustomerDAO: closeStatement");
}
return returnValue;
}
private void closeResultSet(ResultSet result) throws DAOSysException {
try {
if (result != null) {
result.close();
}
} catch (SQLException se) {
throw new DAOSysException("SQL Exception while closing " +
"Result Set : \n" + se);
}
}
private void closeStatement(Statement stmt) throws DAOSysException {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se) {
throw new DAOSysException("SQL Exception while closing " +
"Statement : \n" + se);
}
}
private void log(String s) {
System.out.println(s);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -