在DAO设计模式中使用工厂设计

工厂设计:
    使用工厂设计,使前台不用知道具体子类是谁,是前台显示与后台逻辑操作分离,便于修改。

实例:在DAO设计模式中加入工厂设计

DAOFactory.java:使用工厂设计设置子类

view source
<embed id="highlighter_772179_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://mgc.ahau.edu.cn/common/SyntaxHighlighter/scripts/clipboard.swf" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_772179" menu="false"></embed>
print ?
01. package  mgc.dao.factory;
02.  
03. import  mgc.dao.test.*;
04. import  mgc.dao.test.impl.*;
05. public  class  DAOFactory {
06.      public  static  MemberDAO getMemberDAOInstance() {
07.          return  new  MemberDAOImpl();
08.      }
09. }



MemberDAO.java:数据库操作接口类

view source
<embed id="highlighter_76257_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://mgc.ahau.edu.cn/common/SyntaxHighlighter/scripts/clipboard.swf" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_76257" menu="false"></embed>
print ?
01. package  mgc.dao.test;
02.  
03. import  java.util.*;
04. import  mgc.dao.vo.*;
05. //规定操作member表的全部方法
06. public  interface  MemberDAO {
07.      //插入数据
08.      public  void  insert(Member member)  throws  Exception;
09.      //修改数据
10.      public  void  update(Member member)  throws  Exception;
11.      //删除数据
12.      public  void  delete( int  id)  throws  Exception;
13.      //按ID查询
14.      public  Member queryById( int  id)  throws  Exception;
15.      //全部查询
16.      public  List queryAll()  throws  Exception;
17. }



Member.java:VO对象类

view source
<embed id="highlighter_308880_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://mgc.ahau.edu.cn/common/SyntaxHighlighter/scripts/clipboard.swf" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_308880" menu="false"></embed>
print ?
01. package  mgc.dao.vo;
02.  
03. public  class  Member {
04.      private  int  id;
05.      private  String username;
06.      private  String password;
07.       
08.      public  void  setId( int  id){
09.          this .id=id;
10.      }
11.      public  void  setUsername(String username){
12.          this .username=username;
13.      }
14.      public  void  setPassword(String password){
15.          this .password=password;
16.      }
17.      public  int  getId(){
18.          return  this .id;
19.      }
20.      public  String getUsername(){
21.          return  this .username;
22.      }
23.      public  String getPassword(){
24.          return  this .password;
25.      }
26. }



DatabaseConnection.java:数据库连接类

view source
<embed id="highlighter_592693_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://mgc.ahau.edu.cn/common/SyntaxHighlighter/scripts/clipboard.swf" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_592693" menu="false"></embed>
print ?
01. package  mgc.dao.test.dbc;
02.  
03. import  java.sql.*;
04.  
05. public  class  DataBaseConnection {
06.       
07.      private  final  String DBDRIVER= "sun.jdbc.odbc.JdbcOdbcDriver" ;
08.      private  final  String DBURL= "jdbc:odbc:member" ;
09.      private  Connection conn= null ;
10.       
11.      public  DataBaseConnection() {
12.          try {
13.              Class.forName(DBDRIVER);
14.              this .conn=DriverManager.getConnection(DBURL);
15.          }
16.          catch (Exception e){}
17.      }
18.       
19.      public  Connection getConnection() {
20.          return  this .conn;
21.      }
22.       
23.      public  void  close() {
24.          try {
25.              this .conn.close();
26.          }
27.          catch (Exception e){}
28.      }
29. }



