﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-小方的Java博客-文章分类-java 设计模式</title><link>http://www.blogjava.net/jorwen/category/7241.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 28 Feb 2007 11:51:45 GMT</lastBuildDate><pubDate>Wed, 28 Feb 2007 11:51:45 GMT</pubDate><ttl>60</ttl><item><title>JAVA操作数据库方式与设计模式应用</title><link>http://www.blogjava.net/jorwen/articles/29429.html</link><dc:creator>方佳玮</dc:creator><author>方佳玮</author><pubDate>Wed, 01 Feb 2006 04:44:00 GMT</pubDate><guid>http://www.blogjava.net/jorwen/articles/29429.html</guid><wfw:comment>http://www.blogjava.net/jorwen/comments/29429.html</wfw:comment><comments>http://www.blogjava.net/jorwen/articles/29429.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jorwen/comments/commentRss/29429.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jorwen/services/trackbacks/29429.html</trackback:ping><description><![CDATA[<DIV style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt"><FONT color=#000000>1.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp; </SPAN>在业务层使用JDBC直接操作数据库－最简单，最直接的操作</FONT></DIV>
<DIV style="TEXT-INDENT: 108pt"><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV><FONT color=#000000>1）数据库url,username,password写死在代码中</FONT></DIV>
<DIV><FONT color=#000000>&nbsp;&nbsp;&nbsp; Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();&nbsp;</FONT></DIV>
<DIV><SPAN><FONT color=#000000>&nbsp;&nbsp;&nbsp; String url="jdbc:oracle:thin:@localhost:1521:orcl";&nbsp;</FONT></SPAN></DIV>
<DIV><SPAN><FONT color=#000000>&nbsp;&nbsp;&nbsp; String user="scott";&nbsp;</FONT></SPAN></DIV>
<DIV><SPAN><FONT color=#000000>&nbsp;&nbsp;&nbsp; String password="tiger";&nbsp;</FONT></SPAN></DIV>
<DIV><SPAN><FONT color=#000000>&nbsp;&nbsp;&nbsp; Connection conn= DriverManager.getConnection(url,user,password);&nbsp; </FONT></SPAN></DIV>
<DIV><FONT color=#000000><SPAN>&nbsp;&nbsp;&nbsp; </SPAN>Statement stmt=conn.createStatement(</FONT></DIV>
<DIV style="TEXT-INDENT: 48pt"><FONT color=#000000>ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);&nbsp;</FONT></DIV>
<DIV><SPAN><FONT color=#000000>&nbsp;&nbsp;&nbsp; String sql="select * from test";&nbsp;</FONT></SPAN></DIV>
<DIV><FONT color=#000000><SPAN>&nbsp;&nbsp;&nbsp; </SPAN>ResultSet rs=stmt.executeQuery(sql); </FONT></DIV>
<DIV><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV><FONT color=#000000>2）采用Facade和Command模式，使用DBUtil类封装JDBC操作；</FONT></DIV>
<DIV><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 数据库url,username,password可以放在配置文件中（如xml,properties,ini等）。</FONT></DIV>
<DIV><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这种方法在小程序中应用较多。</FONT></DIV>
<DIV><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV><FONT color=#000000>2.DAO(Data Accessor Object)模式－松耦合的开始</FONT></DIV>
<DIV style="TEXT-INDENT: 30pt"><FONT color=#000000>DAO = data + accessor + domain object</FONT></DIV>
<DIV style="TEXT-INDENT: 30pt"><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV><FONT color=#000000>&nbsp;例如User类－domain object (javabean)</FONT></DIV>
<DIV style="TEXT-INDENT: 30pt"><FONT color=#000000>UserDAO类－accessor ，提供的方法getUser(int id)，save(User user)内包含了JDBC操作</FONT></DIV>
<DIV style="TEXT-INDENT: 30pt"><FONT color=#000000>在业务逻辑中使用这两个类来完成数据操作。</FONT></DIV>
<DIV style="TEXT-INDENT: 30pt"><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV><FONT color=#000000>使用Factory模式可以方便不同数据库连接之间的移植。</FONT></DIV>
<DIV><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV><FONT color=#000000>3.数据库资源管理模式</FONT></DIV>
<DIV style="TEXT-INDENT: 12.05pt"><FONT color=#000000>3.1 数据库连接池技术</FONT></DIV>
<DIV style="TEXT-INDENT: 36pt"><FONT color=#000000>资源重用，避免频繁创建，释放连接引起大大量性能开销；</FONT></DIV>
<DIV style="TEXT-INDENT: 36pt"><FONT color=#000000>更快的系统响应速度；</FONT></DIV>
<DIV style="TEXT-INDENT: 36pt"><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV style="TEXT-INDENT: 24pt"><FONT color=#000000>通过实现JDBC的部分资源对象接口( Connection, Statement, ResultSet )，可以使用Decorator设计模式分别产生三种逻辑资源对象: PooledConnection, PooledStatement和 PooledResultSet。</FONT></DIV>
<DIV><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV><SPAN style="BACKGROUND: #d9d9d9"><SPAN style="BACKGROUND-COLOR: #ffffff">一个最简单地数据库连接池实现</SPAN><FONT color=#eeeeee><SPAN style="BACKGROUND-COLOR: #eeeeee"></SPAN></FONT></SPAN><FONT color=#000000>：</FONT></DIV>
<DIV align=left><FONT color=#000000><SPAN style="BACKGROUND: white; COLOR: #7f0055">public</SPAN><SPAN style="BACKGROUND: white"> <SPAN style="COLOR: #7f0055">class</SPAN> <SPAN style="COLOR: black">ConnectionPool</SPAN> <SPAN style="COLOR: black">{</SPAN></SPAN></FONT></DIV>
<DIV align=left><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">private</SPAN> <SPAN style="COLOR: #7f0055">static</SPAN> <SPAN style="COLOR: black">Vector</SPAN> <SPAN style="COLOR: black">pools;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">private</SPAN> <SPAN style="COLOR: #7f0055">final</SPAN> <SPAN style="COLOR: #7f0055">int</SPAN> <SPAN style="COLOR: black">POOL_MAXSIZE</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: black">25;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #3f5fbf">/**</SPAN></FONT></SPAN></DIV>
<DIV align=left><FONT color=#000000><SPAN style="BACKGROUND: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<SPAN style="COLOR: #3f5fbf">*</SPAN> </SPAN><SPAN style="BACKGROUND: white; COLOR: #3f5fbf">获取数据库连接</SPAN></FONT></DIV>
<DIV align=left><FONT color=#000000><SPAN style="BACKGROUND: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<SPAN style="COLOR: #3f5fbf">*</SPAN> </SPAN><SPAN style="BACKGROUND: white; COLOR: #3f5fbf">如果当前池中有可用连接，则将池中最后一个返回；若没有，则创建一个新的返回</SPAN></FONT></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<SPAN style="COLOR: #3f5fbf">*/</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">public</SPAN> <SPAN style="COLOR: #7f0055">synchronized</SPAN> <SPAN style="COLOR: black">Connection</SPAN> <SPAN style="COLOR: black">getConnection()</SPAN> <SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">Connection</SPAN> <SPAN style="COLOR: black">conn</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: #7f0055">null</SPAN><SPAN style="COLOR: black">;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">if</SPAN> <SPAN style="COLOR: black">(pools</SPAN> <SPAN style="COLOR: black">==</SPAN> <SPAN style="COLOR: #7f0055">null</SPAN><SPAN style="COLOR: black">)</SPAN> <SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">pools</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: #7f0055">new</SPAN> <SPAN style="COLOR: black">Vector();</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN></FONT></SPAN></DIV>
<DIV align=left><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">if</SPAN> <SPAN style="COLOR: black">(pools.isEmpty())</SPAN> <SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">conn</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: black">createConnection();</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN> <SPAN style="COLOR: #7f0055">else</SPAN> <SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">int</SPAN> <SPAN style="COLOR: black">last_idx</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: black">pools.size()</SPAN> <SPAN style="COLOR: black">-</SPAN> <SPAN style="COLOR: black">1;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">conn</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: black">(Connection)</SPAN> <SPAN style="COLOR: black">pools.get(last_idx);</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">pools.remove(last_idx);</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN></FONT></SPAN></DIV>
<DIV align=left><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">return</SPAN> <SPAN style="COLOR: black">conn;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN></FONT></SPAN></DIV>
<DIV align=left><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #3f5fbf">/**</SPAN></FONT></SPAN></DIV>
<DIV align=left><FONT color=#000000><SPAN style="BACKGROUND: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<SPAN style="COLOR: #3f5fbf">*</SPAN> </SPAN><SPAN style="BACKGROUND: white; COLOR: #3f5fbf">将使用完毕的数据库连接放回池中</SPAN></FONT></DIV>
<DIV align=left><FONT color=#000000><SPAN style="BACKGROUND: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<SPAN style="COLOR: #3f5fbf">*</SPAN> </SPAN><SPAN style="BACKGROUND: white; COLOR: #3f5fbf">若池中连接已经超过阈值，则关闭该连接；否则放回池中下次再使用</SPAN></FONT></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<SPAN style="COLOR: #3f5fbf">*/</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">public</SPAN> <SPAN style="COLOR: #7f0055">synchronized</SPAN> <SPAN style="COLOR: #7f0055">void</SPAN> <SPAN style="COLOR: black">releaseConnection(Connection</SPAN> <SPAN style="COLOR: black">conn)</SPAN> <SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">if</SPAN> <SPAN style="COLOR: black">(pools.size()</SPAN> <SPAN style="COLOR: black">&gt;=</SPAN> <SPAN style="COLOR: black">POOL_MAXSIZE)</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">try</SPAN> <SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">conn.close();</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN> <SPAN style="COLOR: #7f0055">catch</SPAN> <SPAN style="COLOR: black">(SQLException</SPAN> <SPAN style="COLOR: black">e)</SPAN> <SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><FONT color=#000000><SPAN style="BACKGROUND: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #3f7f5f">// </SPAN><SPAN style="COLOR: #7f9fbf">TODO</SPAN></SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f">自动生成</SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f"> catch </SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f">块</SPAN></FONT></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">e.printStackTrace();</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN> <SPAN style="COLOR: #7f0055">else</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">pools.add(conn);</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN></FONT></SPAN></DIV>
<DIV align=left><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">public</SPAN> <SPAN style="COLOR: #7f0055">static</SPAN> <SPAN style="COLOR: black">Connection</SPAN> <SPAN style="COLOR: black">createConnection()</SPAN> <SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">Connection</SPAN> <SPAN style="COLOR: black">conn</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: #7f0055">null</SPAN><SPAN style="COLOR: black">;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">try</SPAN> <SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">Class.forName(</SPAN><SPAN style="COLOR: #2a00ff">"oracle.jdbc.driver.OracleDriver"</SPAN><SPAN style="COLOR: black">).newInstance();</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">String</SPAN> <SPAN style="COLOR: black">url</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: #2a00ff">"jdbc:oracle:thin:@localhost:1521:orcl"</SPAN><SPAN style="COLOR: black">;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">String</SPAN> <SPAN style="COLOR: black">user</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: #2a00ff">"scott"</SPAN><SPAN style="COLOR: black">;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">String</SPAN> <SPAN style="COLOR: black">password</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: #2a00ff">"tiger"</SPAN><SPAN style="COLOR: black">;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">conn</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: black">DriverManager.getConnection(url,</SPAN> <SPAN style="COLOR: black">user,</SPAN> <SPAN style="COLOR: black">password);</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN> <SPAN style="COLOR: #7f0055">catch</SPAN> <SPAN style="COLOR: black">(InstantiationException</SPAN> <SPAN style="COLOR: black">e)</SPAN> <SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><FONT color=#000000><SPAN style="BACKGROUND: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #3f7f5f">// </SPAN><SPAN style="COLOR: #7f9fbf">TODO</SPAN></SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f">自动生成</SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f"> catch </SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f">块</SPAN></FONT></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">e.printStackTrace();</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN> <SPAN style="COLOR: #7f0055">catch</SPAN> <SPAN style="COLOR: black">(IllegalAccessException</SPAN> <SPAN style="COLOR: black">e)</SPAN> <SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><FONT color=#000000><SPAN style="BACKGROUND: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #3f7f5f">// </SPAN><SPAN style="COLOR: #7f9fbf">TODO</SPAN></SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f">自动生成</SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f"> catch </SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f">块</SPAN></FONT></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">e.printStackTrace();</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN> <SPAN style="COLOR: #7f0055">catch</SPAN> <SPAN style="COLOR: black">(ClassNotFoundException</SPAN> <SPAN style="COLOR: black">e)</SPAN> <SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><FONT color=#000000><SPAN style="BACKGROUND: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #3f7f5f">// </SPAN><SPAN style="COLOR: #7f9fbf">TODO</SPAN></SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f">自动生成</SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f"> catch </SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f">块</SPAN></FONT></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">e.printStackTrace();</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN> <SPAN style="COLOR: #7f0055">catch</SPAN> <SPAN style="COLOR: black">(SQLException</SPAN> <SPAN style="COLOR: black">e)</SPAN> <SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><FONT color=#000000><SPAN style="BACKGROUND: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #3f7f5f">// </SPAN><SPAN style="COLOR: #7f9fbf">TODO</SPAN></SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f">自动生成</SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f"> catch </SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f">块</SPAN></FONT></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">e.printStackTrace();</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">return</SPAN> <SPAN style="COLOR: black">conn;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN></FONT></SPAN></DIV>
<DIV><SPAN style="FONT-SIZE: 10.5pt; BACKGROUND: white; COLOR: black"><FONT color=#000000>}</FONT></SPAN></DIV>
<DIV><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV><FONT color=#000000><SPAN style="BACKGROUND: white; COLOR: black">注意</SPAN><SPAN style="BACKGROUND: white; COLOR: black">：利用getConnection</SPAN><SPAN style="COLOR: black">()方法得到的Connection，程序员很习惯地调用conn.close()方法关闭了数据库连接，那么上述的数据库连接机制便形同虚设。<SPAN style="BACKGROUND-COLOR: #ffffff">在调用conn.close()方法方法时如何调用releaseConnection()方法？这是关键。这里，我们使用Proxy模式和java反射机制。</SPAN> </SPAN></FONT></DIV>
<DIV><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV align=left><FONT color=#000000><SPAN style="BACKGROUND: white; COLOR: #7f0055">public</SPAN><SPAN style="BACKGROUND: white"> <SPAN style="COLOR: #7f0055">synchronized</SPAN> <SPAN style="COLOR: black">Connection</SPAN> <SPAN style="COLOR: black">getConnection()</SPAN> <SPAN style="COLOR: black">{</SPAN></SPAN></FONT></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">Connection</SPAN> <SPAN style="COLOR: black">conn</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: #7f0055">null</SPAN><SPAN style="COLOR: black">;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">if</SPAN> <SPAN style="COLOR: black">(pools</SPAN> <SPAN style="COLOR: black">==</SPAN> <SPAN style="COLOR: #7f0055">null</SPAN><SPAN style="COLOR: black">)</SPAN> <SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">pools</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: #7f0055">new</SPAN> <SPAN style="COLOR: black">Vector();</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN></FONT></SPAN></DIV>
<DIV align=left><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">if</SPAN> <SPAN style="COLOR: black">(pools.isEmpty())</SPAN> <SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">conn</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: black">createConnection();</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN> <SPAN style="COLOR: #7f0055">else</SPAN> <SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">int</SPAN> <SPAN style="COLOR: black">last_idx</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: black">pools.size()</SPAN> <SPAN style="COLOR: black">-</SPAN> <SPAN style="COLOR: black">1;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">conn</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: black">(Connection)</SPAN> <SPAN style="COLOR: black">pools.get(last_idx);</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">pools.remove(last_idx);</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></DIV>
<DIV align=left><SPAN><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ConnectionHandler handler=new ConnectionHandler(this);</FONT></SPAN></DIV>
<DIV align=left><FONT color=#000000><SPAN style="BACKGROUND: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">return</SPAN> </SPAN>handler.bind(con)<SPAN style="BACKGROUND: white; COLOR: black">;</SPAN></FONT></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN></FONT></SPAN></DIV>
<DIV align=left><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV align=left><FONT color=#000000><SPAN style="BACKGROUND: white; COLOR: #7f0055">public</SPAN><SPAN style="BACKGROUND: white"> <SPAN style="COLOR: #7f0055">class</SPAN> <SPAN style="COLOR: black">ConnectionHandler</SPAN> <SPAN style="COLOR: #7f0055">implements</SPAN> <SPAN style="COLOR: black">InvocationHandler</SPAN> <SPAN style="COLOR: black">{</SPAN></SPAN></FONT></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">private</SPAN> <SPAN style="COLOR: black">Connection</SPAN> <SPAN style="COLOR: black">conn;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">private</SPAN> <SPAN style="COLOR: black">ConnectionPool</SPAN> <SPAN style="COLOR: black">pool;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp; &nbsp;<SPAN style="COLOR: #7f0055">public</SPAN> <SPAN style="COLOR: black">ConnectionHandler(ConnectionPool</SPAN> <SPAN style="COLOR: black">pool){</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">this</SPAN><SPAN style="COLOR: black">.pool=pool;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #3f5fbf">/**</SPAN></FONT></SPAN></DIV>
<DIV align=left><FONT color=#000000><SPAN style="BACKGROUND: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #3f5fbf">*</SPAN> </SPAN><SPAN style="BACKGROUND: white; COLOR: #3f5fbf">将动态代理绑定到指定</SPAN><SPAN style="BACKGROUND: white; COLOR: #3f5fbf">Connection</SPAN></FONT></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #3f5fbf">*</SPAN> <SPAN style="COLOR: #7f9fbf">@param</SPAN> <SPAN style="COLOR: #3f5fbf">conn</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #3f5fbf">*</SPAN> <SPAN style="COLOR: #7f9fbf">@return</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #3f5fbf">*/</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">public</SPAN> <SPAN style="COLOR: black">Connection</SPAN> <SPAN style="COLOR: black">bind(Connection</SPAN> <SPAN style="COLOR: black">conn){</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">this</SPAN><SPAN style="COLOR: black">.conn=conn;</SPAN></FONT></SPAN></DIV>
<DIV style="TEXT-INDENT: 42pt" align=left><FONT color=#000000><SPAN style="BACKGROUND: white; COLOR: black">Connection</SPAN><SPAN style="BACKGROUND: white"> <SPAN style="COLOR: black">proxyConn=(Connection)Proxy.newProxyInstance(</SPAN></SPAN></FONT></DIV>
<DIV style="TEXT-INDENT: 73.5pt" align=left><FONT color=#000000><SPAN style="BACKGROUND: white; COLOR: black">conn.getClass().getClassLoader(),</SPAN><SPAN style="BACKGROUND: white"> <SPAN style="COLOR: black">conn.getClass().getInterfaces(),</SPAN><SPAN style="COLOR: #7f0055">this</SPAN><SPAN style="COLOR: black">);</SPAN></SPAN></FONT></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">return</SPAN> <SPAN style="COLOR: black">proxyConn;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></DIV>
<DIV align=left><FONT color=#000000><SPAN style="BACKGROUND: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #3f7f5f">/* </SPAN></SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f">（非</SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f"> Javadoc</SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f">）</SPAN></FONT></DIV>
<DIV align=left><SPAN style="BACKGROUND: white; COLOR: #3f7f5f"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;* @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])</FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white; COLOR: #3f7f5f"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;*/</FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">public</SPAN> <SPAN style="COLOR: black">Object</SPAN> <SPAN style="COLOR: black">invoke(Object</SPAN> <SPAN style="COLOR: black">proxy,</SPAN> <SPAN style="COLOR: black">Method</SPAN> <SPAN style="COLOR: black">method,</SPAN> <SPAN style="COLOR: black">Object[]</SPAN> <SPAN style="COLOR: black">args)</SPAN> <SPAN style="COLOR: #7f0055">throws</SPAN> <SPAN style="COLOR: black">Throwable</SPAN> <SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><FONT color=#000000><SPAN style="BACKGROUND: white">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #3f7f5f">// </SPAN><SPAN style="COLOR: #7f9fbf">TODO</SPAN></SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f">自动生成方法存根</SPAN></FONT></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">Object</SPAN> <SPAN style="COLOR: black">obj=</SPAN><SPAN style="COLOR: #7f0055">null</SPAN><SPAN style="COLOR: black">;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">if</SPAN><SPAN style="COLOR: black">(</SPAN><SPAN style="COLOR: #2a00ff">"close"</SPAN><SPAN style="COLOR: black">.equals(method.getName())){</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">this</SPAN><SPAN style="COLOR: black">.pool.releaseConnection(</SPAN><SPAN style="COLOR: #7f0055">this</SPAN><SPAN style="COLOR: black">.conn);</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">else</SPAN><SPAN style="COLOR: black">{</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">obj=method.invoke(</SPAN><SPAN style="COLOR: #7f0055">this</SPAN><SPAN style="COLOR: black">.conn,</SPAN> <SPAN style="COLOR: black">args);</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #7f0055">return</SPAN> <SPAN style="COLOR: black">obj;</SPAN></FONT></SPAN></DIV>
<DIV align=left><SPAN style="BACKGROUND: white"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: black">}</SPAN></FONT></SPAN></DIV>
<DIV><SPAN style="FONT-SIZE: 10.5pt; BACKGROUND: white; COLOR: black"><FONT color=#000000>}</FONT></SPAN></DIV>
<DIV><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在实际项目中，并不需要你来从头开始来设计数据库连接池机制，现在成熟的开源项目，如C3P0,dbcp,Proxool等提供了良好的实现。一般推荐使用Apache dbcp，基本使用实例：</FONT></DIV>
<DIV><FONT color=#000000></FONT></DIV>
<DIV><FONT color=#000000></FONT></DIV>
<DIV><FONT color=#000000>DataSource ds = null;<BR>&nbsp;&nbsp; try{<BR>&nbsp;&nbsp;&nbsp;&nbsp; Context initCtx = new InitialContext();<BR>&nbsp;&nbsp;&nbsp;&nbsp; Context envCtx = (Context) initCtx.lookup("java:comp/env");<BR>&nbsp;&nbsp;&nbsp;&nbsp; ds = (DataSource)envCtx.lookup("jdbc/myoracle");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(ds!=null){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.println("Connection is OK!");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Connection cn=ds.getConnection();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(cn!=null){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.println("cn is Ok!");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Statement stmt = cn.createStatement();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ResultSet rst = stmt.executeQuery("select * from BOOK");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.println("&lt;p&gt;rst is Ok!" + rst.next());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(rst.next()){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;out.println("&lt;P&gt;BOOK_CODE:" + rst.getString(1));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cn.close();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}else{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.println("rst Fail!");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.println("Fail!");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }catch(Exception ne){ out.println(ne);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></DIV>
<DIV><FONT color=#000000><BR></FONT></DIV>
<DIV><FONT color=#000000></FONT></DIV>
<DIV><FONT color=#000000>3.2 Statement Pool</FONT></DIV>
<DIV><FONT color=#000000>普通预编译代码：</FONT></DIV>
<DIV style="TEXT-INDENT: 21pt"><FONT color=#000000>String&nbsp;strSQL=”select name from items where id=?”;</FONT></DIV>
<DIV style="TEXT-INDENT: 21pt"><FONT color=#000000>PreparedStatement&nbsp;ps=conn.prepareStatement(strSQL);</FONT></DIV>
<DIV style="TEXT-INDENT: 21pt"><FONT color=#000000>ps.setString(1, “2”);</FONT></DIV>
<DIV style="TEXT-INDENT: 21pt"><FONT color=#000000>ResultSet&nbsp;rs=ps.executeQuery();</FONT></DIV>
<DIV style="TEXT-INDENT: 21pt"><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV style="TEXT-INDENT: 21pt"><FONT color=#000000>但是PreparedStatement&nbsp;是与特定的Connection关联的，一旦Connection关闭，则相关的PreparedStatement&nbsp;也会关闭。</FONT></DIV>
<DIV style="TEXT-INDENT: 21pt"><FONT color=#000000>为了创建PreparedStatement 缓冲池，可以在invoke方法中通过sql语句判断池中还有没有可用实例。</FONT></DIV>
<DIV><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV><FONT color=#000000>4. 持久层设计与O/R mapping 技术</FONT></DIV>
<DIV style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt"><FONT color=#000000>1）&nbsp;<SPAN style="BACKGROUND: #d9d9d9"><SPAN style="BACKGROUND-COLOR: #ffffff">Hernate：适合对新产品的开发，进行封闭化<SPAN style="BACKGROUND-COLOR: #ffffff">的设计 </SPAN></SPAN></SPAN></FONT></DIV>
<DIV style="TEXT-INDENT: 21pt"><FONT color=#000000>Hibernate&nbsp;2003年被Jboss接管，<SPAN style="FONT-SIZE: 10pt">通过把</SPAN><SPAN style="FONT-SIZE: 10pt">java pojo</SPAN><SPAN style="FONT-SIZE: 10pt">对象</SPAN><SPAN style="FONT-SIZE: 10pt">映射到数据库的</SPAN><SPAN style="FONT-SIZE: 10pt">table</SPAN><SPAN style="FONT-SIZE: 10pt">中，采用了</SPAN><SPAN style="FONT-SIZE: 10pt">xml</SPAN><SPAN style="FONT-SIZE: 10pt">/</SPAN><SPAN style="FONT-SIZE: 10pt">javareflection</SPAN><SPAN style="FONT-SIZE: 10pt">技术等。</SPAN><SPAN style="FONT-SIZE: 10pt">3.0</SPAN><SPAN style="FONT-SIZE: 10pt">提供了对存储过程和手写</SPAN><SPAN style="FONT-SIZE: 10pt">sql</SPAN><SPAN style="FONT-SIZE: 10pt">的支持，本身提供了</SPAN><SPAN style="FONT-SIZE: 10pt">hql</SPAN><SPAN style="FONT-SIZE: 10pt">语言。</SPAN></FONT></DIV>
<DIV style="TEXT-INDENT: 20pt"><SPAN style="FONT-SIZE: 10pt"><FONT color=#000000>开发所需要的文件：</FONT></SPAN></DIV>
<DIV><FONT color=#000000><SPAN style="FONT-SIZE: 10pt">hibernate</SPAN><SPAN style="FONT-SIZE: 10pt">配置文件：</SPAN><SPAN style="FONT-SIZE: 10pt"> hibernate.cfg.xml </SPAN><SPAN style="FONT-SIZE: 10pt">或</SPAN><SPAN style="FONT-SIZE: 10pt"> hibernate.properties</SPAN></FONT></DIV>
<DIV><FONT color=#000000><SPAN style="FONT-SIZE: 10pt">hibernate </SPAN><SPAN style="FONT-SIZE: 10pt">映射文件：</SPAN><SPAN style="FONT-SIZE: 10pt"> a.hbm.xml</SPAN></FONT></DIV>
<DIV><FONT color=#000000><SPAN style="FONT-SIZE: 10pt">pojo</SPAN><SPAN style="FONT-SIZE: 10pt">类源文件：</SPAN><SPAN style="FONT-SIZE: 10pt"> a.java</SPAN><SPAN style="FONT-SIZE: 10pt">　　</SPAN></FONT></DIV>
<DIV style="TEXT-INDENT: 21pt"><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV><SPAN style="FONT-SIZE: 10pt"><FONT color=#000000>导出表与表之间的关系：</FONT></SPAN></DIV>
<DIV><FONT color=#000000><SPAN style="FONT-SIZE: 10pt">a. </SPAN><SPAN style="FONT-SIZE: 10pt">从</SPAN><SPAN style="FONT-SIZE: 10pt">java</SPAN><SPAN style="FONT-SIZE: 10pt">对象到</SPAN><SPAN style="FONT-SIZE: 10pt">hbm</SPAN><SPAN style="FONT-SIZE: 10pt">文件：</SPAN><SPAN style="FONT-SIZE: 10pt">xdoclet </SPAN></FONT></DIV>
<DIV><FONT color=#000000><SPAN style="FONT-SIZE: 10pt">b. </SPAN><SPAN style="FONT-SIZE: 10pt">从</SPAN><SPAN style="FONT-SIZE: 10pt">hbm</SPAN><SPAN style="FONT-SIZE: 10pt">文件到</SPAN><SPAN style="FONT-SIZE: 10pt">java</SPAN><SPAN style="FONT-SIZE: 10pt">对象：</SPAN><SPAN style="FONT-SIZE: 10pt">hibernate extension </SPAN></FONT></DIV>
<DIV><FONT color=#000000><SPAN style="FONT-SIZE: 10pt">c. </SPAN><SPAN style="FONT-SIZE: 10pt">从数据库到</SPAN><SPAN style="FONT-SIZE: 10pt">hbm</SPAN><SPAN style="FONT-SIZE: 10pt">文件：</SPAN><SPAN style="FONT-SIZE: 10pt">middlegen </SPAN></FONT></DIV>
<DIV><FONT color=#000000><SPAN style="FONT-SIZE: 10pt">d. </SPAN><SPAN style="FONT-SIZE: 10pt">从</SPAN><SPAN style="FONT-SIZE: 10pt">hbm</SPAN><SPAN style="FONT-SIZE: 10pt">文件到数据库：</SPAN><SPAN style="FONT-SIZE: 10pt">SchemaExport</SPAN></FONT></DIV>
<DIV><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt"><FONT color=#000000>2）<SPAN style="BACKGROUND-COLOR: #ffffff">Iatis ：适合对遗留系统的改造和对既有数据库的复用，有很强的灵活性 3） Apache OJB：优势在于对标准的全面支持 4）EJB：适合集群服务器，其性能也不象某些人所诟病的那么差劲 5） JDO (java data object)</SPAN> </FONT></DIV>
<DIV style="TEXT-INDENT: 21pt"><SPAN style="COLOR: #333333"><FONT color=#000000>设置一个Properties对象，从而获取一个JDO的PersistenceManagerFactory（相当于JDBC连接池中的DataSource），进而获得一个PersistenceManager对象（相当于JDBC中的Connection对象），之后，你可以用这个PersistenceManager对象来增加、更新、删除、查询对象。</FONT></SPAN></DIV>
<DIV style="TEXT-INDENT: 21pt"><SPAN style="COLOR: #333333"><FONT color=#000000>JDOQL是JDO的查询语言；它有点象SQL，但却是依照Java的语法的。</FONT></SPAN></DIV>
<DIV><FONT color=#000000></FONT>&nbsp;</DIV>
<DIV><FONT color=#000000>5. 基于开源框架的Struts+Spring+Hibernate实现方案</FONT></DIV>
<DIV><FONT color=#000000></FONT></DIV>
<DIV>
<DIV style="TEXT-INDENT: 20pt" align=left><FONT color=#000000><SPAN style="FONT-SIZE: 10pt">示例：这是一个</SPAN><SPAN style="FONT-SIZE: 10pt">3</SPAN><SPAN style="FONT-SIZE: 10pt">层架构的</SPAN><SPAN style="FONT-SIZE: 10pt">web </SPAN><SPAN style="FONT-SIZE: 10pt">程序，通过一个</SPAN><SPAN style="FONT-SIZE: 10pt">Action </SPAN><SPAN style="FONT-SIZE: 10pt">来调用业务代理，再通过它来回调</SPAN><SPAN style="FONT-SIZE: 10pt"> DAO</SPAN><SPAN style="FONT-SIZE: 10pt">类。下面的流程图表示了</SPAN><SPAN style="FONT-SIZE: 10pt">MyUsers</SPAN><SPAN style="FONT-SIZE: 10pt">是如何工作的。数字表明了流程的先后顺序，从</SPAN><SPAN style="FONT-SIZE: 10pt">web</SPAN><SPAN style="FONT-SIZE: 10pt">层</SPAN><SPAN style="FONT-SIZE: 10pt">(UserAction)</SPAN><SPAN style="FONT-SIZE: 10pt">到中间层</SPAN><SPAN style="FONT-SIZE: 10pt">(UserManager)</SPAN><SPAN style="FONT-SIZE: 10pt">，再到数据层</SPAN><SPAN style="FONT-SIZE: 10pt">(UserDAO)</SPAN><SPAN style="FONT-SIZE: 10pt">，然后返回。</SPAN></FONT></DIV>
<DIV style="TEXT-INDENT: 20pt" align=left><FONT color=#000000><SPAN style="FONT-SIZE: 10pt">Spring</SPAN><SPAN style="FONT-SIZE: 10pt">是</SPAN><SPAN style="FONT-SIZE: 10pt">AOP, UserManager</SPAN><SPAN style="FONT-SIZE: 10pt">和</SPAN><SPAN style="FONT-SIZE: 10pt">UserDAO</SPAN><SPAN style="FONT-SIZE: 10pt">都是接口</SPAN><SPAN style="FONT-SIZE: 10pt">.</SPAN></FONT></DIV>
<DIV style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; TEXT-ALIGN: left" align=left><FONT color=#000000><SPAN style="FONT-SIZE: 10pt">1)<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-SIZE: 10pt">web</SPAN><SPAN style="FONT-SIZE: 10pt">层</SPAN><SPAN style="FONT-SIZE: 10pt">(UserAction) </SPAN><SPAN style="FONT-SIZE: 10pt">：调用中间层的接口方法，将</SPAN><SPAN style="FONT-SIZE: 10pt">UserManager</SPAN><SPAN style="FONT-SIZE: 10pt">作为属性注入。</SPAN></FONT></DIV>
<DIV style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; TEXT-ALIGN: left" align=left><SPAN style="FONT-SIZE: 10pt"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 采用流行的Struts框架，虽然有很多人不屑一顾，但是这项技术在业界用的比较普遍，能满足基本的功能，可以减少培训学习成本。</FONT></SPAN></DIV>
<DIV style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; TEXT-ALIGN: left" align=left><SPAN style="FONT-SIZE: 10pt"><FONT color=#000000></FONT></SPAN></DIV>
<DIV style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; TEXT-ALIGN: left" align=left><FONT color=#000000><SPAN style="FONT-SIZE: 10pt; COLOR: black">2)<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-SIZE: 10pt">中间层</SPAN><SPAN style="FONT-SIZE: 10pt">(UserManager)</SPAN><SPAN style="FONT-SIZE: 10pt">：将</SPAN><SPAN style="FONT-SIZE: 10pt">UserDAO</SPAN><SPAN style="FONT-SIZE: 10pt">作为属性注入，其实现主要是调用数据层接口的一些方法；</SPAN><SPAN style="FONT-SIZE: 10pt">它处于事务控制中。</SPAN></FONT></DIV>
<DIV style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; TEXT-ALIGN: left" align=left><SPAN style="FONT-SIZE: 10pt"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 采用Spring框架实现，IOC与AOP是它的代名词，功能齐全，非常棒的一个架构。</FONT></SPAN></DIV>
<DIV style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; TEXT-ALIGN: left" align=left><SPAN style="FONT-SIZE: 10pt"><FONT color=#000000></FONT></SPAN></DIV>
<DIV style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; TEXT-ALIGN: left" align=left><FONT color=#000000><SPAN style="FONT-SIZE: 10pt; COLOR: black">3)<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="FONT-SIZE: 10pt">数据层</SPAN><SPAN style="FONT-SIZE: 10pt">(UserDAO)</SPAN><SPAN style="FONT-SIZE: 10pt">：实现类继承</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black">HibernateDaoSupport</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: black">类，在该类中可以调用</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black">getHibernateTemplate()</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: black">的一些方法执行具体的数据操作。</SPAN></FONT></DIV>
<DIV style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; TEXT-ALIGN: left" align=left><SPAN style="FONT-SIZE: 10pt; COLOR: black"><FONT color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 采用Hibernate做O/R mapping，从种种迹象可以看出，Hibernate就是EJB3.0的beta版。</FONT></SPAN></DIV></DIV><img src ="http://www.blogjava.net/jorwen/aggbug/29429.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jorwen/" target="_blank">方佳玮</a> 2006-02-01 12:44 <a href="http://www.blogjava.net/jorwen/articles/29429.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>大道至简-Java之23种模式一点就通</title><link>http://www.blogjava.net/jorwen/articles/29428.html</link><dc:creator>方佳玮</dc:creator><author>方佳玮</author><pubDate>Wed, 01 Feb 2006 04:42:00 GMT</pubDate><guid>http://www.blogjava.net/jorwen/articles/29428.html</guid><wfw:comment>http://www.blogjava.net/jorwen/comments/29428.html</wfw:comment><comments>http://www.blogjava.net/jorwen/articles/29428.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/jorwen/comments/commentRss/29428.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jorwen/services/trackbacks/29428.html</trackback:ping><description><![CDATA[FACTORY—人才市场：以往是要哪个人才，就找哪个人才，效率低，现在有了人才市场，我们只需 <BR>直接去人才市场挑一个好了； <BR><BR>BUILDER—生产流水线：以前是手工业作坊式的人工单个单个的生产零件然后一步一步组装做，好 <BR>比有了工业革命，现在都由生产流水线代替了。如要造丰田汽车，先制定汽车的构造如由车胎、方 <BR>向盘、发动机组成。再以此构造标准生产丰田汽车的车胎、方向盘、发动机。然后进行组装。最后 <BR>得到丰田汽车； <BR><BR>PROTOTYPE—印刷术的发明：以前只能临贴才能保持和别人的字迹基本相同，直从印刷技术发明， <BR>从而保证了复制得和原物一模一样； <BR><BR>SINGLETON—唯一：以前是商标满天飞，相同的商标难免造成侵权，直从有商标保护法后，就保证 <BR>了不会再产生第家企业使用相同的商标； <BR>结构型模式 <BR><BR>ADAPTER—集众人之私，成一己之公：武当派张三丰会太极拳，少林派智空大师会金刚般若掌，如 <BR>果他们两个都成为我的师傅，我就既会太极拳，又会金刚般若掌了； <BR><BR>DECORATOR—青出于蓝而胜于蓝：武当派张三丰会太极拳，是我师傅，他教会了我太极拳，但我自 <BR>己还会点蒙古式摔交，张三丰却不会。于是我就成了DECORATOR模式的实现； <BR><BR>BRIDGE—白马非马：马之颜色有黑白，马之性别有公母。我们说”这是马”太抽象，说”这是黑色 <BR>的公马”又太死板，只有将颜色与性别和马动态组合，”这是（黑色的或白色的）（公或母）马” <BR>才显得灵活而飘逸，如此bridge模式精髓得矣。 <BR><BR>COMPOSITE—大家族：子又生孙，孙又生子，子子孙孙，无穷尽也，将众多纷杂的人口组织成一个 <BR>按辈分排列的大家族即是此模式的实现； <BR><BR>FACADE—求同存异：高中毕业需读初中和高中，博士也需读初中和高中，因此国家将初中和高中普 <BR>及成九年制义务教育； <BR><BR>FLYWEIGHT—一劳永逸：认识三千汉字，可以应付日常读书与写字，可见头脑中存在这个汉字库的 <BR>重要； <BR><BR>PROXY—垂帘听政：犹如清朝康熙年间的四大府臣，很多权利不在皇帝手里，必须通过辅佐大臣去 <BR>办； <BR><BR><BR>行为模式 <BR><BR>CHAIN OF RESPONSIBLEITY—租房：以前为了找房到处打听，效率低且找不到好的房源。现在有了 <BR>房屋中介，于是向房屋中介提出租房请求，中介提供一个合适的房源，满意则不再请求，不满意 <BR>继续看房，直到满意为止； <BR><BR>COMMAND—借刀杀人：以前是想杀谁就杀，但一段时间后领悟到，长此以往必将结仇太多，于是假 <BR>手他人，挑拨他人之间的关系从而达到自己的目的； <BR><BR>INTERPRETER—文言文注释：一段文言文，将它翻译成白话文； <BR><BR>ITERATOR—赶尽杀绝：一个一个的搜索，绝不放掉一个； <BR><BR>MEDIATOR—三角债：本来千头万绪的债务关系，忽出来一中介，包揽其一切，于是三角关系变成了 <BR>独立的三方找第四方中介的关系； <BR><BR>MEMENTO—有福同享：我有多少，你就有多少； <BR><BR>OBSERVER—看守者：一旦被看守者有什么异常情况，定会及时做出反应； <BR><BR>STATE—进出自由：如一扇门，能进能出，如果有很多人随时进进出出必定显得杂乱而安全，如今 <BR>设一保安限制其进出，如此各人进出才显得规范； <BR><BR>STRATEGY—久病成良医：如人生病可以有各种症状，但经过长期摸索，就可以总结出感冒、肺病、 <BR>肝炎等几种； <BR><BR><BR>TEMPLATE METHOD——理论不一定要实践：教练的学生会游泳就行了，至于教练会不会则无关紧要； <BR><BR>VISITOR—依法治罪：因张三杀人要被处死，李四偷窃要被罚款。由此势必制定处罚制度，故制定 <BR>法律写明杀人、放火、偷窃等罪要受什么处罚，经通过后须变动要小。今后有人犯罪不管是谁，按 <BR>共条例处罚即是，这就是访问者模式诞生的全过程；<img src ="http://www.blogjava.net/jorwen/aggbug/29428.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jorwen/" target="_blank">方佳玮</a> 2006-02-01 12:42 <a href="http://www.blogjava.net/jorwen/articles/29428.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>