课程设计实训教学示例项目——《网上银行账户管理系统》——根据省名获得该省中的各个城市名称的DAO组件
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
基于Struts+Spring+JDBC架构的课程设计实训教学示例项目——《网上银行账户管理系统》——根据省名获得该省中的各个城市名称的DAO 组件
1.1.1提供根据省名获得该省中的各个城市名称的DAO组件
1、提供StateCityManageDAOInterface接口
(1)接口名称为StateCityManageDAOInterface,包名称为com.px1987.webbank.dao.inter,并且继承于com.px1987.webbank.dao.inter.AllDAOBaseInterface接口
(2)设计该接口
package com.px1987.webbank.dao.inter;
import java.util.List;
import eFromPO;
import com.px1987.webbank.exception.WebBankException;
public interface StateCityManageDAOInterface extends AllDAOBaseInterface{
public List<ComeFromPO> selectAllCityNameInOneState(String stateName) throws WebBankException;
}
2、提供StateCityManageDAOInterface接口的实现类StateCityManageDAOJDBCImple
(1)新增加StateCityManageDAOJDBCImple类,包名称为com.px1987.webbank.dao.imple,
并且从java.util.Observable类继承
(2)编程该实现类
package com.px1987.webbank.dao.imple;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import com.px1987.webbank.config.ClassNameConfig;
import com.px1987.webbank.dao.inter.ConnectDBInterface;
import com.px1987.webbank.dao.inter.StateCityManageDAOInterface;
import eFromPO;
import com.px1987.webbank.exception.WebBankException;
import com.px1987.webbank.factory.ConnectDBFactory;
import com.px1987.webbank.factory.GenerialFactory;
import com.px1987.webbank.factory.LogInfoFactory;
public class StateCityManageDAOJDBCImple extends Observable implements
StateCityManageDAOInterface { Connection con=null;
public void setCon(Connection con) {
this.con = con;
}
ConnectDBInterface connectDBBean=null;
String connectDBBeanClassName=null;
/* 采用工厂方式实现时 */
public StateCityManageDAOJDBCImple(){
connectDBBeanClassName=
ClassNameConfig.getProperty("connectDBBean.className");
}
public Connection getConnection() throws WebBankException
{
// connectDBBean=new
ConnectDBFactory().newConnectDBBean(connectDBBeanClassName);
connectDBBean = (ConnectDBInterface)new GenerialFactory().newSomeOneInstance(connectDBBeanClassName);
con=connectDBBean.getConnection();
return con;
}
/* 利用 Spring IOC获得目标对象
public StateCityManageDAOJDBCImple(){
}
DataSource dataSource=null;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public Connection getConnection() throws WebBBSException{
if(dataSource==null){
int logImpleKind=
Integer.parseInt(ClassNameConfig.getProperty("logImpleKind"));
this.addObserver(LogInfoFactory.newLogInstance(logImpleKind));
this.setChanged(); //注意要设置变化点
notifyObservers(e); //当出现异常时将通知各个观察者
throw new WebBBSException("由Spring 所提供的DataSource对象为空!");
}
try{
con=dataSource.getConnection();
}
catch(SQLException e){
int logImpleKind=
Integer.parseInt(ClassNameConfig.getProperty("logImpleKind"));
this.addObserver(LogInfoFactory.newLogInstance(logImpleKind));
this.setChanged(); //注意要设置变化点
notifyObservers(e); //当出现异常时将通知各个观察者
throw new WebBBSException("在getConnection方法中出现连接数据库错误!,异常的详细内容为:"+e.getMessage());
}
return con;
}
*/
public List<ComeFromPO> selectAllCityNameInOneState(String stateName)
throws WebBankException {
List<ComeFromPO> allComeFromPOs=new ArrayList<ComeFromPO>();
ResultSet rs=null;
ComeFromPO oneComeFromPO=null;
PreparedStatement pstmt =null;
String selectStatement="select * from comeFrom where stateName=?";
con=getConnection();
try{
try {
pstmt = con.prepareStatement(selectStatement,
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
} catch (SQLException e) {
int logImpleKind=
Integer.parseInt(ClassNameConfig.getProperty("logImpleKind"));
this.addObserver(LogInfoFactory.newLogInstance(logImpleKind));
this.setChanged(); //注意要设置变化点
notifyObservers(e);
throw new WebBankException("SQL语句可能不正确!系统原始的异常信息的详细内容为:"+e.getMessage());
}
try{
pstmt.setString(1, stateName);
}
catch (SQLException e) {
int logImpleKind=
Integer.parseInt(ClassNameConfig.getProperty("logImpleKind"));
this.addObserver(LogInfoFactory.newLogInstance(logImpleKind));
this.setChanged(); //注意要设置变化点
notifyObservers(e); //当出现异常时将通知各个观察者
throw new WebBankException("在设置SQL查询参数中出现了错误!系统原始的异常信息的详细内容为:"+e.getMessage());
}
try {
rs=pstmt.executeQuery();
} catch (SQLException e) {
int logImpleKind=
Integer.parseInt(ClassNameConfig.getProperty("logImpleKind"));
this.addObserver(LogInfoFactory.newLogInstance(logImpleKind));
this.setChanged(); //注意要设置变化点
notifyObservers(e); //当出现异常时将通知各个观察者
throw new WebBankException("在StateCityManageDAOJDBCImple类中的selectAllCityNameInOneState方法中在查询数据库表时出现错误!,异常的详细内容为:"+e.getMessage());
}
try{
while(rs.next()){
oneComeFromPO=new ComeFromPO();
oneComeFromPO.setComeFromID(rs.getString("comeFromID"));
oneComeFromPO.setStateName(rs.getString("stateName"));
oneComeFromPO.setCityName(rs.getString("cityName"));
allComeFromPOs.add(oneComeFromPO);
}
}
catch (SQLException e) {
int logImpleKind=
Integer.parseInt(ClassNameConfig.getProperty("logImpleKind"));
this.addObserver(LogInfoFactory.newLogInstance(logImpleKind));
this.setChanged(); //注意要设置变化点
notifyObservers(e);
throw new WebBankException("在StateCityManageDAOJDBCImple类中的selectAllCityNameInOneState方法中结果集操作中出现了错误!系统原始的异常信息的详细内容为:"+e.getMessage());
}
}
finally{
try{
con.close();
}
catch(SQLException e){
int logImpleKind=
Integer.parseInt(ClassNameConfig.getProperty("logImpleKind"));
this.addObserver(LogInfoFactory.newLogInstance(logImpleKind));
this.setChanged(); //注意要设置变化点
notifyObservers(e); //当出现异常时将通知各个观察者
throw new WebBankException("在selectAllCityNameInOneState方法中不能正确地关闭数据库连接!,异常的详细内容为:"+e.getMessage());
}
}
return allComeFromPOs;
}
}
3、添加一个工厂类,用来统一创建StateCityManageDAOInterface接口的对象实例(1)StateCityManageDAOFactory类,包名称为com.px1987.webbank.factory ,并且从java.util.Observable类继承
(2)编程该StateCityManageDAOFactory类
package com.px1987.webbank.factory;
import java.util.Observable;
import com.px1987.webbank.config.ClassNameConfig;
import com.px1987.webbank.dao.inter.StateCityManageDAOInterface;
import com.px1987.webbank.exception.WebBankException;
public class StateCityManageDAOFactory extends Observable {
public StateCityManageDAOFactory() {
}
public StateCityManageDAOInterface newStateCityManageDAOBean(String stateCityManageDAOJDBCImpleClassName) throws WebBankException{
StateCityManageDAOInterface oneStateCityManageDAOJDBCImple=null;
Class oneClass=null;
try {
oneClass = Class.forName(stateCityManageDAOJDBCImpleClassName);
}
catch (ClassNotFoundException e){
int logImpleKind=
Integer.parseInt(ClassNameConfig.getProperty("logImpleKind"));
this.addObserver(LogInfoFactory.newLogInstance(logImpleKind));
this.setChanged(); //注意要设置变化点
notifyObservers(e); //当出现异常时将通知各个观察者
throw new WebBankException("不能正确地获得"+stateCityManageDAOJDBCImpleClassName+"类");
}
try {
oneStateCityManageDAOJDBCImple =
(StateCityManageDAOInterface)oneClass.newInstance();
}
catch (InstantiationException e){
int logImpleKind=
Integer.parseInt(ClassNameConfig.getProperty("logImpleKind"));
this.addObserver(LogInfoFactory.newLogInstance(logImpleKind));
this.setChanged(); //注意要设置变化点
notifyObservers(e); //当出现异常时将通知各个观察者
throw new WebBankException("不能正确地创建"+stateCityManageDAOJDBCImpleClassName+"类的对象实例");
}
catch (IllegalAccessException e){
int logImpleKind=
Integer.parseInt(ClassNameConfig.getProperty("logImpleKind"));
this.addObserver(LogInfoFactory.newLogInstance(logImpleKind));
this.setChanged(); //注意要设置变化点
notifyObservers(e); //当出现异常时将通知各个观察者
throw new WebBankException("不能正确地创建"+stateCityManageDAOJDBCImpleClassName+"类的对象实例");
}
return oneStateCityManageDAOJDBCImple;
}
}
杨教授大学堂精心创作的优秀程序员职业提升必读系列资料
4、修改classNameConfig.xml以添加对StateCityManageDAOJDBCImple类的定义
<className>
<classNameID>stateCityManageDAOJDBCImple.className</classNameID> <classNamePath>
com.px1987.webbank.dao.imple.StateCityManageDAOJDBCImple </classNamePath>
</className>
杨教授大学堂,版权所有,盗版必究。
11/11页。