MemberDAOImpl.java:数据库操作类

 
001. package  mgc.dao.test.impl;
002.  
003. import  java.util.*;
004. import  java.sql.*;
005. import  mgc.dao.vo.*;
006. import  mgc.dao.test.*;
007. import  mgc.dao.test.dbc.*;
008.  
009. public  class  MemberDAOImpl  implements  MemberDAO {
010.       
011.      //插入数据
012.      public  void  insert(Member member)  throws  Exception {
013.          String sql= "Insert INTO member (username,password) VALUES (?,?)" ;
014.          PreparedStatement pstmt= null ;
015.          DataBaseConnection dbc= null ;
016.          try {
017.              //连接数据库
018.              dbc= new  DataBaseConnection();
019.              pstmt=dbc.getConnection().prepareStatement(sql);
020.              pstmt.setString( 1 , member.getUsername());
021.              pstmt.setString( 2 , member.getPassword());
022.              //操作数据库
023.              pstmt.executeUpdate();
024.              pstmt.close();
025.          }
026.          catch (Exception e) {
027.              throw  new  Exception(e);
028.          }
029.          finally {
030.              //关闭数据库
031.              dbc.close();
032.          }
033.      }
034.       
035.      //修改操作
036.      public  void  update(Member member)  throws  Exception {
037.          String sql= "Update  member SET username=?,password=? Where ID=?" ;
038.          PreparedStatement pstmt= null ;
039.          DataBaseConnection dbc= null ;
040.          try {
041.              //连接数据库
042.              dbc= new  DataBaseConnection();
043.              pstmt=dbc.getConnection().prepareStatement(sql);
044.              pstmt.setString( 1 , member.getUsername());
045.              pstmt.setString( 2 , member.getPassword());
046.              pstmt.setInt( 3 ,member.getId());
047.              //操作数据库
048.              pstmt.executeUpdate();
049.              pstmt.close();
050.          }
051.          catch (Exception e) {
052.              throw  new  Exception(e);
053.          }
054.          finally {
055.              //关闭数据库
056.              dbc.close();
057.          }
058.      }
059.       
060.      public  void  delete( int  id)  throws  Exception {
061.          String sql= "Delete FROM member Where ID=?" ;
062.          PreparedStatement pstmt= null ;
063.          DataBaseConnection dbc= null ;
064.           
065.          try {
066.              //连接数据库
067.              dbc= new  DataBaseConnection();
068.              pstmt=dbc.getConnection().prepareStatement(sql);
069.              pstmt.setInt( 1 , id);
070.              //操作数据库
071.              pstmt.executeUpdate();
072.              pstmt.close();
073.          }
074.          catch (Exception e) {
075.              throw  new  Exception(e);
076.          }
077.          finally {
078.              //关闭数据库
079.              dbc.close();
080.          }
081.      }
082.       
083.      public  Member queryById( int  id)  throws  Exception {
084.          String sql= "Select * FROM member Where ID=?" ;
085.          PreparedStatement pstmt= null ;
086.          DataBaseConnection dbc= null ;
087.          ResultSet rs= null ;
088.          Member member= null ;
089.          try {
090.              //连接数据库
091.              dbc= new  DataBaseConnection();
092.              pstmt=dbc.getConnection().prepareStatement(sql);
093.              pstmt.setInt( 1 , id);
094.              //操作数据库
095.              rs=pstmt.executeQuery();
096.              if (rs.next()) {
097.                  //将查询结果保存在Member对象中
098.                  member= new  Member();
099.                  member.setUsername(rs.getString( "username" ));
100.                  member.setPassword(rs.getString( "password" ));
101.              }
102.              rs.close();
103.              pstmt.close();
104.          }
105.          catch (Exception e) {
106.              throw  new  Exception(e);
107.          }
108.          finally {
109.              //关闭数据库
110.              dbc.close();
111.          }
112.          return  member;
113.      }
114.       
115.      public  List queryAll()  throws  Exception {
116.          String sql= "Select * FROM member" ;
117.          PreparedStatement pstmt= null ;
118.          DataBaseConnection dbc= null ;
119.          ResultSet rs= null ;
120.          List all= new  ArrayList();
121.          try {
122.              //连接数据库
123.              dbc= new  DataBaseConnection();
124.              pstmt=dbc.getConnection().prepareStatement(sql);
125.              //操作数据库
126.              rs=pstmt.executeQuery();
127.              while (rs.next()) {
128.                  //将查询结果保存在Member对象中
129.                  Member member= new  Member();
130.                  member.setUsername(rs.getString( "username" ));
131.                  member.setPassword(rs.getString( "password" ));
132.                  all.add(member);
133.              }
134.              rs.close();
135.              pstmt.close();
136.          }
137.          catch (Exception e) {
138.              throw  new  Exception(e);
139.          }
140.          finally {
141.              //关闭数据库
142.              dbc.close();
143.          }
144.          return  all;
145.      }
146. }



dao.jsp:前台显示页面

view source
<embed id="highlighter_126215_clipboard" title="copy to clipboard" type="application/x-shockwave-flash" width="16" height="16" src="http://mgc.ahau.edu.cn/common/SyntaxHighlighter/scripts/clipboard.swf" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_126215" menu="false"></embed>
print ?
01. <%@page contentType="text/html;charset=GB2312" %>
02. <%@page import="java.util.*" %>
03. <%@page import="mgc.dao.factory.*" %>
04. <%@page import="mgc.dao.test.impl.*" %>
05. <%@page import="mgc.dao.vo.*" %>
06. < html >
07.    < head >
08.      < title >dao.jsp</ title ></c
分享到:
评论
gstarwd
  • 浏览: 897934 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

你可能感兴趣的:(DAO,设计模式,sql,jdbc,Flash)