﻿<?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-dyerac-文章分类-转载学习区</title><link>http://www.blogjava.net/dyerac/category/13363.html</link><description>dyerac 的天天天蓝</description><language>zh-cn</language><lastBuildDate>Sun, 19 Aug 2007 07:43:59 GMT</lastBuildDate><pubDate>Sun, 19 Aug 2007 07:43:59 GMT</pubDate><ttl>60</ttl><item><title>JFreeChart Study Resources</title><link>http://www.blogjava.net/dyerac/articles/137511.html</link><dc:creator>dyerac in java...</dc:creator><author>dyerac in java...</author><pubDate>Fri, 17 Aug 2007 05:03:00 GMT</pubDate><guid>http://www.blogjava.net/dyerac/articles/137511.html</guid><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: ------------------------------------------------------------------------------------------jfreechart文档居然是收费的，在网上找了好多资料都是针对1。0之前的版本的，好入容易找到一个1.0下面可以用的package com.meetexpo.cms.backend.util;import ja...&nbsp;&nbsp;<a href='http://www.blogjava.net/dyerac/articles/137511.html'>阅读全文</a><img src ="http://www.blogjava.net/dyerac/aggbug/137511.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/dyerac/" target="_blank">dyerac in java...</a> 2007-08-17 13:03 <a href="http://www.blogjava.net/dyerac/articles/137511.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(zt)乱码解决之道</title><link>http://www.blogjava.net/dyerac/articles/136482.html</link><dc:creator>dyerac in java...</dc:creator><author>dyerac in java...</author><pubDate>Mon, 13 Aug 2007 10:58:00 GMT</pubDate><guid>http://www.blogjava.net/dyerac/articles/136482.html</guid><description><![CDATA[<p>为什么说乱码是中国程序员无法避免的话题呢？这个首先要从编码机制上说起，大家都是中文和英文的编码格式不是一样，解码也是不一样的！如果中国的程序员不会遇到乱码，那么只有使用汉语编程。汉语编程是怎么回事我也不大清楚，应该是前年吧，我一朋友给我介绍汉语编程，怎么不错不错？当时因为学习忙没去关注这个，等我闲了，那个朋友不弄这个，问他他也不说不大清楚，最后自己对这个学习也不了了之了。<br>&nbsp;&nbsp;&nbsp; 今天我写这个不是讲解中英文之间的差距，解码等，我是将我在这几年工作遇到各种各样的乱码的解决方法，总结一样，也希望大家能把自己晕倒解决乱码的方法都说出来，咱们弄一个解决乱码的&#8220;葵花宝典&#8221;。</p>
<p>对于Java由于默认的编码方式是 UNICODE,所以用中文也易出问题,常见的解决是<br>String s2 = new String(s1.getBytes(&#8220;ISO-8859-1&#8221;),&#8221;GBK&#8221;);<br><br><br><span style="COLOR: red">1、utf8解决JSP中文乱码问题</span> <br>一般说来在每个页面的开始处，加入：</p>
<p>&lt;%@ page language="java" contentType="text/html; charset=UTF-8"<br>&nbsp;&nbsp;&nbsp; pageEncoding="UTF-8"%&gt;</p>
<p>&lt;%<br>&nbsp;request.setCharacterEncoding("UTF-8");<br>%&gt; </p>
<p>charset=UTF-8&nbsp; 的作用是指定JSP向客户端输出的编码方式为&#8220;UTF-8&#8221;</p>
<p>pageEncoding="UTF-8"&nbsp; 为了让JSP引擎能正确地解码含有中文字符的JSP页面，这在LINUX中很有效</p>
<p>&nbsp;request.setCharacterEncoding("UTF-8"); 是对请求进行了中文编码</p>
<p>有时，这样仍不能解决问题，还需要这样处理一下：</p>
<p>String msg = request.getParameter("message");<br>&nbsp;String str=new String(msg.getBytes("ISO-8859-1"),"UTF-8");<br>&nbsp;out.println(st);</p>
<p><span style="COLOR: red">2、Tomcat 5.5 中文乱码</span> </p>
<p>)只要把%TOMCAT安装目录%/&nbsp;&nbsp; webapps\servlets-examples\WEB-INF\classes\filters\SetCharacterEncodingFilter.class文件拷到你的webapp目录/filters下，如果没有filters目录，就创建一个。&nbsp;&nbsp; <br>&nbsp; 2)在你的web.xml里加入如下几行：&nbsp;&nbsp; &lt;filter&gt;&nbsp;&nbsp; <br>&nbsp; &lt;filter-name&gt;Set&nbsp;&nbsp; Character&nbsp;&nbsp; Encoding&lt;/filter-name&gt;&nbsp;&nbsp; <br>&nbsp; &lt;filter-class&gt;filters.SetCharacterEncodingFilter&lt;/filter-class&gt;&nbsp;&nbsp; <br>&nbsp; &lt;init-param&gt;&nbsp;&nbsp; <br>&nbsp; &lt;param-name&gt;encoding&lt;/param-name&gt;&nbsp;&nbsp; <br>&nbsp; &lt;param-value&gt;GBK&lt;/param-value&gt;&nbsp;&nbsp; <br>&nbsp; &lt;/init-param&gt;&nbsp;&nbsp; <br>&nbsp; &lt;/filter&gt;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;filter-mapping&gt;&nbsp;&nbsp; <br>&nbsp; &lt;filter-name&gt;Set&nbsp;&nbsp; Character&nbsp;&nbsp; Encoding&lt;/filter-name&gt;&nbsp;&nbsp; <br>&nbsp; &lt;url-pattern&gt;/*&lt;/url-pattern&gt;&nbsp;&nbsp; <br>&nbsp; &lt;/filter-mapping&gt;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; <br>&nbsp; 3)完成.&nbsp;&nbsp; <br>&nbsp; 2&nbsp;&nbsp; get方式的解决办法&nbsp;&nbsp; <br>&nbsp; 1)&nbsp;&nbsp; 打开tomcat的server.xml文件，找到区块，加入如下一行：&nbsp;&nbsp; <br>&nbsp; URIEncoding=&#8221;GBK&#8221;&nbsp;&nbsp; <br>&nbsp; 完整的应如下：&nbsp;&nbsp; <br>&nbsp; &lt;Connector&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp; port="80"&nbsp;&nbsp; maxThreads="150"&nbsp;&nbsp; minSpareThreads="25"&nbsp;&nbsp; maxSpareThreads="75"&nbsp;&nbsp; <br>&nbsp; enableLookups="false"&nbsp;&nbsp; redirectPort="8443"&nbsp;&nbsp; acceptCount="100"&nbsp;&nbsp; <br>&nbsp; debug="0"&nbsp;&nbsp; connectionTimeout="20000"&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp; disableUploadTimeout="true"&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp; URIEncoding="GBK"&nbsp;&nbsp; <br>&nbsp; /&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; <br>&nbsp; 2)重启tomcat,一切OK。</p>
<p><span style="COLOR: red">3、xmlHttpRequest中文问题</span> </p>
<p>页面jsp用的GBK编码 </p>
<p><br>代码<br>&lt;%@ page contentType="text/html; charset=GBK"%&gt;&nbsp; </p>
<p><br>javascript部分 </p>
<p>代码<br>function addFracasReport() {&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; var url="controler?actionId=0_06_03_01&amp;actionFlag=0010";&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; var urlmsg="&amp;reportId="+fracasReport1.textReportId.value;&nbsp; //故障报告表编号&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; var xmlHttp=Common.createXMLHttpRequest();&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; xmlHttp.onreadystatechange = Common.getReadyStateHandler(xmlHttp, eval("turnAnalyPage"));&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; xmlHttp.open("POST",url,true);&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; xmlHttp.setRequestHeader( " Content-Type " , " application/x-www-form-urlencoded);&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; xmlHttp.send(urlmsg);&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>}&nbsp; </p>
<p><br>后台java中获得的reportId是乱码，不知道该怎么转，主要是不知道xmlHttp.send(urlmsg);以后是什么编码？在后面用java来转，试了几种，都没有成功，其中有：</p>
<p><br>代码<br>public static String UTF_8ToGBK(String str) {&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new String(str.getBytes("UTF-8"), "GBK");&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Exception ex) {&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return null;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; public static String UTF8ToGBK(String str) {&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new String(str.getBytes("UTF-16BE"), "GBK");&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Exception ex) {&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return null;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; public static String GBK(String str) {&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new String(str.getBytes("GBK"),"GBK");&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Exception ex) {&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return null;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static String getStr(String str) {&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String temp_p = str;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String temp = new String(temp_p.getBytes("ISO8859_1"), "GBK");&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; temp = sqlStrchop(temp);&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return temp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Exception e) {&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return null;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; }&nbsp; </p>
<p><span style="COLOR: red">4、JDBC ODBC Bridge的Bug及其解决方法</span> </p>
<p>在编写一数据库管理程序时，发现JDBC-ODBC Bridge存在不易发现的Bug。在向数据表插入数据时，如果为英文字符，存储内容完全正确，如果存入中文字符，部分数据库只能存储前七八个中文字符，其他内容被截去，导致存储内容的不完整（有些数据库不存在这个问题，如Sybase SQL Anywhere 5.0。JDBC-ODBC Bridge还存在无法建表的Bug）。 </p>
<p>　　对于广大需要存储中文信息的Java程序员来说，这可是一个不好的消息。要么改用其他语言编程，要么选择其他价格昂贵的数据库产品。&#8220;一次编写，到处运行&#8221;的目标，也大打折扣。能不能采用变通的方法，将中文信息进行处理后再存储来解决这个问题呢？答案是肯定的。 </p>
<p>　　解决问题的具体思路、方法 <br>　　Java采用Unicode码编码方式，中英文字符均采用16bit存储。既然存储英文信息是正确的，根据一定规则，将中文信息转换成英文信息后存储，自然不会出现截尾现象。读取信息时再进行逆向操作，将英文信息还原成中文信息即可。由GB2312编码规则可知，汉字一般为二个高位为1的ASCII码，在转换时将一个汉字的二个高位1去掉，还原时再将二个高位1加上。为了处理含有英文字符的中文字串，对英文字符则需要加上一个Byte 0标记。以下提供的两个公用静态方法，可加入任何一个类中使用。 </p>
<p>　　将中英文字串转换成纯英文字串 <br>　　public static String toTureAsciiStr(String str){ </p>
<p>　　StringBuffer sb = new StringBuffer(); </p>
<p>　　byte[] bt = str.getBytes(); </p>
<p>　　for(int i =0 ;i〈bt.length;i++){ </p>
<p>　　if(bt[i]〈0){ </p>
<p>　　//是汉字去高位1 </p>
<p>　　sb.append((char)(bt[i]&amp;&amp;0x7f)); </p>
<p>　　 }else{//是英文字符 补0作记录 </p>
<p>　　sb.append((char)0); </p>
<p>　　sb.append((char)bt[i]); </p>
<p>　　 } </p>
<p>　　 } </p>
<p>　　return sb.toString(); </p>
<p>　　} </p>
<p>　　将经转换的字串还原 <br>　　public static String unToTrueAsciiStr(String str){ </p>
<p>　　 byte[] bt = str.getBytes(); </p>
<p>　　 int i,l=0,length = bt.length,j=0; </p>
<p>　　 for(i = 0;i〈length;i++){ </p>
<p>　　 if(bt[i] == 0){ </p>
<p>　　 l++; </p>
<p>　　 } </p>
<p>　　 } </p>
<p>　　 byte []bt2 = new byte[length-l]; </p>
<p>　　 for(i =0 ;i〈length;i++){ </p>
<p>　　 if(bt[i] == 0){ </p>
<p>　　 i++; </p>
<p>　　 bt2[j] = bt[i]; </p>
<p>　　 }else{ </p>
<p>　　 bt2[j] = (byte)(bt[i]|0x80); </p>
<p>　　 } </p>
<p>　　 j++; </p>
<p>　　 } </p>
<p>　　String tt = new String(bt2); </p>
<p>　　return tt; </p>
<p>　　} </p>
<p>　　上例在实际编程中效果很好，只是存储的中文信息需要经过同样处理，才能被其他系统使用。而且如果中文字串出现英文字符，实际上增加了额外的存储空间。<br><br><span style="COLOR: red">5、Solaris下Servlet编程的中文问题及解决办法 <br></span>在使用Java开发Internet上的一个应用系统时，发现在Windows下调试完全正常的Servlet，上传到Solaris 服务器上，运行却出现故障——返回的网页不能显示中文，应为中文的信息全为乱码；用中文信息做关键字，不能正确检索数据库。后来采用加入检查代码等方法探知故障原因如下： </p>
<p>　　显示乱码主要是因为通过类 HttpServletResponse提供的方法setContentType 无法改变返回给客户的数据的编码方式，正确的编码方式应为GB2312或者GBK，而事实上为缺省的ISO8859-1。无法检索中文信息则是因为，客户提交的中文信息经浏览器编码到达服务器后，Servlet无法将其正确解码。 </p>
<p>　　举例说明显示乱码解决方法 <br>　　Servlet 一般通常做法如下： </p>
<p>　　public class ZldTestServlet extends HttpServlet { </p>
<p>　　public void doGet (HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException{ </p>
<p>　　//在使用 Writer向浏览器返回数据前，设置 content-type header ，在这里设置相应的字符集gb2312 </p>
<p>　　response.setContentType("text/html;charset=gb2312"); </p>
<p>　　PrintWriter out = response.getWriter(); //* </p>
<p>　　// 正式返回数据 </p>
<p>　　out.println("〈html〉〈head〉〈title〉Servlet test〈/title〉〈/head〉" ); </p>
<p>　　out.println("这是一个测试页！"); </p>
<p>　　out.println("〈/body〉〈/html〉"); </p>
<p>　　out.close(); </p>
<p>　　} </p>
<p>　　 ... </p>
<p>　　} </p>
<p>　　解决页面显示乱码问题，需将*处代码换成如下内容： </p>
<p>　　PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(),"gb2312")); </p>
<p>　　Solaris中文信息检索问题的解决 <br>　　浏览器利用表单向服务器提交信息时，一般采用x-www-form-urlencoded 的MIME格式对数据进行编码。如果使用get方法，参数名称和参数值经编码后附加在URL后，在Java中称作查询串（query string）。 </p>
<p>　　在Servlet程序中，如果采用ServletRequest的方法getParameter取得参数值，在Solaris环境下，对汉字却不能正确解码。因而无法正确检索数据库。 </p>
<p>　　在Java 1.2的包——java.net中提供了URLEncode和URLDecode类。类URLEncode提供了按x-www-form-urlencoded格式对给定串进行转换的方法。类URLEncode则提供了逆方法。 </p>
<p><span style="COLOR: red">6、Common Mail乱码问题</span><br>common mail是一个小而方便的mail包，他实现了对Java Mail的封装，使用起来十分的方便，但是我在使用他的时候发现，使用纯文本的内容发送，结果是乱码，代码如下：<br>public class TestCommonMail {<br>public static void main(String[] args) throws EmailException, MessagingException {<br>SimpleEmail email = new SimpleEmail();<br>email.setCharset("GB2312");<br>email.setHostName("smtp.163.com");<br>email.setSubject("test");<br>email.addTo("<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#116;&#101;&#115;&#116;&#64;&#49;&#54;&#51;&#46;&#99;&#111;&#109;"><font color=#1d58d1>test@163.com</font></a>");<br>email.setFrom("<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#116;&#101;&#115;&#116;&#64;&#49;&#54;&#51;&#46;&#99;&#111;&#109;"><font color=#1d58d1>test@163.com</font></a>");<br>email.setMsg("我的测试");<br>email.setAuthentication("test", "test");<br>email.send();<br>}<br>}</p>
<p>分析了一下commons mail的源码找到了原因。源码如下:<br>public class SimpleEmail extends Email<br>{<br>public Email setMsg(String msg) throws EmailException, MessagingException<br>{<br>if (EmailUtils.isEmpty(msg))<br>{<br>throw new EmailException("Invalid message supplied");<br>}</p>
<p>setContent(msg, TEXT_PLAIN);<br>return this;<br>}<br>}</p>
<p>Email代码片段<br>public void setContent(Object aObject, String aContentType)<br>{<br>this.content = aObject;<br>if (EmailUtils.isEmpty(aContentType))<br>{<br>this.contentType = null;<br>}<br>else<br>{<br>// set the content type<br>this.contentType = aContentType;</p>
<p>// set the charset if the input was properly formed<br>String strMarker = "; charset=";<br>int charsetPos = aContentType.toLowerCase().indexOf(strMarker);<br>if (charsetPos != -1)<br>{<br>// find the next space (after the marker)<br>charsetPos += strMarker.length();<br>int intCharsetEnd =<br>aContentType.toLowerCase().indexOf(" ", charsetPos);</p>
<p>if (intCharsetEnd != -1)<br>{<br>this.charset =<br>aContentType.substring(charsetPos, intCharsetEnd);<br>}<br>else<br>{<br>this.charset = aContentType.substring(charsetPos);<br>}<br>}<br>}<br>}</p>
<p>email.send();的send方法将调用<br>public void buildMimeMessage() throws EmailException<br>{<br>try<br>{<br>this.getMailSession();<br>this.message = new MimeMessage(this.session);</p>
<p>if (EmailUtils.isNotEmpty(this.subject))<br>{<br>if (EmailUtils.isNotEmpty(this.charset))<br>{<br>this.message.setSubject(this.subject, this.charset);<br>}<br>else<br>{<br>this.message.setSubject(this.subject);<br>}<br>}</p>
<p>// ========================================================<br>// Start of replacement code<br>if (this.content != null)<br>{<br>this.message.setContent(this.content, this.contentType);<br>}<br>// end of replacement code<br>// ========================================================<br>else if (this.emailBody != null)<br>{<br>this.message.setContent(this.emailBody);<br>}<br>else<br>{<br>this.message.setContent("", Email.TEXT_PLAIN);<br>}</p>
<p>if (this.fromAddress != null)<br>{<br>this.message.setFrom(this.fromAddress);<br>}<br>else<br>{<br>throw new EmailException("Sender address required");<br>}</p>
<p>if (this.toList.size() + this.ccList.size() + this.bccList.size() == 0)<br>{<br>throw new EmailException(<br>"At least one receiver address required");<br>}</p>
<p>if (this.toList.size() &gt; 0)<br>{<br>this.message.setRecipients(<br>Message.RecipientType.TO,<br>this.toInternetAddressArray(this.toList));<br>}</p>
<p>if (this.ccList.size() &gt; 0)<br>{<br>this.message.setRecipients(<br>Message.RecipientType.CC,<br>this.toInternetAddressArray(this.ccList));<br>}</p>
<p>if (this.bccList.size() &gt; 0)<br>{<br>this.message.setRecipients(<br>Message.RecipientType.BCC,<br>this.toInternetAddressArray(this.bccList));<br>}</p>
<p>if (this.replyList.size() &gt; 0)<br>{<br>this.message.setReplyTo(<br>this.toInternetAddressArray(this.replyList));<br>}</p>
<p>if (this.headers.size() &gt; 0)<br>{<br>Iterator iterHeaderKeys = this.headers.keySet().iterator();<br>while (iterHeaderKeys.hasNext())<br>{<br>String name = (String) iterHeaderKeys.next();<br>String value = (String) headers.get(name);<br>this.message.addHeader(name, value);<br>}<br>}</p>
<p>if (this.message.getSentDate() == null)<br>{<br>this.message.setSentDate(getSentDate());<br>}</p>
<p>if (this.popBeforeSmtp)<br>{<br>Store store = session.getStore("pop3");<br>store.connect(this.popHost, this.popUsername, this.popPassword);<br>}<br>}<br>catch (MessagingException me)<br>{<br>throw new EmailException(me);<br>}<br>}<br>由代码可以知道纯文本方式最终调用了Java Mail的<br>message.setContent(this.content, this.contentType);<br>content是内容<br>contentType是类型，如text/plain,<br>(我们可以试试直接用Java mail发邮件，设置文本内容不使用setText方法，也使用setContent("测试", "text/plain")方式，你可以看到内容也是乱码)<br>关键就在于text/plain，我们改成text/plain;charset=gb2312，ok乱码解决了。在commons mail我们看SimpleEmail 类中setMsg方法调用的就是 setContent(msg, TEXT_PLAIN);我们只需要将Email类中的常量TEXT_PLAIN修改一下加入 charset=你的字符集 ，重新打包jar，这样就可以了</p>
<p><span style="COLOR: red">7、toad的字符集的设置与oracle的安装</span><br>oracle数据库服务器的安装一般是中文字符集，有时安装在不同的平台下，设置为ISO编码，toad是oracle开发的最好工具，不是我说的，可是中文环境下安装的toad，打开英文字符的oracle时，中文全是乱码。必须进行设置</p>
<p>环境变量---〉系统变量<br>加<br>&nbsp; NLS_lANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK<br>&nbsp; 或<br>&nbsp; NLS_lANG=AMERICAN_AMERICA.WE8ISO8859P1</p>
<p>AMERICAN_AMERICA.WE8MSWIN1252<br>&nbsp;<br>&nbsp;或者</p>
<p>&nbsp;打开注册表，点击HKEY_LOCAL_MATHINE<br>再点击Software,再点击ORACLE<br>在点击HOME（ORACLE所在目录）<br>在注册表的右半面有NLS_LANG,<br>双击它，将你想要的覆盖掉原来的就可以了<br>最好记下旧的，以便可以改回来。</p>
<p><br>connect sys/chang_on_install<br>update props$<br>set value$='ZHS16CGB231280'<br>where name='NLS_CHARACTERSET';<br>commit;<br>&nbsp;这样就OK了</p>
<p>&nbsp;<br><span style="COLOR: red">8、如何解決GWT(google web toolkit)中文的問題</span><br>GWT 中文乱码解决方法 </p>
<p>1.把你要显示的中文&#8220;测试字符串&#8221;输入到一个文件，如:1.txt <br>2.进入命令行,进入1.txt所在的目录,敲入以下命令:native2ascii.exe 1.txt 2.txt 回车。这样就生成了另外一个文件2.txt。 <br>3.2.txt的内容如下:\u6d4b\u8bd5\u5b57\u7b26\u4e32 <br>4.然后用上面的编码，在gwt中使用，就可以了. </p>
<p><span style="COLOR: red">9、xmlHttp得到的网页怎么是乱码？</span> <br>(1)在服务器端使用WebRequest而不是xmlHttp<br>(2) 将 </p>
<p>StreamReader sr = new StreamReader(stream);</p>
<p><br>对于简体中文改成：</p>
<p>StreamReader sr = new StreamReader(stream , Encoding.Default );<br>对于utf-8改成：</p>
<p><br>StreamReader sr = new StreamReader(stream , Encoding.UTF8 );<br>当然，Encoding枚举还有很多其他的成员，对于不同的编码content-type可以有选择的应用</p>
<p>(3)后来我发现无论是content-type是gb2312还是utf-8,用</p>
<p><br>StreamReader sr = new StreamReader(stream , Encoding.Default );</p>
<p>都可以返回正常的汉字，所以统一的改成Encoding.Default</p>
<p>&nbsp;</p>
<p><br>--------------------------------------------------------------------------------</p>
<p>最后，在服务器端从一个url获得网页的源代码的代码如下：</p>
<p>&nbsp;</p>
<p>/// &lt;summary&gt;<br>/// post一个指定的url，获得网页的源代码(用WebRequest实现)<br>/// &lt;/summary&gt;<br>/// &lt;param name="url"&gt;&lt;/param&gt;<br>/// &lt;returns&gt;<br>/// 如果请求失败，返回null<br>/// 如果请求成功，返回网页的源代码<br>/// &lt;/returns&gt;<br>public static string GetContentFromUrl2( string url )<br>{<br>&nbsp;&nbsp;&nbsp; //变量定义<br>&nbsp;&nbsp;&nbsp; string respstr;</p>
<p>&nbsp;&nbsp;&nbsp; WebRequest myWebRequest=WebRequest.Create(url);<br>&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myWebRequest.PreAuthenticate=true;<br>&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NetworkCredential networkCredential=new NetworkCredential( username , password , domain );<br>&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myWebRequest.Credentials=networkCredential;</p>
<p>&nbsp;&nbsp;&nbsp; // Assign the response object of 'WebRequest' to a 'WebResponse' variable.<br>&nbsp;&nbsp;&nbsp; WebResponse myWebResponse=myWebRequest.GetResponse();<br>&nbsp;&nbsp;&nbsp; System.IO.Stream stream = myWebResponse.GetResponseStream();<br>&nbsp;&nbsp;&nbsp; StreamReader sr = new StreamReader(stream , Encoding.Default );<br>&nbsp;&nbsp;&nbsp; //以字符串形式读取数据流<br>&nbsp;&nbsp;&nbsp; respstr = sr.ReadToEnd();<br>&nbsp;&nbsp;&nbsp; sr.Close(); <br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; return respstr;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>}</p>
<img src ="http://www.blogjava.net/dyerac/aggbug/136482.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/dyerac/" target="_blank">dyerac in java...</a> 2007-08-13 18:58 <a href="http://www.blogjava.net/dyerac/articles/136482.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JDK1.6.0新特性详解与代码示例</title><link>http://www.blogjava.net/dyerac/articles/131761.html</link><dc:creator>dyerac in java...</dc:creator><author>dyerac in java...</author><pubDate>Sun, 22 Jul 2007 14:44:00 GMT</pubDate><guid>http://www.blogjava.net/dyerac/articles/131761.html</guid><wfw:comment>http://www.blogjava.net/dyerac/comments/131761.html</wfw:comment><comments>http://www.blogjava.net/dyerac/articles/131761.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/dyerac/comments/commentRss/131761.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/dyerac/services/trackbacks/131761.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: JDK1.6.0新特性详解与代码示例	JDK6.0发布有段时间了，新的JDK也有不少新的特性，我去网上搜集了一下，列在下面和大家一起学习．１．Desktop和SystemTray. 在JDK6中,AWT新增加了两个类:Desktop和SystemTray,前者可以用来打开系统默认浏览器浏览指定的URL,打开系统默认邮件客户端给指定的邮箱发邮件,用默认应用程序打开或编辑文件(比如,用...&nbsp;&nbsp;<a href='http://www.blogjava.net/dyerac/articles/131761.html'>阅读全文</a><img src ="http://www.blogjava.net/dyerac/aggbug/131761.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/dyerac/" target="_blank">dyerac in java...</a> 2007-07-22 22:44 <a href="http://www.blogjava.net/dyerac/articles/131761.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>考虑将 SQLJ 用于 DB2 V8 Java 应用程序</title><link>http://www.blogjava.net/dyerac/articles/129147.html</link><dc:creator>dyerac in java...</dc:creator><author>dyerac in java...</author><pubDate>Mon, 09 Jul 2007 13:54:00 GMT</pubDate><guid>http://www.blogjava.net/dyerac/articles/129147.html</guid><wfw:comment>http://www.blogjava.net/dyerac/comments/129147.html</wfw:comment><comments>http://www.blogjava.net/dyerac/articles/129147.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/dyerac/comments/commentRss/129147.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/dyerac/services/trackbacks/129147.html</trackback:ping><description><![CDATA[<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr vAlign=top>
            <td width="100%">
            <h1>考虑将 SQLJ 用于 DB2 V8 Java 应用程序</h1>
            <img class=display-img height=6 alt="" src="http://www.ibm.com/i/c.gif" width=1></td>
            <td class=no-print width=192><img height=18 alt=developerWorks src="http://www.ibm.com/developerworks/cn/i/dw.gif" width=192></td>
        </tr>
    </tbody>
</table>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr vAlign=top>
            <td width=10><img height=1 alt="" src="http://www.ibm.com/i/c.gif" width=10></td>
            <td width="100%">
            <table class=no-print cellSpacing=0 cellPadding=0 width=160 align=right border=0>
                <tbody>
                    <tr>
                        <td width=10><img height=1 alt="" src="http://www.ibm.com/i/c.gif" width=10></td>
                        <td>
                        <table cellSpacing=0 cellPadding=0 width=150 border=0>
                            <tbody>
                                <tr>
                                    <td class=v14-header-1-small>文档选项</td>
                                </tr>
                            </tbody>
                        </table>
                        <table class=v14-gray-table-border cellSpacing=0 cellPadding=0 border=0>
                            <tbody>
                                <tr>
                                    <td class=no-padding width=150>
                                    <table cellSpacing=0 cellPadding=0 width=143 border=0>
                                        <img height=1 alt="" src="http://www.ibm.com/i/c.gif" width=8>
                                        <form name=email action=https://www.ibm.com/developerworks/secure/email-it.jsp>
                                            <input type=hidden value="了解为什么 SQLJ 是注重安全性、性能和简单性的开发人员所选择的语言。其中还包含了代码样本。" name=body><input type=hidden value="考虑将 SQLJ 用于 DB2 V8 Java 应用程序" name=subject><input type=hidden value=cn name=lang>
                                            <script language=JavaScript type=text/javascript>
                                            <!--
                                            document.write('
                                            <tr valign="top">
                                                <td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt=""/></td>
                                                <td width="16"><img src="//www.ibm.com/i/v14/icons/em.gif" height="16" width="16" vspace="3" alt="将此页作为电子邮件发送" /></td>
                                                <td width="122">
                                                <p><a class="smallplainlink" href="javascript:document.email.submit();"><strong>将此页作为电子邮件发送</strong></a></p>
                                                </td>
                                            </tr>
                                            ');
                                            //-->
                                            </script>
                                            <tbody>
                                                <tr vAlign=top>
                                                    <td width=8><img height=1 alt="" src="http://www.ibm.com/i/c.gif" width=8></td>
                                                    <td width=16><img height=16 alt=将此页作为电子邮件发送 src="http://www.ibm.com/i/v14/icons/em.gif" width=16 vspace=3></td>
                                                    <td width=122>
                                                    <p><a class=smallplainlink href="javascript:document.email.submit();"><strong><font color=#5c81a7 size=2>将此页作为电子邮件发送</font></strong></a></p>
                                                    </td>
                                                </tr>
                                                <noscript>
                                                <tr valign="top">
                                                    <td width="8"><img alt="" height="1" width="8" src="//www.ibm.com/i/c.gif"/></td>
                                                    <td width="16"><img alt="" width="16" height="16" src="//www.ibm.com/i/c.gif"/></td>
                                                    <td class="small" width="122">
                                                    <p><span class="ast">未显示需要 JavaScript 的文档选项</span></p>
                                                    </td>
                                                </tr>
                                                </noscript>
                                            </form>
                                        </tbody>
                                    </table>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        <!--start RESERVED FOR FUTURE USE INCLUDE FILES--><!-- this content will be automatically generated across all content areas --><br><!--end RESERVED FOR FUTURE USE INCLUDE FILES--><br></td>
                    </tr>
                </tbody>
            </table>
            <p>级别: 初级</p>
            <p><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#author"><font color=#996699>Connie Tsui</font></a>, DB2 解决方案集成团队, IBM 多伦多实验室<br></p>
            <p>2003 年 2 月 01 日</p>
            <blockquote>了解为什么 SQLJ 是注重安全性、性能和简单性的开发人员所选择的语言。其中还包含了代码样本。</blockquote><!--start RESERVED FOR FUTURE USE INCLUDE FILES--><!-- include java script once we verify teams wants to use this and it will work on dbcs and cyrillic characters --><!--end RESERVED FOR FUTURE USE INCLUDE FILES-->
            <p><a name=0><span class=atitle>简介</span></a></p>
            <p>使用 Java 访问关系数据的标准方法有两种：SQLJ 和 JDBC。对于 IBM&#174; DB2&#174; Universal Database（UDB）应用程序，为什么应该考虑 SQLJ 呢？这是因为当应用程序员要考虑安全性、性能和简单性时，往往会选择 SQLJ 这样的语言。本文向您介绍了有关 SQLJ 的一些背景，讨论了相对于 JDBC，SQLJ 所具有的优势，还特别指出了 DB2 UDB V8.1 所提供的一些新增和改进的 SQLJ 特性。</p>
            <br>
            <table cellSpacing=0 cellPadding=0 width="100%" border=0>
                <tbody>
                    <tr>
                        <td><img height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"><br><img height=6 alt="" src="http://www.ibm.com/i/c.gif" width=8 border=0></td>
                    </tr>
                </tbody>
            </table>
            <table class=no-print cellSpacing=0 cellPadding=0 align=right>
                <tbody>
                    <tr align=right>
                        <td><img height=4 alt="" src="http://www.ibm.com/i/c.gif" width="100%"><br>
                        <table cellSpacing=0 cellPadding=0 border=0>
                            <tbody>
                                <tr>
                                    <td vAlign=center><img height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><br></td>
                                    <td vAlign=top align=right><a class=fbox href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#main"><strong><font color=#996699>回页首</font></strong></a></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br><br>
            <p><a name=1><span class=atitle>SQLJ 的背景</span></a></p>
            <p>1997 年 4 月，一个由数据库供应商组成的非正式和开放小组开始定期开会，交流有关如何在 Java 编程语言中使用静态 SQL 语句和构造的想法。主要参与者包括 IBM、Oracle、Compaq、Informix&#174;、Sybase、Cloudscape 和 Sun Microsystems。该小组把他们正制定的规范命名为 JSQL。但他们发现术语 JSQL 是一个注册商标，因此就将 JSQL 重命名为 SQLJ。1997 年 12 月，Oracle 向其它成员提供了一个 Java 中嵌入式 SQL 的参考实现。这个参考实现可以在任何支持 JDK 1.1 的平台上运行，并且它与供应商无关。1998 年 12 月，完成了 Java 中嵌入式 SQL 规范的整个开发工作，并被接纳为 ANSI 标准 <em>Database Language - SQL, Part 10 Object Language Bindings (SQL/OLB)</em>ANSI x3.135.10-1998。这个规范一般称为 SQLJ 规范的第 0 部分。现在它被称为 SQL/OLB（对象语言绑定，Object Language Binding）。 </p>
            <p>SQLJ 规范目前由两部分组成：</p>
            <ul>
                <li><strong>SQL/OLB：Java 中的嵌入式 SQL</strong> <br>这部分标准规定了在 Java 方法中嵌入 SQL 的语法和语义，还规定了一些机制来确保生成的 SQLJ 应用程序的二进制可移植性。这正是本文要阐述的主题。
                <li><strong>SQL/JRT：使用 Java 编程语言的 SQL 例程和类型</strong> <br>这部分标准包含以下内容：
                <ul>
                    <li>将 Java 静态方法作为 SQL 存储过程和用户定义的函数来调用的规范。它定义了 SQL 扩展，用于在 SQL 系统中安装 Java 类，在 SQL 中以 SQL 函数和存储过程方式调用 Java 类的静态方法，获取指定的参数输出值以及返回 SQL 结果集。
                    <li>将 Java 类用作 SQL 用户定义的数据类型的规范。它定义了 SQL 扩展，用于将 Java 类用作 SQL 中的数据类型。 </li>
                </ul>
                </li>
            </ul>
            <p><strong>术语：</strong>当我们在本文其余部分使用术语 SQLJ 时，仅指 SQL/OLB。 </p>
            <br>
            <table cellSpacing=0 cellPadding=0 width="100%" border=0>
                <tbody>
                    <tr>
                        <td><img height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"><br><img height=6 alt="" src="http://www.ibm.com/i/c.gif" width=8 border=0></td>
                    </tr>
                </tbody>
            </table>
            <table class=no-print cellSpacing=0 cellPadding=0 align=right>
                <tbody>
                    <tr align=right>
                        <td><img height=4 alt="" src="http://www.ibm.com/i/c.gif" width="100%"><br>
                        <table cellSpacing=0 cellPadding=0 border=0>
                            <tbody>
                                <tr>
                                    <td vAlign=center><img height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><br></td>
                                    <td vAlign=top align=right><a class=fbox href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#main"><strong><font color=#996699>回页首</font></strong></a></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br><br>
            <p><a name=2><span class=atitle>SQLJ 编程环境</span></a></p>
            <p>SQLJ 环境由两个阶段组成：开发和运行时。本节向您介绍每个阶段所涉及到的组件以及各组件间的关系。</p>
            <p><a name=N10081><span class=smalltitle><strong><font face=Arial>开发 SQLJ 应用程序</font></strong></span></a></p>
            <p>使用 SQLJ 开发应用程序需要三个组件：转换程序、概要文件定制程序和概要文件绑定程序。有三个实用程序提供了支持这三个组件的功能，它们分别是： <strong>sqlj、db2sqljcustomize</strong>和 <strong>db2sqljbind</strong>。这里对 <a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#fig1"><font color=#996699>图 1</font></a>中演示的过程作一个概述： </p>
            <ol>
                <li>首先，调用 SQLJ 转换程序（sqlj）以读取 SQLJ 源文件并检查该程序中 SQLJ 语法的正确性。转换程序生成一个 Java 源文件，可能不生成 SQLJ 概要文件或生成多个 SQLJ 概要文件，并且如果所生成的 Java 源文件中没有错误，那么它还可以选择将该源文件编译成字节码（缺省情况）。生成的 Java 源文件将嵌入式 SQL 替代为对执行 SQL 操作的 SQLJ 运行时的调用。
                <li>接着，调用 SQLJ 概要文件定制程序（db2sqljcustomize）来为生成的序列化概要文件创建 DB2 定制。该定制程序可以选择（缺省情况下） <em>联机检查</em>能够动态编译的 SQL 语句。联机检查执行语法、语义和模式验证。也可以选择（缺省情况下）调用 SQLJ 概要文件绑定程序以绑定 DB2 包。
                <li>如果选择在概要文件定制期间不执行自动绑定，那么可以单独调用 SQLJ 概要文件绑定程序（db2sqljbind），以将先前定制的 SQLJ 概要文件绑定到数据库。
                <li>不管概要文件是否被定制，要查看其内容，可以使用 SQLJ 概要文件打印程序（db2sqljprint）以文本格式打印出概要文件的内容。 </li>
            </ol>
            <br><br><a name=fig1><strong>图 1. SQLJ 开发环境</strong></a><br><img height=484 alt="SQLJ 开发环境" src="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/images/sqlj-dev_revised.gif" width=504> <br>
            <p><a name=N100B5><span class=smalltitle><strong><font face=Arial>执行 SQLJ 应用程序</font></strong></span></a></p>
            <p>为访问数据库，SQLJ 运行时要依靠 JDBC 驱动程序来获取数据库连接。未定制的 SQLJ 应用程序可以与任何 JDBC 2.0 驱动程序一起运行。在开发期间，为了测试，只需运行未定制应用程序。要运行定制的 SQLJ 应用程序，可以使用 V8 基于 CLI 的 JDBC 类型 2 驱动程序 － 通用 JDBC 驱动程序（类型 2 或类型 4）来建立数据库连接。本节中，我们只描述用于定制的 SQLJ 应用程序的运行时环境。</p>
            <p>当您运行 SQLJ 应用程序时，SQLJ 运行时从定制的概要文件中读取有关 SQL 操作的信息，并执行与存储在定制中的包关键信息（包名、包一致性标记和集合名）相符的 DB2 包中的语句。</p>
            <br><br><a name=N100C3><strong>图 2. SQLJ 运行时环境</strong></a><br><img height=466 alt="SQLJ 运行时环境" src="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/images/sqlj-run_revised.gif" width=385> <br><br>
            <table cellSpacing=0 cellPadding=0 width="100%" border=0>
                <tbody>
                    <tr>
                        <td><img height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"><br><img height=6 alt="" src="http://www.ibm.com/i/c.gif" width=8 border=0></td>
                    </tr>
                </tbody>
            </table>
            <table class=no-print cellSpacing=0 cellPadding=0 align=right>
                <tbody>
                    <tr align=right>
                        <td><img height=4 alt="" src="http://www.ibm.com/i/c.gif" width="100%"><br>
                        <table cellSpacing=0 cellPadding=0 border=0>
                            <tbody>
                                <tr>
                                    <td vAlign=center><img height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><br></td>
                                    <td vAlign=top align=right><a class=fbox href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#main"><strong><font color=#996699>回页首</font></strong></a></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br><br>
            <p><a name=3><span class=atitle>SQLJ 相对于 JDBC 的优势</span></a></p>
            <p>SQLJ 规范和 JDBC 规范都描述了如何使用 Java 来访问关系数据库。本节从以下几个方面讨论它们之间的差异：</p>
            <ul>
                <li><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#standards"><font color=#996699>标准和 SQL 规范级别</font></a>
                <li><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#security"><font color=#996699>安全性</font></a>
                <li><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#performance"><font color=#996699>性能</font></a>
                <li><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#syntax"><font color=#996699>语法</font></a>
                <li><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#interop"><font color=#996699>SQLJ 和 JDBC 互操作性</font></a>
                <li><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#type"><font color=#996699>类型和模式检查</font></a> </li>
            </ul>
            <p><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#table1"><font color=#996699>表 1</font></a>汇总了我们在本节中所描述的 SQLJ 和 JDBC 之间的差异。 </p>
            <p><a name=standards><span class=smalltitle><strong><font face=Arial>标准和 SQL 规范级别</font></strong></span></a></p>
            <p>SQLJ 是 <em>ISO/IEC 9075-10:2000 Information technology -- Database languages -- SQL -- Part 10: Object Language Bindings (SQL/OLB)</em>的实现。SQLJ 不属于 J2EE 平台。 </p>
            <p>JDBC 是 J2SE 1.4 和 J2EE 1.4 平台规范的一个必不可少的组件。它自 Java 软件开发工具箱（Java Software Development Kit，JDK）V1.1 之后已成为其核心部件。它包含在 java.sql 包中。JDBC 驱动程序必须至少支持 Entry SQL-92 语句，在该规范中还定义了一些扩展。</p>
            <p><a name=security><span class=smalltitle><strong><font face=Arial>安全性</font></strong></span></a></p>
            <p>SQLJ 中实现的安全性权限模型是用户考虑使用 SQLJ 的一个主要原因。使用静态 SQL，安全性特权就被指派给了包创建者，并被存储在 DB2 包中。</p>
            <p>使用定制的 DB2 SQLJ，静态地执行 SQL；因此使用包所有者的特权来执行 SQL 语句。任何运行 SQLJ 应用程序的其他用户都必须被授予具有该包的 EXECUTE 特权。即，被授权可以运行程序的用户未必有权对该程序所查询或正在修改的同一表或视图执行 SELECT、UPDATE、DELETE 或 INSERT 操作，除非显式地授予该用户相应的特权。</p>
            <p>拥有连接到数据库并执行 JDBC 应用程序特权的人可以执行这些应用程序中的 SQL 语句。因此，用户必须获得访问表的特权。</p>
            <p><a name=N1012B><span class=smalltitle><strong><font face=Arial>性能 </font></strong></span></a></p>
            <p>SQLJ 允许在 Java 程序中嵌入 SQL 语句，这类似于 SQL-92 允许 SQL 语句嵌入到 C、COBOL、FORTRAN 以及其它编程语言中的方式。但是，根据 SQLJ 概要文件是否被定制，可以决定 SQLJ 应用程序是动态还是静态地运行。当将包存储在 DB2 数据库中时，就会预编译 SQLJ 应用程序并优化 SQL 语句的路径长度。静态执行的 SQLJ 应用程序的性能会优于 JDBC 的性能。</p>
            <p>如果想利用静态执行（我们建议这样做），必须使用 SQLJ 概要文件定制程序来定制概要文件。</p>
            <p>JDBC 提供了 SQL 语句的动态执行。如果这些语句中存在语法或语义错误，那么在该应用程序运行时任何此类异常都会产生。</p>
            <p>使用 DB2 UDB 监视器可以验证静态或动态的 SQL 语句处理。监控方法有两种：快照监控和事件监控。快照监视器提供有关数据库在某个特定时间点的活动信息。事件监视器记录了 DB2 UDB 事件发生的特定位置。下面的 <a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#listing1"><font color=#996699>清单 1</font></a> 摘自从 JDBC 程序生成的事件监视器的样本输出。&#8220;Type: Dynamic&#8221;告诉您动态执行了 <code>SELECT job FROM staff WHERE name = ?</code> 语句。 </p>
            <p><a name=listing1>清单 1. 从 JDBC 程序生成的事件监视器的样本输出</a> <br>
            <table cellSpacing=0 cellPadding=0 width="100%" border=0>
                <tbody>
                    <tr>
                        <td class=code-outline>
                        <pre class=displaycode>10) Statement Event ...
                        Appl Handle: 23
                        Appl Id: G91AA377.G576.00F306261BF2
                        Appl Seq number: 0001
                        Record is the result of a flush: FALSE
                        -------------------------------------------
                        <span class=boldcode><strong>Type</strong></span>     :
                        <span class=boldcode>
                        <strong>            Dynamic
                        </strong></span>
                        Operation: Prepare
                        Section  : 1
                        Creator  : NULLID
                        Package  : SYSSH200
                        Consistency Token  : SYSLVL01
                        Package Version ID  :
                        Cursor   : SQL_CURSH200C1
                        Cursor was blocking: FALSE
                        Text     : SELECT job FROM staff WHERE name = ?
                        </pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br></p>
            <p><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#listing2"><font color=#996699>清单 2</font></a>摘自从 SQLJ 程序生成的事件监视器的样本输出。输出中的&#8220;Type: Static&#8221;和&#8220;Package: SRQT402&#8221;告诉您，对 SRQT402 包静态执行了该语句。 </p>
            <p><a name=listing2>清单 2. 从 SQLJ 程序生成的事件监视器的样本输出</a> <br>
            <table cellSpacing=0 cellPadding=0 width="100%" border=0>
                <tbody>
                    <tr>
                        <td class=code-outline>
                        <pre class=displaycode>10) Statement Event ...
                        Appl Handle: 12
                        Appl Id: G91ABD18.G47D.00F306C01D63
                        Appl Seq number: 0001
                        Record is the result of a flush: FALSE
                        -------------------------------------------
                        Type     :
                        <span class=boldcode>
                        <strong>            Static
                        </strong></span>
                        Operation: Execute
                        Section  : 1
                        Creator  : NULLID
                        Package  :
                        <span class=boldcode>
                        <strong>            SRQT402
                        </strong></span>
                        Consistency Token  : SARoQCAp
                        Package Version ID  :
                        Cursor   :
                        Cursor was blocking: FALSE
                        </pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br></p>
            <p><strong>注：</strong>有些语句对于定制的 SQLJ 程序会正确执行，但对于未定制的 SQLJ 程序就不会正确执行。可滚动游标的 UPDATE/DELETE WHERE CURRENT OF 就是这样一个示例。一般而言，如果底层 JDBC 驱动程序不支持某个功能，那么未定制的 SQLJ 程序也不会支持该功能。 </p>
            <p><strong>性能技巧：</strong> <br>对于单个 select 查询，与盲目地对庞大的 JDBC ResultSets 执行操作相比，可以通过使用由 SQLJ 提供的 SELECT INTO 语法来减少网络活动。 </p>
            <p><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#fig3"><font color=#996699>图 3</font></a>比较了用于单个 select 查询的 SQLJ 和 JDBC 语法。 </p>
            <p><a name=fig3>图 3. 使用 SQLJ 和 JDBC 检索一个行</a>
            <table cellSpacing=1 cellPadding=5 width=500 border=1>
                <tbody>
                    <tr vAlign=top>
                        <td>
                        <p><strong>SQLJ 语法：</strong> </p>
                        <code>#sql [conCtx] { SELECT job INTO :job FROM staff WHERE name = :name };</code>
                        <p><strong>JDBC 语法：</strong> </p>
                        <table cellSpacing=0 cellPadding=0 width="100%" border=0>
                            <tbody>
                                <tr>
                                    <td class=code-outline>
                                    <pre class=displaycode>PreparedStatement pstmt = con.prepareStatement(
                                    "SELECT job FROM staff WHERE name = ? FETCH FIRST 1 ROW ONLY" );
                                    ResultSet rs = pstmt.executeQuery();
                                    if ( rs.next() )
                                    job = rs.getString(1);
                                    else
                                    job = null;
                                    pstmt.close();
                                    </pre>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        <br></td>
                    </tr>
                </tbody>
            </table>
            </p>
            <p><a name=N101C0><span class=smalltitle><strong><font face=Arial>语法 </font></strong></span></a></p>
            <p><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#fig3"><font color=#996699>图 3</font></a>表明 SQLJ 语法在简单性方面优于 JDBC。SQLJ 的简单性受到了许多 Java 开发人员的欢迎。编写 SQLJ 模块通常要比 JDBC 模块简洁且容易。这暗示着 SQLJ 可以使开发周期缩短并减少开发和维护成本。 <a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#fig4"><font color=#996699>图 4</font></a>向您显示了 SQLJ 可以多么简单地向数据库插入一行数据。如果您已有了用其它语言（如 C 或 COBOL）编写的嵌入式 SQL 应用程序，那么就可以使用 SQLJ 轻松地将应用程序迁移到 Java。 </p>
            <p><a name=fig4>图 4. 使用 SQLJ vs. JDBC 插入一个行</a> </p>
            <p><strong>SQLJ 语法：</strong> </p>
            <code>sql [conCtx] { INSERT INTO sales VALUES(:date, :salesperson, :region, :sales) };</code>
            <p><strong>JDBC 语法：</strong> </p>
            <table cellSpacing=0 cellPadding=0 width="100%" border=0>
                <tbody>
                    <tr>
                        <td class=code-outline>
                        <pre class=displaycode>PreparedStatement pstmt = con.prepareStatement( "INSERT INTO sales VALUES (?, ?, ?, ?)" );
                        // set input parameter
                        pstmt.setObject(1, date);
                        pstmt.setString(2, salesperson);
                        pstmt.setString(3, region);
                        pstmt.setInteger(4, sales);
                        pstmt.executeUpdate();
                        pstmt.close();
                        </pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br>
            <p><a name=N101F0><span class=smalltitle><strong><font face=Arial>SQLJ 和 JDBC 互操作性 </font></strong></span></a></p>
            <p>SQLJ 语言允许您在 SQLJ 应用程序中使用 JDBC 语句。要使 JDBC 和 SQLJ 之间便于交互，SQLJ 提供了一种方法以便在同一应用程序内共享 SQLJ 连接和 JDBC 连接，这种方法还可以从 SQLJ 迭代器中获取 JDBC 结果集，或从 JDBC 迭代器中获取 SQLJ 结果集。</p>
            <p><strong>何时需要在 SQLJ 应用程序中使用 JDBC？</strong> <br>您需要将 JDBC 用于动态操作时；即，在编写程序时不清楚 SQL 操作的时候。 <a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#listing3"><font color=#996699>清单 3</font></a>演示了在 SQLJ 程序内用 JDBC 来执行动态查询（WHERE 子句中的名称在开发时是未知的），以及如何将 JDBC 结果集转换到 SQLJ 迭代器。 </p>
            <p>与 SQLJ 不同的是，JDBC 不能识别 SQLJ 语法，而且 SQL 语句不能嵌入到 JDBC 应用程序。</p>
            <p><a name=listing3>清单 3. 将 JDBC 结果集转换到 SQLJ 迭代器</a> <br>
            <table cellSpacing=1 cellPadding=5 width=500 border=1>
                <tbody>
                    <tr vAlign=top>
                        <td>
                        <table cellSpacing=0 cellPadding=0 width="100%" border=0>
                            <tbody>
                                <tr>
                                    <td class=code-outline>
                                    <pre class=displaycode>Public class ResultSetInterop
                                    {
                                    #sql public static iterator Employees (String name, double salary);
                                    public static void main(String[] argv) throws SQLException
                                    {
                                    // the code for creating the SQLJ connection context (conCtx) and
                                    // the Connection object (con) is omitted
                                    // create a JDBC statement object to execute a dynamic query
                                    Statement stmt = con.createStatement();
                                    String query = "SELECT name, salary FROM staff WHERE ";
                                    query += argv[0];
                                    ResultSet rs = stmt.executeQuery(query);
                                    Employees SalReport;
                                    // turn a JDBC result set to an SQLJ interator using the CAST statement
                                    #sql [conCtx] SalReport = {
                                    <span class=boldcode><strong>CAST</strong></span> :rs };
                                    while (SalReport.next()) {
                                    System.out.println( SalReport.name() + " earns " + SalReport.salary() );
                                    }
                                    SalReport.close();
                                    stmt.close();
                                    }
                                    }
                                    </pre>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        <br></td>
                    </tr>
                </tbody>
            </table>
            </p>
            <p><a name=N1022A><span class=smalltitle><strong><font face=Arial>类型和模式检查 </font></strong></span></a></p>
            <p>SQLJ 与 Java 类似的一点是，它也是强类型的。在将 SQLJ 源文件转换成 Java 源文件时，SQLJ 转换程序会检查 SQLJ 语法。这类似于其它 DB2 预编译器。而且，在转换阶段的 Java 编译期间执行迭代器数据类型的转换。例如，在禁止使用双精度的迭代器列（如雇员工资）中，Java 编译器就会阻止该列使用双精度类型。因此， <code>String hv = employees.salary();</code> 这样的赋值在编译时就会生成一个错误。另外，在概要文件定制期间也执行联机检查，以便可以较早地捕获编程错误。 </p>
            <p>JDBC 不能在运行时之前进行语法或语义检查。如果存在语法或语义错误，那么在应用程序运行时任何此类异常都会产生。</p>
            <p><strong>注：</strong> </p>
            <ul>
                <li>在 V8.1 中，在概要文件定制期间执行联机检查，而在以前的发行版中这一操作是在转换阶段执行的。
                <li>有些 SQLJ 错误只有在运行时才被捕获到。另外，不能动态编译的语句不会进行联机检查。 </li>
            </ul>
            <br>
            <table cellSpacing=0 cellPadding=0 width="100%" border=0>
                <tbody>
                    <tr>
                        <td><img height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"><br><img height=6 alt="" src="http://www.ibm.com/i/c.gif" width=8 border=0></td>
                    </tr>
                </tbody>
            </table>
            <table class=no-print cellSpacing=0 cellPadding=0 align=right>
                <tbody>
                    <tr align=right>
                        <td><img height=4 alt="" src="http://www.ibm.com/i/c.gif" width="100%"><br>
                        <table cellSpacing=0 cellPadding=0 border=0>
                            <tbody>
                                <tr>
                                    <td vAlign=center><img height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><br></td>
                                    <td vAlign=top align=right><a class=fbox href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#main"><strong><font color=#996699>回页首</font></strong></a></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br><br>
            <p><a name=4><span class=atitle>差异汇总</span></a></p>
            <p><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#table1"><font color=#996699>表 1</font></a>汇总了 SQLJ 和 JDBC 之间的差异。 </p>
            <p><a name=table1>表 1. 比较 SQLJ 和 JDBC</a> <br>
            <table cellSpacing=1 cellPadding=5 width=500 border=1>
                <tbody>
                    <tr vAlign=top>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                        <td><strong>SQLJ</strong> </td>
                        <td><strong>JDBC</strong> </td>
                    </tr>
                    <tr vAlign=top>
                        <td><strong>标准</strong> </td>
                        <td>ISO/ANSI（不属于 J2EE）</td>
                        <td>Sun（属于 J2EE）</td>
                    </tr>
                    <tr vAlign=top>
                        <td><strong>SQL 规范级别</strong> </td>
                        <td>SQL-1999</td>
                        <td>N/A（必须至少支持 Entry Level SQL-92）</td>
                    </tr>
                    <tr vAlign=top>
                        <td><strong>安全性</strong> </td>
                        <td>强</td>
                        <td>一般</td>
                    </tr>
                    <tr vAlign=top>
                        <td><strong>性能</strong> </td>
                        <td>较快（在开发期间创建了静态存取方案）</td>
                        <td>较慢（在应用程序执行期间创建了动态存取方案）</td>
                    </tr>
                    <tr vAlign=top>
                        <td><strong>语法</strong> </td>
                        <td>高级（紧凑）</td>
                        <td>低级（繁琐）</td>
                    </tr>
                    <tr vAlign=top>
                        <td><strong>SQLJ 和 JDBC 互操作性</strong> </td>
                        <td>是</td>
                        <td>N/A</td>
                    </tr>
                    <tr vAlign=top>
                        <td><strong>类型和模式检查</strong> </td>
                        <td>强（在开发期间执行）</td>
                        <td>弱（在运行时期间执行）</td>
                    </tr>
                </tbody>
            </table>
            </p>
            <br>
            <table cellSpacing=0 cellPadding=0 width="100%" border=0>
                <tbody>
                    <tr>
                        <td><img height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"><br><img height=6 alt="" src="http://www.ibm.com/i/c.gif" width=8 border=0></td>
                    </tr>
                </tbody>
            </table>
            <table class=no-print cellSpacing=0 cellPadding=0 align=right>
                <tbody>
                    <tr align=right>
                        <td><img height=4 alt="" src="http://www.ibm.com/i/c.gif" width="100%"><br>
                        <table cellSpacing=0 cellPadding=0 border=0>
                            <tbody>
                                <tr>
                                    <td vAlign=center><img height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><br></td>
                                    <td vAlign=top align=right><a class=fbox href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#main"><strong><font color=#996699>回页首</font></strong></a></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br><br>
            <p><a name=5><span class=atitle>V8.1 中的新增功能</span></a></p>
            <p>DB2 UDB V8.1 提供了新设计的 SQLJ 驱动程序，它有几个新特性。新的 SQLJ 驱动程序基于一种称为 Distributed Relational Database Architecture（DRDA&#174;）的开放分布式协议。基于 CLI 的 JDBC 驱动程序（类型 2 和类型 3）以及 V8.1 中引入的新的通用 JDBC 驱动程序（类型 2 和类型 4）都支持该协议。</p>
            <p>SQLJ 的主要增强功能可以概括如下：</p>
            <ul>
                <li><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#utilities"><font color=#996699>新的 SQLJ 实用程序和运行时</font></a>
                <li><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#elim"><font color=#996699>消除了特定于平台的文件</font></a>
                <li><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#new"><font color=#996699>一些新特性</font></a> </li>
            </ul>
            <p><a name=N10311><span class=smalltitle><strong><font face=Arial>新的 SQLJ 实用程序和运行时 </font></strong></span></a></p>
            <p>DB2 UDB V8.1 中的 SQLJ 实现了纯 Java SQLJ 实用程序和运行时，并带有一些新选项和可选的格式。新的运行时性能比 V7 的性能好得多。</p>
            <p>在 V8.1 中，SQLJ 转换程序 sqlj 在缺省情况下总是编译所生成的 Java 源文件。在 V7 中，这个编译选项不能与某些 JDK 一起使用，因此您必须手工编译 Java 文件。</p>
            <p>V8.1 中新的概要文件打印程序 db2sqljprint 不再需要您提供 URL，而且它提供了有关要执行的 SQL 语句的详细信息，例如 DB2 语句的类型、节号以及 DB2 结果集元数据信息。</p>
            <p><a name=N10324><span class=smalltitle><strong><font face=Arial>消除了特定于平台的文件 </font></strong></span></a></p>
            <p>V8.1 中的 SQLJ 概要文件定制程序包含新的序列化概要文件格式，它不必使用 DBRM 文件和绑定文件（.bnd 文件）。新格式完全可以移植到所有平台。它包含所有 BIND 操作所需的所有信息，用户不必在目标系统（UNIX&#174;、Windows&#174;、OS/390&#174; 和 z/OS）上重新定制序列化概要文件就可以部署在任何服务器平台上。</p>
            <p><a name=N10331><span class=smalltitle><strong><font face=Arial>V8 中的新特性 </font></strong></span></a></p>
            <p>DB2 UDB V8.1 中添加的主要特性包括以下各项：</p>
            <ul>
                <li><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#jdbc"><font color=#996699>使用 DataSource 创建 SQLJ 连接上下文</font></a>
                <li><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#iterators"><font color=#996699>可滚动的迭代器</font></a>
                <li><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#batch"><font color=#996699>批处理更新</font></a> </li>
            </ul>
            <p><a name=N10356><span class=smalltitle><strong><font face=Arial>使用 DataSource 创建 SQLJ 连接上下文 </font></strong></span></a></p>
            <p>有了 V8.1 SQLJ，您可以使用 JDBC DataSource 接口来创建 SQLJ 连接。而且，缺省连接上下文的实现也更改了。要获得缺省连接上下文，SQLJ 运行时要执行 JNDI 查询以获得 jdbc/defaultDataSource。如果没有注册任何 jdbc/defaultDataSource，那么在驱动程序试图访问上下文时，就会抛出一个空上下文异常。因此，必须向 JNDI 注册 jdbc/defaultDataSource，或者通过调用 DefaultContext.setDefaultContext(userctxt) 来设置缺省上下文。</p>
            <p><strong>建议：</strong>在 SQLJ 子句中使用显式的连接上下文。 </p>
            <p><a name=listing4>清单 4. 使用数据源创建 SQLJ 连接上下文</a> <br>
            <table cellSpacing=1 cellPadding=5 width=500 border=1>
                <tbody>
                    <tr vAlign=top>
                        <td>
                        <table cellSpacing=0 cellPadding=0 width="100%" border=0>
                            <tbody>
                                <tr>
                                    <td class=code-outline>
                                    <pre class=displaycode>// Create connection context class Ctx with the new dataSource keyword
                                    #sql public static context Ctx with (dataSource="jdbc/sampledb");
                                    String userid, password;
                                    String empname;
                                    ?
                                    // Create connection context object conCtx for the connection to jdbc/sampledb
                                    Ctx conCtx = new Ctx(userid, password);
                                    #sql [conCtx] { SELECT lastname INTO :empname FROM emp WHERE empno = '000010' };
                                    ?
                                    conCtx.close();
                                    </pre>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        <br></td>
                    </tr>
                </tbody>
            </table>
            </p>
            <p><a name=N10384><span class=smalltitle><strong><font face=Arial>可滚动的迭代器 </font></strong></span></a></p>
            <p>可滚动的迭代器允许您向前移、向后移或移动到指定行。与 JDBC 中可滚动的游标相似，可滚动的迭代器可以是 <em>不敏感的</em>，也可以是 <em>敏感的</em>。 </p>
            <ul>
                <li>不敏感的迭代器意味着在迭代器打开后，它不能看到底层表中的更改。不敏感的迭代器是只读的。
                <li>敏感的迭代器意味着迭代器可以看到迭代器或其它处理对底层表所作的更改。例如， <a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#listing5"><font color=#996699>清单 5</font></a>中的代码演示了如何使用指定的迭代器对雇员表的所有行以逆序方式检索雇员号和雇员的姓氏。 </li>
            </ul>
            <p><a name=listing5>清单 5. 使用可滚动的迭代器</a> <br>
            <table cellSpacing=1 cellPadding=5 width=500 border=1>
                <tbody>
                    <tr vAlign=top>
                        <td>
                        <table cellSpacing=0 cellPadding=0 width="100%" border=0>
                            <tbody>
                                <tr>
                                    <td class=code-outline>
                                    <pre class=displaycode>//  Declare a scrollable iterator.
                                    #sql  iterator ScrollIter implements sqlj.runtime.Scrollable with (sensitivity = SENSITIVE)
                                    (String EmpNo, String LastName);
                                    {
                                    ScrollIter scrlIter;
                                    #sql [conCtx] scrlIter={ SELECT empno, lastname FROM emp };
                                    scrlIter.afterLast();
                                    while (scrlIter.previous())
                                    {
                                    System.out.println(scrlIter.EmpNo() + " " + scrlIter.LastName());
                                    }
                                    scrlIter.close();
                                    }
                                    </pre>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        <br></td>
                    </tr>
                </tbody>
            </table>
            </p>
            <p><a name=N103BF><span class=smalltitle><strong><font face=Arial>批处理更新 </font></strong></span></a></p>
            <p>批处理更新允许将语句集中到一起，随后将其以批处理方式发送到数据库，一次执行完这些语句。您可以在批处理更新中包含以下几种类型的语句：</p>
            <ul>
                <li>搜索到的 INSERT、UPDATE 或 DELETE 语句
                <li>CREATE、ALTER、DROP、GRANT 或 REVOKE 语句
                <li>只带输入参数的 CALL 语句 </li>
            </ul>
            <p>与 JDBC 不同，SQLJ 允许异构批处理，这些批处理中包含带有输入参数或主机表达式的语句。因此在同一个 SQLJ 语句批处理中可以包含同一语句的实例、不同语句的实例、带输入参数或主机表达式的语句的实例以及不带输入参数或主机表达式的语句的实例等。</p>
            <p><strong>建议：</strong>在关闭批处理或在结束使用 ExecutionContext（会使批处理打开）之前，要显式地调用 executeBatch()。这将确保执行经过批处理的所有语句。 </p>
            <p><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#listing6"><font color=#996699>清单 6</font></a>中的代码段向您显示了如何以批处理方式执行更新操作来给所有管理人员加薪。 </p>
            <p><a name=listing6>清单 6. 执行批处理更新</a> <br>
            <table cellSpacing=0 cellPadding=0 width="100%" border=0>
                <tbody>
                    <tr>
                        <td class=code-outline>
                        <pre class=displaycode>#sql iterator getMgr(String);
                        {
                        getMgr deptIter;
                        String mgrnum = null;
                        int raise = 400;
                        int currentSalary;
                        String url = null, username = null, password = null;
                        testContext conCtx = new testContext (url, username, password, false);
                        // Acquire execution context.
                        // All statements that execute in a batch must use this execution context.
                        ExecutionContext exeCtx = new ExecutionContext();
                        // Invoke ExecutionContext.setBatching (true) to create a batch.
                        exeCtx.setBatching(true);
                        #sql [conCtx] deptIter =  { SELECT mgrno FROM dept };
                        #sql {FETCH :deptIter INTO :mgrnum};
                        while (!deptIter.endFetch())
                        {
                        #sql [conCtx] {
                        SELECT SALARY INTO :currentSalary FROM emp WHERE empno = :mgrnum};
                        #sql [conCtx, exeCtx]
                        { UPDATE emp SET SALARY = :(currentSalary+raise) WHERE empno =:mgrnum };
                        #sql { FETCH :deptIter INTO :mgrnum };
                        }
                        exeCtx.executeBatch();
                        exeCtx.setBatching(false);
                        #sql [conCtx] {COMMIT};
                        deptIter.close();
                        exeCtx.close();
                        conCtx.close();
                        }
                        </pre>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br></p>
            <br>
            <table cellSpacing=0 cellPadding=0 width="100%" border=0>
                <tbody>
                    <tr>
                        <td><img height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"><br><img height=6 alt="" src="http://www.ibm.com/i/c.gif" width=8 border=0></td>
                    </tr>
                </tbody>
            </table>
            <table class=no-print cellSpacing=0 cellPadding=0 align=right>
                <tbody>
                    <tr align=right>
                        <td><img height=4 alt="" src="http://www.ibm.com/i/c.gif" width="100%"><br>
                        <table cellSpacing=0 cellPadding=0 border=0>
                            <tbody>
                                <tr>
                                    <td vAlign=center><img height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><br></td>
                                    <td vAlign=top align=right><a class=fbox href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#main"><strong><font color=#996699>回页首</font></strong></a></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br><br>
            <p><a name=6><span class=atitle>结束语</span></a></p>
            <p>相对于 JDBC，SQLJ 的 DB2 实现具有明显的优势。在预编译时 SQLJ 较简单语法和类型以及模式检查大大降低了开发成本。SQLJ 还具有在 SQLJ 应用程序中嵌入 JDBC 语句这样的灵活性。这意味着一个应用程序可以同时利用 SQLJ 和 JDBC 的优点。当安全性和性能对 Java 应用程序至关重要时，SQLJ 是正确的选择。</p>
            <br>
            <table cellSpacing=0 cellPadding=0 width="100%" border=0>
                <tbody>
                    <tr>
                        <td><img height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"><br><img height=6 alt="" src="http://www.ibm.com/i/c.gif" width=8 border=0></td>
                    </tr>
                </tbody>
            </table>
            <table class=no-print cellSpacing=0 cellPadding=0 align=right>
                <tbody>
                    <tr align=right>
                        <td><img height=4 alt="" src="http://www.ibm.com/i/c.gif" width="100%"><br>
                        <table cellSpacing=0 cellPadding=0 border=0>
                            <tbody>
                                <tr>
                                    <td vAlign=center><img height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0><br></td>
                                    <td vAlign=top align=right><a class=fbox href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html#main"><strong><font color=#996699>回页首</font></strong></a></td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br><br>
            <p><a name=7><span class=atitle>更多信息</span></a></p>
            <ul>
                <li>Meet the Experts: John Campbell on Java Performance for DB2 Applications <br><a href="http://www7b.software.ibm.com/dmdd/library/techarticle/0202campbell/0202campbell.html"><font color=#5c81a7>http://www7b.software.ibm.com/dmdd/library/techarticle/0202campbell/0202campbell.html</font></a>
                <li>DB2 Universal Database V8 Application Development <br><a href="http://www.ibm.com/software/data/db2/udb/ad/v8/java"><font color=#5c81a7>http://www.ibm.com/software/data/db2/udb/ad/v8/java</font></a>
                <li>使用 DB2 版本 8 开发企业 Java 应用程序 <br><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0209hutchison/index.html"><font color=#5c81a7>http://www.ibm.com/developerworks/cn/dmdd/library/techarticles/0209hutchison/index.shtml</font></a>
                <li>DB2 V8 信息中心 <br><a href="http://www.ibm.com/cgi-bin/db2www/data/db2/udb/winos2unix/support/index.d2w/report"><font color=#5c81a7>http://www.ibm.com/cgi-bin/db2www/data/db2/udb/winos2unix/support/index.d2w/report</font></a>
                <li>在使用 SQLJ 和 JDBC 时获取最优的 DB2 性能（教程） <br><a href="http://www.ibm.com/developerworks/cn/views/db2/tutorials.jsp?cv_doc_id=85232"><font color=#5c81a7>http://www.ibm.com/developerWorks/cn/cndmdd.nsf/dmdd-onlinecourse-bynewest/9607CCEEE2E42D09C8256D18002359F4?OpenDocument </font></a>
                <li>IBM Java 技术专区 <br><a href="http://www.ibm.com/java/"><font color=#5c81a7>http://www.ibm.com/java/</font></a>
                <li>DB2 开发者园地 Java 专区 <br><a href="http://www.ibm.com/software/data/db2/java"><font color=#5c81a7>http://www.ibm.com/software/data/db2/java</font></a>
                <li>SQL/OLB 标准 <br><a href="http://www.ansi.org/"><font color=#5c81a7>http://www.ansi.org/</font></a> </li>
            </ul>
            <br><br>
            <p><a name=author><span class=atitle>关于作者</span></a></p>
            <table cellSpacing=0 cellPadding=0 width="100%" border=0>
                <tbody>
                    <tr>
                        <td colSpan=3><img height=5 alt="" src="http://www.ibm.com/i/c.gif" width="100%"></td>
                    </tr>
                    <tr vAlign=top align=left>
                        <td>
                        <p><img height=107 alt="照片：Connie Tsui" hspace=10 src="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/images/ConnieTsui_sm.jpg" width=120 align=left></p>
                        </td>
                        <td><img height=5 alt="" src="http://www.ibm.com/i/c.gif" width=4></td>
                        <td width="100%">
                        <p><strong>Connie Tsui</strong>是 IBM 多伦多实验室 DB2 解决方案集成团队的专职软件分析师。她从多伦多大学（University of Toronto）获得了计算机科学学士学位。她目前主要从事 DB2 和 WebSphere&#174; 的集成。 </p>
                        </td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<img src ="http://www.blogjava.net/dyerac/aggbug/129147.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/dyerac/" target="_blank">dyerac in java...</a> 2007-07-09 21:54 <a href="http://www.blogjava.net/dyerac/articles/129147.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用 SQLJ 开发应用程序</title><link>http://www.blogjava.net/dyerac/articles/129143.html</link><dc:creator>dyerac in java...</dc:creator><author>dyerac in java...</author><pubDate>Mon, 09 Jul 2007 13:51:00 GMT</pubDate><guid>http://www.blogjava.net/dyerac/articles/129143.html</guid><wfw:comment>http://www.blogjava.net/dyerac/comments/129143.html</wfw:comment><comments>http://www.blogjava.net/dyerac/articles/129143.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/dyerac/comments/commentRss/129143.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/dyerac/services/trackbacks/129143.html</trackback:ping><description><![CDATA[&nbsp;
<p style="FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: arial; mso-outline-level: 1">使用 SQLJ 开发应用程序</p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 9pt; MARGIN: 0in; COLOR: #666666; FONT-FAMILY: verdana; mso-outline-level: 1">用 DB2 UDB V8.1 和 Application Developer V5.1.2 创建使用 SQLJ 的 Java 和 J2EE 应用程序</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=18 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image002.gif" width=192></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: white; FONT-FAMILY: Arial; mso-outline-level: 1">文档选项</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image003.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="javascript:document.email.submit();"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>将此页作为电子邮件发送</u></span></a></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><u><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></u></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><u><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></u></p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: #ff3300; FONT-FAMILY: Verdana; mso-outline-level: 1">未显示需要 JavaScript 的文档选项</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image004.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0412cline/index.html#download"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>样例代码</u></span></a></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">级别: 初级</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0412cline/index.html#author"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>Owen Cline</u></span></a><span style="COLOR: black; FONT-FAMILY: Arial"> (</span><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#111;&#119;&#101;&#110;&#99;&#64;&#117;&#115;&#46;&#105;&#98;&#109;&#46;&#99;&#111;&#109;&#63;&#115;&#117;&#98;&#106;&#101;&#99;&#116;&#61;&#20351;&#29992;&#37;&#50;&#48;&#83;&#81;&#76;&#74;&#37;&#50;&#48;&#24320;&#21457;&#24212;&#29992;&#31243;&#24207;"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>owenc@us.ibm.com</u></span></a><span style="COLOR: black; FONT-FAMILY: Arial">), 认证的 IT 咨询专家 - WebSphere 软件服务, IBM</span></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">2005 年 2 月 01 日</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 2">这篇 "how-to" 文章将解释如何在 Java&#8482; 应用程序和 J2EE&#8482; 应用程序中使用 SQLJ。文中将介绍 SQLJ 语法，使用 SQLJ 访问数据源，建立 WebSphere&#174; Application Developer 项目以支持 SQLJ，以及创建和调用 DB2&#174; 存储过程。</p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">SQLJ 概述</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">SQL 语句包括查询（SELECT）、插入（INSERT）、更改（UPDATE）、删除（DELETE）语句以及其他对存储在关系数据库表中的数据进行操作的语句。SQLJ 是在 Java 中嵌入这些 SQL 语句的一种约定，它是以某种允许编程工具对 Java 中 SQL 进行编译时分析的方式实现嵌入的，在编译时会对 SQL 语句进行语法检查以保证语法的正确性，进行类型检查以确定 Java 和 SQL 之间交换的数据具有兼容的类型和适当的类型转换，以及进行模式检查以确保 SQL 结构在它们执行时所在的数据库模式中是格式良好的、合法的。嵌入式 SQL 语句被称为是 "静态的（static）"，因为这些语句在 Java 程序中一目了然，因而嵌入式 SQL 语句随着包含它们的 Java 程序的编译而编译（通常的术语是"预编译"）。我们提议将 SQLJ 作为用于紧密集成 Java/SQL 程序的一种方便有效的标准。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 11.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: arial; mso-outline-level: 1">SQLJ 简史</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"><span style="COLOR: black">正如 Connie Tsui 在她的</span><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html"><span style="COLOR: blue"><u>文章</u></span></a><span style="COLOR: black">（后面有引用）中解释的那样，一个非正式的、开放的数据库供应商组织在 1997 年 4 月开始定期的会面，以便交换关于如何在 Java 编程语言中使用静态 SQL 语句和结构的思想。主要参与者包括 IBM&#174;、Oracle、Compaq、Informix&#174;、Sybase、Cloudscape&#8482; 和 Sun Microsystems。该组织将他们为之努力的规范命名为 </span><span style="COLOR: black; FONT-STYLE: italic">JSQL</span><span style="COLOR: black">。在发现 JSQL 已经是商标术语后，JSQL 被更名为 </span><span style="COLOR: black; FONT-STYLE: italic">SQLJ</span><span style="COLOR: black">。在 1997 年 12 月，Oracle 向其他成员提供了 Java 中嵌入式 SQL 的一个参考实现。该参考实现可以在任何支持 JDK 1.1 的平台上运行，并且是与供应商无关的。在 1998 年 12 月，用于在 Java 中嵌入 SQL 的规范被完全开发出来，并且被接受为 ANSI 标准 Database Language —— SQL, Part 10 Object Language Bindings (SQL/OLB) ANSI x3.135.10-1998。该规范已经被当作 SQLJ 规范的 Part 0 供大家引用。如今它被称作 SQL/OLB (Object Language Bindings)。 </span></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 11.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: arial; mso-outline-level: 1">SQLJ 与 JDBC 的比较</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">SQLJ —— Java 数据库应用程序的开门咒语</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"><span style="COLOR: black">SQLJ 标准现在为 Java 应用程序提供了基于 SQL 的数据库访问。</span><a href="http://www.javaworld.com/javaworld/jw-05-1999/jw-05-sqlj_p.html"><span style="COLOR: blue"><u>SQLJ ——</u></span></a><a href="http://www.javaworld.com/javaworld/jw-05-1999/jw-05-sqlj_p.html"><span style="COLOR: blue"><u> </u></span></a><a href="http://www.javaworld.com/javaworld/jw-05-1999/jw-05-sqlj_p.html"><span style="COLOR: blue"><u>Java 数据库应用程序的开门咒语</u></span></a><span style="COLOR: black"> </span></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">下面是 SQLJ 相对于直接在 JDBC 中编写代码的优势： </p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">SQLJ 程序需要的代码行数比 JDBC 程序更少。SQL 程序更简短，因而更易于调试。SQLJ 可以通过在编译时使用数据库连接，对代码执行语法和语义上的检查。SQLJ 提供了对查询结果及其他返回参数的强类型检查，而 JDBC 值则是直接从 SQL 返回，在编译时未进行任何检查。</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">SQLJ 为处理 SQL 语句提供了一种简化的方式。现在无需编写不同的方法调用来绑定每个输入参数和获取每个选择列表项，而只需编写一条使用 Java 宿主变量的 SQL 语句。SQLJ 会替您完成绑定。</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">然而，JDBC 提供了对 SQL 语句的执行的细粒度控制，并提供了真正的动态 SQL 能力。如果应用程序需要动态能力（在运行时发现数据库或实例元数据），那么应该使用 JDBC。 </p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0412cline/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">为 SQLJ 建立 WebSphere Studio Application Developer 项目</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">如果您计划将 SQLJ 代码包括到所有 WebSphere Studio Application Developer (Application Developer) 项目中，那么需要使您的项目支持 SQLJ。为此，可以右击项目并选择 <span style="FONT-WEIGHT: bold">Add SQLJ Support</span>。这将弹出 SQLJ 向导，如下所示。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 1. 使 Application Developer 项目支持 SQLJ</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=338 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image007.gif" width=438></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0412cline/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">SQLJ 和 Java 应用程序</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">从 Application Developer 主菜单中，选择 <span style="FONT-WEIGHT: bold">File -&gt; New -&gt; Other </span>，然后选择 <span style="FONT-WEIGHT: bold">Java </span>和 <span style="FONT-WEIGHT: bold">Java Project</span>。单击 <span style="FONT-WEIGHT: bold">Next</span>。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 2. 创建 Java 项目</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=346 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image008.gif" width=500></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">输入项目名，单击 <span style="FONT-WEIGHT: bold">Next</span>。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 3. 输入 Java 项目名</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=273 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image009.gif" width=500></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">选择 <span style="FONT-WEIGHT: bold">Finish</span> 创建该项目。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 4. 输入 Java 项目设置</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=384 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image010.gif" width=491></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; mso-outline-level: 1"><span style="FONT-FAMILY: Arial">为了让 Java 应用程序能够成功运行，还需添加 DB2 JDBC 驱动程序 </span><span style="FONT-FAMILY: courier">db2java.zip</span><span style="FONT-FAMILY: Arial">。右击 Java 项目，选择 </span><span style="FONT-WEIGHT: bold; FONT-FAMILY: Arial">Properties</span><span style="FONT-FAMILY: Arial">。 </span></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 5. 编辑 Java 项目属性</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=384 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image011.gif" width=500></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">接下来，选择 <span style="FONT-WEIGHT: bold">Java Build path</span>，然后选择 <span style="FONT-WEIGHT: bold">Libraries </span>标签页。然后选择 <span style="FONT-WEIGHT: bold">Add External JARs</span>。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 6. 添加外部 JAR —— db2java.zip</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=347 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image012.gif" width=563></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">完成这些设置后，库路径应该如下所示： </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 7. 添加外部 JAR —— db2java.zip</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=384 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image013.gif" width=509></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">接下来的步骤是创建有关 Java 类，用于容纳主方法。为此，需要右击 Java 项目，然后选择 <span style="FONT-WEIGHT: bold">New -&gt; Class</span>。填入包名和类名。另外，确保勾选了关于创建主方法的复选框。单击 <span style="FONT-WEIGHT: bold">Finish</span>。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 8. 创建 Java 主类</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=384 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image014.gif" width=373></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">最后，需要创建 SQLJ 文件。从 Application Developer 主菜单中，选择 <span style="FONT-WEIGHT: bold">File -&gt; New -&gt; Other</span>，然后选择 <span style="FONT-WEIGHT: bold">Data -&gt; SQLJ</span> 和 <span style="FONT-WEIGHT: bold">SQLJ File</span>。最后单击 <span style="FONT-WEIGHT: bold">Next</span>。Application Developer 将保证 SQLJ 文件被转换成一个 Java 文件。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 9. 选择 SQLJ 文件向导</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=384 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image015.gif" width=384></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">填入包名和文件名。然后单击 <span style="FONT-WEIGHT: bold">Finish</span>。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 10. 创建 SQLJ 文件</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=310 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image016.gif" width=500></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">至此，还需要将代码添加到 Java 主方法中，另外必须创建一个 SQLJ 方法。首先让我们明确要完成什么。我们将创建一个 SQLJ 方法，该方法从 DB2 SAMPLE 数据库中根据给定的雇员号读取一个雇员记录。这个 SQLJ 方法以雇员号（一个 Java String）作为输入，并将其作为参数传递给一条 SQLJ Select 语句。Java 主方法将调用这个 SQLJ 方法。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">清单 1. 包含主方法的 Java 类</p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">&lt;![CDATA[<br>/*<br>* Created on Nov 27, 2004<br>*<br>*/<br>package com.ibm.sqlj.main;<br>import com.ibm.sqlj.Select;<br>/**<br>* @author Owen Cline<br>*<br>*/<br>public class SQLJJave {<br>public static void main(String[] args) {<br>Select select = new Select();<br>select.selectEmployee("000110");<br>}<br>}<br>]]&gt;<br></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">清单 2. 包含 SQLJ 方法的 SQLJ 类</p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">&lt;![CDATA[<br>/*<br>* Created on Nov 27, 2004<br>*<br>*/<br>package com.ibm.sqlj;<br>import java.sql.*;<br>import sqlj.runtime.ref.*;<br>/**<br>* @author Owen Cline<br>*<br>*/<br>public class Select {<br>// First, load the JDBC driver<br>static<br>{ try<br>{ Class.forName ("COM.ibm.db2.jdbc.app.DB2Driver").newInstance ();<br>} catch (Exception e)<br>{ System.out.println ("\n Error loading DB2 Driver...\n");<br>System.out.println (e);<br>System.exit(1);<br>}<br>}<br>public void selectEmployee(String empNo) {<br>Connection con = null; DefaultContext ctx = null; try {<br>String firstName = null;<br>String lastName = null;<br>// use the DB2 SAMPLE database<br>String url = "jdbc:db2:SAMPLE";<br>// Get the connection<br>con = DriverManager.getConnection(url); // Set the default context<br>ctx = new DefaultContext(con); DefaultContext.setDefaultContext(ctx);<br>// Lookup the employee given the employee number<br>#sql { SELECT FIRSTNME, LASTNAME INTO :firstName, :lastName<br>FROM EMPLOYEE<br>WHERE EMPNO = :empNo } ;<br>System.out.println ("Employee " + firstName + " " + lastName);<br>ctx.close();<br>con.close();<br>}<br>catch( Exception e )<br>{<br>System.out.println (e);<br>}<br>}<br>}<br>]]&gt;<br></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">现在便可以运行这个 Java 应用程序了。选中您的 Java 项目，然后从 Application Developer 主菜单中选择 <span style="FONT-WEIGHT: bold">Run -&gt; Run As -&gt; Java Application</span>。您应该可以看到在控制台 "Employee VINCENZO LUCCHESSI" 中打印出如下消息。 </p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0412cline/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">SQLJ 和 J2EE 应用程序</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">您可能想到，还可以在 J2EE 应用程序、servlet、会话 bean、BMP 实体 bean 和 MDB bean 中使用 SQLJ。现在我们要做的就是将我们创建的在 Java 应用程序中运行的上述代码移植，使之在一个会话 bean 中运行。 </p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">首先，需要创建一个 EJB 项目。从 Application Developer 主菜单中，选择 <span style="FONT-WEIGHT: bold">File- &gt; New -&gt; Project</span>。 然后选择 <span style="FONT-WEIGHT: bold">EJB </span>and <span style="FONT-WEIGHT: bold">EJB Project</span>。单击 <span style="FONT-WEIGHT: bold">Next</span>。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 11. 创建 EJB Project</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=337 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image017.gif" width=500></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">确保选择了 <span style="FONT-WEIGHT: bold">Create 2.0 EJB Project </span>，然后单击 <span style="FONT-WEIGHT: bold">Next</span>。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 12. 选择一个 EJB 版本</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=303 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image018.gif" width=500></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">现在，输入 <span style="FONT-WEIGHT: bold">SQLJSession</span> 作为 EJB 项目名。EAR 项目应该是 <span style="FONT-WEIGHT: bold">DefaultEAR</span>。单击 <span style="FONT-WEIGHT: bold">Next</span>。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 13. 输入 EJB 项目名</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=289 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image019.gif" width=500></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">在 Module Dependencies 对话框中，选择 <span style="FONT-WEIGHT: bold">Finish </span>创建 EJB 项目和 EAR 项目。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 14. 输入 EJB 模块依赖</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=359 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image020.gif" width=500></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">在这里，我们还想创建一个会话 Bean。 选择 Application Developer 主菜单中的 <span style="FONT-WEIGHT: bold">File -&gt; New -&gt; Enterprise Bean</span>。然后单击 <span style="FONT-WEIGHT: bold">Next</span>。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 15. 创建一个会话 Bean —— 步骤 1</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=230 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image021.gif" width=500></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">确保选择了 Session bean 单选按钮，并输入了一个 Bean 名称。然后单击 <span style="FONT-WEIGHT: bold">Next</span>。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 16. 创建一个会话 Bean —— 步骤 2</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=384 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image022.gif" width=501></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">确保选中 <span style="FONT-WEIGHT: bold">Local client view</span> 复选框，以得到本地接口（如果 EJB 容器部署在本地，那么这样做会更快一些），然后单击 <span style="FONT-WEIGHT: bold">Finish </span>创建会话 Bean。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 17. 创建一个会话 Bean - 步骤 3</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=384 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image023.gif" width=384></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">现在我们来创建一个 SQLJ 文件，该文件将包含我们的业务逻辑。右击 EJB 项目 <span style="FONT-WEIGHT: bold">SQLJSession </span>并选择 <span style="FONT-WEIGHT: bold">New -&gt; Other</span>，然后选择 <span style="FONT-WEIGHT: bold">Data -&gt; SQLJ</span> 和 <span style="FONT-WEIGHT: bold">SQLJ File</span>。最后单击 <span style="FONT-WEIGHT: bold">Next</span>。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 18. 创建 SQLJ 文件 —— 步骤 1</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=384 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image024.gif" width=414></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">输入包名和文件名，然后选择 <span style="FONT-WEIGHT: bold">Finish </span>创建 SQLJ 文件。还应注意的是，由于是在一个项目中创建这个 SQLJ 文件，而这个项目还没有添加 SQLJ 支持，因此这一次需添加该支持。 </p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">接着，使用上述步骤创建第二个 SQLJ 文件，文件名为 <span style="FONT-WEIGHT: bold">SessionBeanSelectUsingDefaultDatasource</span>，以便以后演示如何使用 defaultDataSource。现在，我们可以在创建的 SQLJ 文件中添加自己的业务逻辑，如下所示： </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 19. 创建 SQLJ File —— 步骤 2</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=380 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image025.gif" width=500></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">清单 3. SessionBeanSelect.sqlj</p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">&lt;![CDATA[<br>package com.ibm.sqlj;<br>import java.sql.*;<br>import sqlj.runtime.ref.*;<br>import javax.sql.DataSource;<br>/**<br>* @author Owen Cline<br>*<br>*/<br>public class SessionBeanSelect {<br>// Setup datasource to use. Notice that I am not using a global JNDI name // but instead using a Resource Reference which points to the global JNDI name. // This is a best practice.<br>#sql public static context Ctx with (dataSource="java:comp/env/sqljDS");<br>public void selectEmployee(String empNo) {<br>String firstName = null;<br>String lastName = null;<br>try {<br>// Create context<br>Ctx conCtx = new Ctx();<br>// Lookup the employee given the employee number<br>#sql [conCtx] { SELECT FIRSTNME, LASTNAME INTO :firstName, :lastName<br>FROM EMPLOYEE<br>WHERE EMPNO = :empNo } ;<br>System.out.println ("SessionBeanSelect-Employee " + firstName + " " + lastName);<br>conCtx.close();<br>} catch (Exception e) {<br>System.out.println(e);<br>}<br>}<br>}<br>]]&gt;<br></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">清单 4. SessionBeanSelectUsingDefaultDatasource.sqlj</p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">&lt;![CDATA[<br>package com.ibm.sqlj;<br>import java.sql.*;<br>import sqlj.runtime.ref.*;<br>import javax.sql.DataSource;<br>/**<br>* @author Owen Cline<br>*<br>*/<br>public class SessionBeanSelectUsingDefaultDatasource {<br>public void selectEmployee(String empNo) {<br>String firstName = null;<br>String lastName = null;<br>try {<br>// Lookup the employee given the employee number<br>#sql { SELECT FIRSTNME, LASTNAME INTO :firstName, :lastName<br>FROM EMPLOYEE<br>WHERE EMPNO = :empNo } ;<br>System.out.println ("SessionBeanSelectUsingDefaultDatasource-Employee " + firstName + " " + lastName);<br>} catch (Exception e) {<br>System.out.println(e);<br>}<br>}<br>}<br>]]&gt;<br></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">现在，我们要添加 bean 模块，以便调用 SQLJ 文件中的业务逻辑。编辑 SQLJSessionBeanBean.java 文件，添加两个方法：selectEmployee 和 selectEmployeeUsingDefaultDatasource，如以下清单所示。然后，别忘了使这两个方法成为本地接口。最后，在进入下一步之前，确保生成了部署和 RMIC 代码。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">清单 4. SQLJSessionBeanBean.java</p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">&lt;![CDATA[<br>package com.ibm.sqlj.ejb.session;<br>import com.ibm.sqlj.SessionBeanSelect;<br>import com.ibm.sqlj.SessionBeanSelectUsingDefaultDatasource;<br>/**<br>* Bean implementation class for Enterprise Bean: SQLJSessionBean<br>*/<br>public class SQLJSessionBeanBean implements javax.ejb.SessionBean {<br>private javax.ejb.SessionContext mySessionCtx;<br>/**<br>* getSessionContext<br>*/<br>public javax.ejb.SessionContext getSessionContext() {<br>return mySessionCtx;<br>}<br>/**<br>* setSessionContext<br>*/<br>public void setSessionContext(javax.ejb.SessionContext ctx) {<br>mySessionCtx = ctx;<br>}<br>/**<br>* ejbCreate<br>*/<br>public void ejbCreate() throws javax.ejb.CreateException {<br>}<br>/**<br>* ejbActivate<br>*/<br>public void ejbActivate() {<br>}<br>/**<br>* ejbPassivate<br>*/<br>public void ejbPassivate() {<br>}<br>/**<br>* ejbRemove<br>*/<br>public void ejbRemove() {<br>}<br>/**<br>* selectEmployee<br>*/<br>public void selectEmployee(String empNo) {<br>SessionBeanSelect sbs = new SessionBeanSelect();<br>sbs.selectEmployee(empNo);<br>}<br>/**<br>* selectEmployee<br>*/<br>public void selectEmployeeUsingDefaultDatasource(String empNo) {<br>SessionBeanSelectUsingDefaultDatasource sbs = new SessionBeanSelectUsingDefaultDatasource();<br>sbs.selectEmployee(empNo);<br>}<br>}<br>]]&gt;<br></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">为最终测试我们的会话 Bean，必须创建一个 Server Configuration。切换到 Server Perspective，并在 <span style="FONT-WEIGHT: bold">Server Configuration</span> 视图中单击右键。选择 <span style="FONT-WEIGHT: bold">New -&gt; Server and Server Configuration</span>。输入服务器名 <span style="FONT-WEIGHT: bold">TestServer </span>，然后单击 <span style="FONT-WEIGHT: bold">Finish </span>创建该服务器。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 20. 创建一个 Server Configuration</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=384 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image026.gif" width=422></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">创建好服务器后，在服务器上单击右键（同样也是在 "Server Configuration" 视图中），然后选择 <span style="FONT-WEIGHT: bold">Add and Remove Projects</span>。 在 "Add and Remove Projects" 对话框中（这里没有显示出来），添加 <span style="FONT-WEIGHT: bold">DefaultEAR </span>项目并单击 <span style="FONT-WEIGHT: bold">Finish</span>。</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">最后要做的是创建我们的会话 Bean 将要使用的 DB2 数据源。在 Server Configuration 视图中右击 <span style="FONT-WEIGHT: bold">Test Server</span>，这将弹出服务器配置编辑器。选择 <span style="FONT-WEIGHT: bold">Security </span>标签页，然后选择下面显示的 JAAS Authentication Entries 对话框旁边的 <span style="FONT-WEIGHT: bold">Add </span>按钮。为该条目输入一个名称，然后输入一个用于访问数据库的合法用户名和密码。最后单击 <span style="FONT-WEIGHT: bold">OK</span>。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 21. 添加 JAAS 认证条目</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=193 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image027.gif" width=299></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">现在，选择 <span style="FONT-WEIGHT: bold">Data source</span> 标签页。在 JDBC Provider 列表中选择 <span style="FONT-WEIGHT: bold">Default DB2 JDBC Provider</span> 条目。单击在上面所选 JDBC 提供者中定义的数据源旁边的 <span style="FONT-WEIGHT: bold">Add </span>按钮。确保选中了 <span style="FONT-WEIGHT: bold">DB2 JDBC Provider</span> 和 <span style="FONT-WEIGHT: bold">Version 5.0 data source</span> 单选按钮。然后单击 <span style="FONT-WEIGHT: bold">Next</span>。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 22. 选择要创建的数据源的类型</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=341 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image028.gif" width=438></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">输入 <span style="FONT-WEIGHT: bold">sqljDS </span>作为数据源名称，输入 <span style="FONT-WEIGHT: bold">jdbc/sqljDS</span> 作为 JNDI 名称。对于 "Component-managed authentication alias" 和 "Container-managed authentication alias"，都选择前面创建的 <span style="FONT-WEIGHT: bold">JAAS Authentication Entry</span>。然后单击 <span style="FONT-WEIGHT: bold">Finish</span>。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 23. 输入 sqljDS 的数据源参数</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=384 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image029.gif" width=375></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">重复这些步骤，创建另一个名为 <span style="FONT-WEIGHT: bold">defaultDataSource </span>的数据源，数据源的 JNDI 名称为 <span style="FONT-WEIGHT: bold">jdbc/defaultDataSource</span>。最后，保存该服务器配置。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 24. 输入 defaultDataSource 的数据源参数</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=384 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image030.gif" width=378></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">现在，我们需要创建一个 Resource Reference，以便会话 bean 可以查找 sqljDS 数据源。因此，切换到 Application Developer 中的 J2EE Perspective。在 J2EE Hierarchy 视图中，右击 SQLJSession EJB 项目，以调用 EJB Deployment Descriptor 编辑器。选择 <span style="FONT-WEIGHT: bold">References </span>标签页。选择 <span style="FONT-WEIGHT: bold">SQLJSessionBean</span> 并选择 <span style="FONT-WEIGHT: bold">Add </span>按钮。选择 <span style="FONT-WEIGHT: bold">Resource Reference</span> 单选按钮，然后单击 <span style="FONT-WEIGHT: bold">Next</span>。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 25. 创建资源引用</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=384 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image031.gif" width=426></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">输入 <span style="FONT-WEIGHT: bold">sqljDS </span>作为名称。选择 <span style="FONT-WEIGHT: bold">javax.sql.DataSource</span> 作为类型。为 Authentication 选择 <span style="FONT-WEIGHT: bold">Container </span>。 保留 Sharing Scope 为 <span style="FONT-WEIGHT: bold">Shareable</span>。然后单击 <span style="FONT-WEIGHT: bold">Finish</span>。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 26. 输入资源引用参数</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=326 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image032.gif" width=438></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">选中刚创建的资源引用，然后输入 <span style="FONT-WEIGHT: bold">jdbc/sqljDS</span> 作为 JNDI 名。保存该 EJB 部署描述文件。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 27. 输入资源引用的 JNDI 名</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=384 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image033.gif" width=547></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">至此，您应该可以测试会话 Bean 模块了。首先，在 Server Perspective 中，通过在 Servers 视图中右键单击 TestServer 并选择 <span style="FONT-WEIGHT: bold">Start</span> 来启动该服务器。接着，通过在 Servers 视图中单击右键并选择 <span style="FONT-WEIGHT: bold">Run universal test client</span> 调用 Universal Test Client。从这个 universal test client 中，选择 JNDI Explorer 并展开 "jdbc" 菜单，以显示出两个数据源：sqljDS 和 defaultDataSource。接下来，完全展开 Local EJB beans 菜单，直到能够选择 <span style="FONT-WEIGHT: bold">SQLJSessionBeanLocalHome</span>。在 EJB reference 菜单中完全展开菜单项，直到能够选择 <span style="FONT-WEIGHT: bold">SQLJSessionBeanLocal.create()</span> 模块，然后选择 <span style="FONT-WEIGHT: bold">Invoke followed by the Work with Object</span> 按钮。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 28. 调用 SQLJSessionBeanLocal.create() 方法</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=382 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image034.gif" width=576></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">现在，您将看到有两个方法可供调用：selectEmployee 和 selectEmployeeUsingDefaultDatasource。选择 <span style="FONT-WEIGHT: bold">selectEmployee</span> 方法。输入 <span style="FONT-WEIGHT: bold">000110 </span>作为 empNo 值，并选择 <span style="FONT-WEIGHT: bold">Invoke </span>按钮。检查控制台窗口，以确信显示了 "[11/28/04 16:56:05:176 PST] 6d2f338b SystemOut O SessionBeanSelect-Employee VINCENZO LUCCHESSI"。最后，您自己尝试一下 selectEmployeeUsingDefaultDatasource 方法。 </p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 29. 调用 selectEmployee() 方法</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=384 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image035.gif" width=573></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0412cline/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">结束语</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">总之，对于不需要动态 SQL 的 Java 和 J2EE 应用程序，SQLJ 是提供持久性框架的一种非常有用的方式。虽然本文没有提到，但使用 SQLJ 开发存储过程也是可行的。 </p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0412cline/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">下载</p>
<p style="MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1"><span style="FONT-SIZE: 9pt">描述</span><span style="FONT-SIZE: 10pt"> </span><span style="FONT-SIZE: 9pt">名字</span><span style="FONT-SIZE: 10pt"> </span><span style="FONT-SIZE: 9pt">大小</span><span style="FONT-SIZE: 10pt"> </span><span style="FONT-SIZE: 9pt">下载方法</span></p>
<p style="MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1"><span style="FONT-SIZE: 9pt">code samples</span><span style="FONT-SIZE: 10pt"> </span></p>
<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">SQLJArticle_sourcecode.zip</p>
<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">3443 KB</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: 宋体; mso-outline-level: 1">&nbsp;</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><a href="ftp://ftp.software.ibm.com/software/dw/dm/db2/dm-0412cline/SQLJArticle_sourcecode.zip"><span style="FONT-WEIGHT: bold; FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>FTP</u></span></a></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><u><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></u></p>
<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">|</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0412cline/SQLJArticle_sourcecode.zip"><span style="FONT-WEIGHT: bold; FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>HTTP</u></span></a></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><u><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></u></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><u><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image036.gif" width=16></u></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/whichmethod.html"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>关于下载方法的信息</u></span></a></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><u><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></u></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><u><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image037.gif" width=16></u></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.adobe.com/products/acrobat/readstep2.html"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>Get Adobe&#174;</u></span></a><a href="http://www.adobe.com/products/acrobat/readstep2.html"><span style="COLOR: blue; FONT-FAMILY: Arial"><u> </u></span></a><a href="http://www.adobe.com/products/acrobat/readstep2.html"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>Reader&#174;</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">参考资料 </p>
<ul style="MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in; MARGIN-LEFT: 1in; DIRECTION: ltr; unicode-bidi: embed" type=disc>
    <li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 2"><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">您可以参阅本文在 developerWorks 全球站点上的 </span><a href="http://www.ibm.com/developerworks/db2/library/techarticle/dm-0412cline/?S_TACT=105AGX52&amp;S_CMP=cn-a-db2"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>英文原文</u></span></a><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial"> 。</span></li>
    <li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 2"><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">下载本文中用到的</span><a href="http://www3.software.ibm.com/ibmdl/pub/software/dw/dm/db2/dm-0412cline/SQLJArticle_sourcecode.zip"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>源代码</u></span></a><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">。</span></li>
    <li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 2"><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">"</span><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>考虑将</u></span></a><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u> </u></span></a><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/0302tsui/0302tsui.html"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>SQLJ 用于 DB2 V8 Java 应用程序</u></span></a><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">" (</span><span style="FONT-SIZE: 9pt; COLOR: black; FONT-STYLE: italic; FONT-FAMILY: Arial">developerWorks</span><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">, February 2003) </span></li>
    <li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 2"><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">"</span><a href="http://www.webagesolutions.com/knowledgebase/waskb/waskb006/"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>Building DB2</u></span></a><a href="http://www.webagesolutions.com/knowledgebase/waskb/waskb006/"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u> </u></span></a><a href="http://www.webagesolutions.com/knowledgebase/waskb/waskb006/"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>Stored Procedure Using WSAD V5</u></span></a><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">" (Bibhas Bhattacharya) </span></li>
    <li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 2"><a href="http://www.redbooks.ibm.com/abstracts/sg245945.html?Open"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>DB2 Java</u></span></a><a href="http://www.redbooks.ibm.com/abstracts/sg245945.html?Open"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u> </u></span></a><a href="http://www.redbooks.ibm.com/abstracts/sg245945.html?Open"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>Stored Procedures Learning by Example</u></span></a><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial"> (IBM Redbook, September 2000) </span></li>
</ul>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">关于作者</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=80 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image038.gif" width=64></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">Owen Cline 是位于加州圣地亚哥市的 IBM Software Services for Websphere 小组的一名成员。他有 20 多年软件开发领域的开发经验。他拥有 4 项软件专利，编写了大量 IBM 红皮书，并曾多次出席各种技术会议。在过去的 5 年当中，Owen 专门从事 J2EE 架构、应用开发和部署，重点研究了 WebSphere 平台。此外，这几年来，他还参与了许多高知名度的 Web 站点的开发。</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: 宋体; mso-outline-level: 1">&nbsp;</p>
<p style="FONT-SIZE: 8pt; MARGIN: 0in; COLOR: #666666; FONT-FAMILY: 宋体; mso-outline-level: 1">源文档 &lt;<a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0412cline/index.html"><u><font color=#800080>http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0412cline/index.html</font></u></a>&gt; </p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: 宋体; mso-outline-level: 1">&nbsp;</p>
<img src ="http://www.blogjava.net/dyerac/aggbug/129143.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/dyerac/" target="_blank">dyerac in java...</a> 2007-07-09 21:51 <a href="http://www.blogjava.net/dyerac/articles/129143.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Developing a UDR with Embedded SQL</title><link>http://www.blogjava.net/dyerac/articles/129141.html</link><dc:creator>dyerac in java...</dc:creator><author>dyerac in java...</author><pubDate>Mon, 09 Jul 2007 13:48:00 GMT</pubDate><guid>http://www.blogjava.net/dyerac/articles/129141.html</guid><wfw:comment>http://www.blogjava.net/dyerac/comments/129141.html</wfw:comment><comments>http://www.blogjava.net/dyerac/articles/129141.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/dyerac/comments/commentRss/129141.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/dyerac/services/trackbacks/129141.html</trackback:ping><description><![CDATA[<p><a name=develop><span class=atitle>Developing a UDR with Embedded SQL</span></a></p>
<p>This section will cover the nuts and bolts of developing a Java UDR with embedded SQL. Our topics will include:
<ul>
    <li><a href="http://www.ibm.com/developerworks/db2/zones/informix/library/samples/db_sqlj.html#one"><font color=#996699>Software Requirements</font></a>
    <li><a href="http://www.ibm.com/developerworks/db2/zones/informix/library/samples/db_sqlj.html#two"><font color=#996699>Server Configuration</font></a>
    <li><a href="http://www.ibm.com/developerworks/db2/zones/informix/library/samples/db_sqlj.html#three"><font color=#996699>Development Environment</font></a>
    <li><a href="http://www.ibm.com/developerworks/db2/zones/informix/library/samples/db_sqlj.html#four"><font color=#996699>Runtime Environment</font></a>
    <li><a href="http://www.ibm.com/developerworks/db2/zones/informix/library/samples/db_sqlj.html#five"><font color=#996699>Embedded SQL Syntax</font></a>
    <li><a href="http://www.ibm.com/developerworks/db2/zones/informix/library/samples/db_sqlj.html#six"><font color=#996699>Compiling Embedded SQL Java Programs</font></a> </li>
</ul>
<p>&#160;</p>
<p><a name=one><span class=smalltitle><strong><font face=Arial>Software Requirements</font></strong></span></a></p>
<p><strong><font face=Arial></font></strong>
<ol>
    <li>To develop Java UDRs that employ embedded SQL, the first thing you'll need is version 9.21 or later of Informix Foundation. Foundation consists of the Informix database engine, plus a suite of supporting software that includes "Krakatoa", which permits Java routines to run inside the server. Note that 9.20 and earlier versions will not support embedded SQL.
    <li>Next, you'll need the Informix JDBC Driver, version 2.1+ . The JDBC Driver can be downloaded free from the Informix download web site. Versions of the JDBC Driver prior to 2.1 will not support embedded SQL. </li>
</ol>
<br><br>You'll need the latest version of the Java compiler, JDK 1.2.2 or later. This can be downloaded free from Sun's Java web site.<br>
<p>&#160;</p>
<p><a name=two><span class=smalltitle><strong><font face=Arial>Server Configuration</font></strong></span></a></p>
<p>Your Informix server configuration file (typically, $INFORMIXDIR/etc/onconfig on Unix, or %INFORMIXDIR%\etc\ONCONFIG.&lt;servername&gt; on NT) includes a number of parameters for setting the server's environment to support Krakatoa (a. k. a., "Java-in-the-server"). Most are pre-set in the default configuration file, and most of the information needed to set the rest can be found in the release notes for your platform.<br><br>One setting, which is specific to using embedded SQL in Java UDRs, may not be documented in your version of the server: <strong>JVPCLASSPATH must include the ifxsqlj.jar (or ifxsqlj-g.jar) file in your JDBC installation, and it must also include the full path to the directory where the .class file for your Java UDR will reside.</strong> <br><br>On NT, your Krakatoa-related ONCONFIG settings should look something like this:</p>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td class=code-outline>
            <pre class=displaycode>VPCLASS      jvp,num=1                       # Number of JVPs to start with
            JVPJAVAHOME  D:\informix\extend\krakatoa\jre # JDK installation root directory
            JVPHOME      D:\informix\extend\krakatoa     # Krakatoa installation directory
            JVPLOGFILE   D:\informix\extend\krakatoa\jvp.log    # VP log file
            JVPPROPFILE  D:\informix\extend\krakatoa\.jvpprops  # JVP property file
            JDKVERSION   1.2                  # JDK version supported by this server
            JVMTHREAD    native               # Java VM thread type (green or native)
            # The path to the JRE libraries relative to JVPJAVAHOME
            JVPJAVALIB   \bin\
            # The JRE libraries to use for the Java VM
            JVPJAVAVM    hpi;jvm;java;net;math;zip;jpeg
            # Classpath to use upon Java VM start-up (use _g version for debugging)
            # IMPORTANT: In this sample, the line is broken to fit on a page. In a live
            # ONCONFIG file, your JVPCLASSPATH entry MUST be a single, unbroken line.
            JVPCLASSPATH d:/informix/extend/krakatoa/jdbc.jar;d:/informix/extend/krakatoa/
            krakatoa.jar;d:/java/jdbc211jc1/lib/ifxsqlj.jar;d:/informix/extend/SQLjDemo.1.0</pre>
            </td>
        </tr>
    </tbody>
</table>
<br>
<p>Note that in this example, the Informix installation directory is D:\informix.<br><br>Most of the values here were inserted automatically when Foundation was installed. However, it's worth reiterating a couple of comments about JVPCLASSPATH:</p>
<ul>
    <li>In any installation where Krakatoa is enabled, JVPCLASSPATH will include the path to the jdbc.jar and krakatoa.jar (or their debugging equivalents) in $INFORMIXDIR/extend/krakatoa. If you are going to use embedded SQL, however, you must add the full path to the ifxsqlj.jar file in your JDBC installation.
    <li>SQLj standards call for your Java routines to be bundled in .jar files, and for those .jar files to be stored in the database by means of the install_jar() SQL routine. The corresponding SQL routine is then mapped to the stored .jar file.<br><br>For any Java UDR, an alternative to this approach is to map your SQL routine directly to the .class file for your Java implementation, and enable the server to find your .class file by naming its directory in your server's JVPCLASSPATH variable.<br><br>At the time of this writing, Java UDRs that include embedded SQL must use the latter method.
    <li>JVPCLASSPATH is (at the time of this writing) limited to 256 characters. If you include a large number of directories where .class files will be stored, then you may overrun this limit, and your UDR will fail at runtime.<br><br>An alternative syntax allows you to specify JVPCLASSPATH as the name of a file, for instance:<br><br>JVPCLASSPATH file:d:/informix/extend/krakatoa/classpath.txt<br><br>The content of classpath.txt takes exactly the same format as the text in your server configuration file would: full pathnames separated by semicolons on NT, or colons on Unix, all on a single line. In this case, however, the length of the line is not limited to 256 characters. </li>
</ul>
<p><a name=two><span class=smalltitle><strong><font face=Arial>Development Environment</font></strong></span></a></p>
<p>Developing a Java UDR that uses embedded SQL is not much different from developing any other Java UDR:</p>
<ul>
    <li>You'll need access to JDK 1.2.x (1.2.x is compatible with the Java Runtime Environment (JRE) that is included with Krakatoa), so you'll need to set your PATH environment variable to include the "bin" directory of your JDK installation.
    <li>You'll want to have access to your Informix installation, so that you can test your UDR-in-progress. This means having INFORMIXDIR set to point to the root of your Informix installation, and including $INFORMIXDIR/bin in your PATH. </li>
</ul>
<p>However, there's one additional requirement:</p>
<ul>
    <li>Set CLASSPATH to include each of the .jar files in the lib directory of your JDBC installation. You can use either the debug versions, whose names end with "-g.jar", or the optimized versions. </li>
</ul>
<p><a name=three><span class=smalltitle><strong><font face=Arial>Runtime Environment</font></strong></span></a></p>
<p>Other than configuring your server as described above, there are no special runtime requirements for using Java UDRs with embedded SQL.</p>
<p><a name=four><span class=smalltitle><strong><font face=Arial>Embedded SQL Syntax</font></strong></span></a></p>
<p>This paper can present only the barest minimum of information about embedded SQL syntax, but it does seem appropriate to take a quick look at it here. Much more information about SQLj syntax can be found in the demo programs included with the Informix JDBC Driver distribution. Remember, however, when reviewing those demos, that they are client applications, and are not server-side Java routines.</p>
<p>In this section, we'll look at:</p>
<ul>
    <li><a href="http://www.ibm.com/developerworks/db2/zones/informix/library/samples/db_sqlj.html#oneb"><font color=#996699>Filenames</font></a>
    <li><a href="http://www.ibm.com/developerworks/db2/zones/informix/library/samples/db_sqlj.html#twob"><font color=#996699>SQLj import files</font></a>
    <li><a href="http://www.ibm.com/developerworks/db2/zones/informix/library/samples/db_sqlj.html#threeb"><font color=#996699>Establishing a database connection</font></a>
    <li><a href="http://www.ibm.com/developerworks/db2/zones/informix/library/samples/db_sqlj.html#fourb"><font color=#996699>Embedded SQL syntax</font></a> </li>
</ul>
<br>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td><img height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></td>
        </tr>
    </tbody>
</table>
<table class=no-print cellSpacing=0 cellPadding=0 align=right>
    <tbody>
        <tr align=right>
            <td></td>
        </tr>
    </tbody>
</table>
<br><br>
<p><a name=oneb><span class=atitle>Filenames</span></a></p>
<p>As with other Java programs, the names of those using embedded SQL are case sensitive. The precompiler will generate a .java file with the same name prefix, and the compiler will generate a .class file with the same name prefix.<br><br>The filename extension used for Java embedded SQL programs is ".sqlj".</p>
<p><a name=twob><span class=smalltitle><strong><font face=Arial>SQLj import files</font></strong></span></a></p>
<p>Your "import" list must include:<br><br>
<ol>
    <li><strong>java.sql.*</strong> (Since you will be using SQL to access a database.)
    <li><strong>com.informix.jdbc.*</strong> (The precompiler translates your SQL statements into JDBC calls.)
    <li><strong>sqlj.runtime.*</strong> (To provide runtime support for sqlj.)
    <li><strong>sqlj.runtime.ref.DefaultContext</strong> (For access to the underlying routines that establish your database connection.) </li>
</ol>
<p>&#160;</p>
<br>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td><img height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"></td>
        </tr>
    </tbody>
</table>
<table class=no-print cellSpacing=0 cellPadding=0 align=right>
    <tbody>
        <tr align=right>
            <td></td>
        </tr>
    </tbody>
</table>
<br><br>
<p><a name=threeb><span class=atitle>Establishing a Database Connection</span></a></p>
<p>In a client application using Java embedded SQL, there are a lot of options for establishing database connections, including multiple concurrent connections to several databases or server installations. To support this flexibility, SQLj uses the concept of a "context" to extend the simpler idea of a "connection", and it provides a whole range of classes and methods for managing "contexts".<br><br>Since a UDR runs inside the server, it only needs a "DefaultContext". You get a DefaultContext from a database connection that is obtained by using the JDBC DriverManager.getConnection() call, and supplying a "direct connection" database URL:</p>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td class=code-outline>
            <pre class=displaycode>public static Connection conn = null;
            public static String DRIVER = "com.informix.jdbc.IfxDriver";
            public static String DBURL = "jdbc:informix-direct";
            ...
            public static Connection newConnection() throws SQLException {
            // Load the JDBC driver
            try {
            Class.forName( DRIVER );
            }
            ...
            // Get a database connection
            try {
            conn = DriverManager.getConnection (DBURL);
            }
            ...
            // Use the connection to get a connection context
            DefaultContext ctx = DefaultContext.getDefaultContext();
            try {
            ctx = new DefaultContext(newConnection());
            }
            ...
            }   </pre>
            </td>
        </tr>
    </tbody>
</table>
<br>
<p>Please refer to the downloadable demo for a complete example.</p>
<p><a name=fourb><span class=smalltitle><strong><font face=Arial>Embedded SQL syntax</font></strong></span></a></p>
<p>Once you have established a database connection, and obtained a DefaultContext, accessing information stored in a database is simple. You'll need a program variable of the appropriate type to receive the data, and an SQL statement to retrieve it.<br><br>If you have a table like this:</p>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td class=code-outline>
            <pre class=displaycode>          CREATE TABLE excuses (id integer, words lvarchar);
            INSERT INTO excuses VALUES (1, "Dog ate my homework");
            INSERT INTO excuses VALUES (2, "Missed the bus");
            INSERT INTO excuses VALUES (3, "I Forgot!"); </pre>
            </td>
        </tr>
    </tbody>
</table>
<br>
<p>Then you could retrieve the text of an excuse into a program variable like this:</p>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td class=code-outline>
            <pre class=displaycode>          ...
            String excuse = new String();
            #sql {
            SELECT words
            INTO :excuse
            FROM excuses
            WHERE id = 3
            }; </pre>
            </td>
        </tr>
    </tbody>
</table>
<br>
<p>Note that the program variable "excuse" is preceeded by a colon when used in the SQL statement. It's important to note, too, the placement of the semicolon that terminates the SQL statement: It follows the closing bracket.</p>
<p>Complete information about mapping Informix data types to JDBC data types can be found in the Informix JDBC Driver Programmer's Guide, which is included with your JDBC distribution.</p>
<p><a name=fourb><span class=smalltitle><strong><font face=Arial>Compiling Embedded SQL Java Programs</font></strong></span></a></p>
<p>Compiling a Java UDR containing embedded SQL is simple, and is identical to compiling a client application that contains embedded SQL. If your source file is "Foo.sqlj", then compile it like this:</p>
<p>java ifxsqlj Foo.sqlj<br><br>ifxsqlj will precompile Foo.sqlj into Foo.java, then will call javac to compile Foo.java into Foo.class.<br><br>Along the way, two additional files are generated:</p>
<ul>
    <li>Foo_SJProfile0.ser
    <li>Foo_SJProfileKeys.class </li>
</ul>
<p>These files contain hooks for vendor-specific customizations, as provided by the SQLj standards. In this way, vendors can optimize the implementation of embedded SQL statements for their own database products, and still retain a vendor-neutral, portable compiled object.<br><br><strong>Tip:</strong> In the current release of the SQLj package, ifxsqlj may hang and produce no feedback at all if it encounters certain syntax errors in your code. If this happens, break out with ^C, then try to compile the generated .java file manually: javac Foo.java . The javac compiler will flag the errors in the .java file, and you can trace them back to your .sqlj code.<br><br>All three of the files generated by a successful compile will need to be made available to the server at runtime, by placing them in a directory named in your server's JVPCLASSPATH entry.</p>
<strong>Deploying a Java UDR That Uses Embedded SQL</strong>
<p>The hard part is done now: You've written your UDR and successfully compiled it. Now all you need to do is "register it to a database", which amounts to mapping it to an SQL routine.<br><br>Let's assume we have a "bladelet" with a single routine, "Foo()". Let's also assume we're going to follow the convention that most DataBlades do, and store our "Foo" files in "$INFORMIXDIR/extend/Foo". We'll call the script that maps our "Foo" Java routine to an SQL "Foo" routine -- that "registers the UDR to the database" -- "register.sql".<br><br>Following this convention, $INFORMIXDIR/extend/Foo will contain:<br><br></p>
<ul>
    <li>register.sql
    <li>Foo.class
    <li>Foo_SJProfileKeys.class
    <li>Foo_SJProfile0.ser </li>
</ul>
<p>Your register.sql script looks like this:</p>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td class=code-outline>
            <pre class=displaycode>create procedure testmain()
            external name 'Foo.Foo()'
            language java; </pre>
            </td>
        </tr>
    </tbody>
</table>
<br>
<p>The first "Foo" in the "external name" identifies the Java class, and the second "Foo" refers to the method name within that class.<br><br>How does the server find the class? It looks in each of the directories named in its JVPCLASSPATH variable until it finds the one with the name "Foo.class". If we've remembered to include d:\informix\extend\Foo in our JVPCLASSPATH entry, the server will find it.<br><br>IBM, DB2, Informix, and WebSphere are trademarks or registered trademarks of IBM Corporation in the United States, other countries, or both.<br><br>Windows and Windows NT are registered trademarks of Microsoft Corporation in the United States, other countries, or both.<br><br>Java and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States, other countries, or both.<br><br>Other company, product, and service names may be trademarks or service marks of others.</p>
<img src ="http://www.blogjava.net/dyerac/aggbug/129141.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/dyerac/" target="_blank">dyerac in java...</a> 2007-07-09 21:48 <a href="http://www.blogjava.net/dyerac/articles/129141.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Vi指令大全</title><link>http://www.blogjava.net/dyerac/articles/129135.html</link><dc:creator>dyerac in java...</dc:creator><author>dyerac in java...</author><pubDate>Mon, 09 Jul 2007 12:36:00 GMT</pubDate><guid>http://www.blogjava.net/dyerac/articles/129135.html</guid><wfw:comment>http://www.blogjava.net/dyerac/comments/129135.html</wfw:comment><comments>http://www.blogjava.net/dyerac/articles/129135.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/dyerac/comments/commentRss/129135.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/dyerac/services/trackbacks/129135.html</trackback:ping><description><![CDATA[<h1 class=firstHeading>Vi指令大全</h1>
<div id=bodyContent>
<h3 id=siteSub>Wikipedia，自由的百科全书</h3>
<div id=contentSub></div>
<!-- start content -->
<table id=toc border=0>
    <tbody>
        <tr id=toctitle>
            <td align=middle><strong>目录</strong>
            <script type=text/javascript>showTocToggle("显示","隐藏")</script>
            <span class=toctoggle>[<a class=internal href="javascript:toggleToc()"><u><font color=#0000ff><span id=showlink style="DISPLAY: none">显示</span><span id=hidelink>隐藏</span></font></u></a>]</span> </td>
        </tr>
        <tr id=tocinside>
            <td>
            <div class=tocline><a href="http://kb.discuz.net/index.php?title=Vi%E6%8C%87%E4%BB%A4%E5%A4%A7%E5%85%A8#.E8.BF.9B.E5.85.A5vi.E7.9A.84.E5.91.BD.E4.BB.A4"><font color=#0000ff><u>1 进入vi的命令</u></font></a><br></div>
            <div class=tocline><a href="http://kb.discuz.net/index.php?title=Vi%E6%8C%87%E4%BB%A4%E5%A4%A7%E5%85%A8#.E7.A7.BB.E5.8A.A8.E5.85.89.E6.A0.87.E7.B1.BB.E5.91.BD.E4.BB.A4"><font color=#0000ff><u>2 移动光标类命令</u></font></a><br></div>
            <div class=tocline><a href="http://kb.discuz.net/index.php?title=Vi%E6%8C%87%E4%BB%A4%E5%A4%A7%E5%85%A8#.E5.B1.8F.E5.B9.95.E7.BF.BB.E6.BB.9A.E7.B1.BB.E5.91.BD.E4.BB.A4"><font color=#0000ff><u>3 屏幕翻滚类命令</u></font></a><br></div>
            <div class=tocline><a href="http://kb.discuz.net/index.php?title=Vi%E6%8C%87%E4%BB%A4%E5%A4%A7%E5%85%A8#.E6.8F.92.E5.85.A5.E6.96.87.E6.9C.AC.E7.B1.BB.E5.91.BD.E4.BB.A4"><font color=#0000ff><u>4 插入文本类命令</u></font></a><br></div>
            <div class=tocline><a href="http://kb.discuz.net/index.php?title=Vi%E6%8C%87%E4%BB%A4%E5%A4%A7%E5%85%A8#.E5.88.A0.E9.99.A4.E5.91.BD.E4.BB.A4"><font color=#0000ff><u>5 删除命令</u></font></a><br></div>
            <div class=tocline><a href="http://kb.discuz.net/index.php?title=Vi%E6%8C%87%E4%BB%A4%E5%A4%A7%E5%85%A8#.E6.90.9C.E7.B4.A2.E5.8F.8A.E6.9B.BF.E6.8D.A2.E5.91.BD.E4.BB.A4"><font color=#0000ff><u>6 搜索及替换命令</u></font></a><br></div>
            <div class=tocline><a href="http://kb.discuz.net/index.php?title=Vi%E6%8C%87%E4%BB%A4%E5%A4%A7%E5%85%A8#.E9.80.89.E9.A1.B9.E8.AE.BE.E7.BD.AE"><font color=#0000ff><u>7 选项设置</u></font></a><br></div>
            <div class=tocline><a href="http://kb.discuz.net/index.php?title=Vi%E6%8C%87%E4%BB%A4%E5%A4%A7%E5%85%A8#.E6.9C.80.E5.90.8E.E8.A1.8C.E6.96.B9.E5.BC.8F.E5.91.BD.E4.BB.A4"><font color=#0000ff><u>8 最后行方式命令</u></font></a><br></div>
            <div class=tocline><a href="http://kb.discuz.net/index.php?title=Vi%E6%8C%87%E4%BB%A4%E5%A4%A7%E5%85%A8#.E5.AF.84.E5.AD.98.E5.99.A8.E6.93.8D.E4.BD.9C"><u><font color=#0000ff>9 寄存器操作</font></u></a><br></div>
            </td>
        </tr>
    </tbody>
</table>
<h2>&nbsp;</h2>
<h2>进入vi的命令</h2>
<p>vi filename: 打开或新建文件，并将光标置于第一行首 </p>
<p>vi +n filename: 打开文件，并将光标置于第n行首 </p>
<p>vi + filename: 打开文件，并将光标置于最后一行首 </p>
<p>vi +/pattern filename: 打开文件，并将光标置于第一个与pattern匹配的串处 </p>
<p>vi -r filename: 在上次正用vi编辑时发生系统崩溃，恢复filename </p>
<p>vi filename....filename: 打开多个文件，依次进行编辑 </p>
<h2>&nbsp;</h2>
<h2>移动光标类命令</h2>
<p>h: 光标左移一个字符 </p>
<p>l: 光标右移一个字符 </p>
<p>space: 光标右移一个字符 </p>
<p>Backspace: 光标左移一个字符 </p>
<p>k或Ctrl+p: 光标上移一行 </p>
<p>j或Ctrl+n: 光标下移一行 </p>
<p>Enter: 光标下移一行 </p>
<p>w或W&nbsp;: 光标右移一个字至字首 </p>
<p>b或B&nbsp;: 光标左移一个字至字首 </p>
<p>e或E&nbsp;: 光标右移一个字至字尾 </p>
<p>): 光标移至句尾 </p>
<p>(: 光标移至句首 </p>
<p>}: 光标移至段落开头 </p>
<p>{: 光标移至段落结尾 </p>
<p>nG: 光标移至第n行首 </p>
<p>n+: 光标下移n行 </p>
<p>n-: 光标上移n行 </p>
<p>n$: 光标移至第n行尾 </p>
<p>H: 光标移至屏幕顶行 </p>
<p>M: 光标移至屏幕中间行 </p>
<p>L: 光标移至屏幕最后行 </p>
<p>0: 光标移至当前行首 </p>
<p>$: 光标移至当前行尾 </p>
<a name=.E5.B1.8F.E5.B9.95.E7.BF.BB.E6.BB.9A.E7.B1.BB.E5.91.BD.E4.BB.A4></a>
<h2>屏幕翻滚类命令</h2>
<p>Ctrl+u: 向文件首翻半屏 </p>
<p>Ctrl+d: 向文件尾翻半屏 </p>
<p>Ctrl+f: 向文件尾翻一屏 </p>
<p>Ctrl＋b: 向文件首翻一屏 </p>
<p>nz: 将第n行滚至屏幕顶部，不指定n时将当前行滚至屏幕顶部。 </p>
<a name=.E6.8F.92.E5.85.A5.E6.96.87.E6.9C.AC.E7.B1.BB.E5.91.BD.E4.BB.A4></a>
<h2>插入文本类命令</h2>
<p>i: 在光标前 </p>
<p>I: 在当前行首 </p>
<p>a: 光标后 </p>
<p>A: 在当前行尾 </p>
<p>o: 在当前行之下新开一行 </p>
<p>O: 在当前行之上新开一行 </p>
<p>r: 替换当前字符 </p>
<p>R: 替换当前字符及其后的字符，直至按ESC键 </p>
<p>s: 从当前光标位置处开始，以输入的文本替代指定数目的字符 </p>
<p>S: 删除指定数目的行，并以所输入文本代替之 </p>
<p>ncw或nCW: 修改指定数目的字 </p>
<p>nCC: 修改指定数目的行 </p>
<a name=.E5.88.A0.E9.99.A4.E5.91.BD.E4.BB.A4></a>
<h2>删除命令</h2>
<p>ndw或ndW: 删除光标处开始及其后的n-1个字 </p>
<p>do: 删至行首 </p>
<p>d$: 删至行尾 </p>
<p>ndd: 删除当前行及其后n-1行 </p>
<p>x或X: 删除一个字符，x删除光标后的，而X删除光标前的 </p>
<p>Ctrl+u: 删除输入方式下所输入的文本 </p>
<a name=.E6.90.9C.E7.B4.A2.E5.8F.8A.E6.9B.BF.E6.8D.A2.E5.91.BD.E4.BB.A4></a>
<h2>搜索及替换命令</h2>
<pre>/pattern: 从光标开始处向文件尾搜索pattern
?pattern: 从光标开始处向文件首搜索pattern
n: 在同一方向重复上一次搜索命令
N: 在反方向上重复上一次搜索命令
:s/p1/p2/g: 将当前行中所有p1均用p2替代
:n1,n2s/p1/p2/g: 将第n1至n2行中所有p1均用p2替代
:g/p1/s//p2/g: 将文件中所有p1均用p2替换
</pre>
<a name=.E9.80.89.E9.A1.B9.E8.AE.BE.E7.BD.AE></a>
<h2>选项设置</h2>
<p>all: 列出所有选项设置情况 </p>
<p>term: 设置终端类型 </p>
<p>ignorance: 在搜索中忽略大小写 </p>
<p>list: 显示制表位(Ctrl+I)和行尾标志($) </p>
<p>number: 显示行号 </p>
<p>report: 显示由面向行的命令修改过的数目 </p>
<p>terse: 显示简短的警告信息 </p>
<p>warn: 在转到别的文件时若没保存当前文件则显示NO write信息 </p>
<p>nomagic: 允许在搜索模式中，使用前面不带&#8220;\&#8221;的特殊字符 </p>
<p>nowrapscan: 禁止vi在搜索到达文件两端时，又从另一端开始 </p>
<p>mesg: 允许vi显示其他用户用write写到自己终端上的信息 </p>
<a name=.E6.9C.80.E5.90.8E.E8.A1.8C.E6.96.B9.E5.BC.8F.E5.91.BD.E4.BB.A4></a>
<h2>最后行方式命令</h2>
<pre>:n1,n2 co n3: 将n1行到n2行之间的内容拷贝到第n3行下
:n1,n2 m n3:将n1行到n2行之间的内容移至到第n3行下
:n1,n2 d: 将 n1行到n2行之间的内容删除
:w: 保存当前文件
:e filename: 打开文件filename进行编辑
:x: 保存当前文件并退出
:q: 退出vi
:q!: 不保存文件并退出vi
:!command: 执行shell命令command
:n1,n2 w!command: 将文件中n1行至n2行的内容作为command的输入并执行之，<br> 若不指定n1，n2，则表示将整个文件内容作为command的输入
:r!command: 将命令command的输出结果放到当前行
</pre>
<a name=.E5.AF.84.E5.AD.98.E5.99.A8.E6.93.8D.E4.BD.9C></a>
<h2>寄存器操作</h2>
<p>"?nyy: 将当前行及其下n行的内容保存到寄存器？中，其中?为一个字母，n为一个数字 </p>
<p>"?nyw: 将当前行及其下n个字保存到寄存器？中，其中?为一个字母，n为一个数字 </p>
<p>"?nyl: 将当前行及其下n个字符保存到寄存器？中，其中?为一个字母，n为一个数字 </p>
<p>"?p: 取出寄存器？中的内容并将其放到光标位置处。这里？可以是一个字母，也可以是一个数字 </p>
<p>ndd: 将当前行及其下共n行文本删除，并将所删内容放到1号删除寄存器中 </p>
<!-- Saved in parser cache with key crossday_kb:pcache:idhash:2242-1!1!0!1!0!1!0!!zh_cn and timestamp 20070708142220 -->
<div class=printfooter>取自"<a href="http://kb.discuz.net/index.php?title=Vi%E6%8C%87%E4%BB%A4%E5%A4%A7%E5%85%A8"><u><font color=#0000ff>http://kb.discuz.net/index.php?title=Vi%E6%8C%87%E4%BB%A4%E5%A4%A7%E5%85%A8</font></u></a>"</div>
<!-- end content -->
<div class=visualClear></div>
</div>
<img src ="http://www.blogjava.net/dyerac/aggbug/129135.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/dyerac/" target="_blank">dyerac in java...</a> 2007-07-09 20:36 <a href="http://www.blogjava.net/dyerac/articles/129135.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(zt)现在你可以定义自己的语言！</title><link>http://www.blogjava.net/dyerac/articles/129134.html</link><dc:creator>dyerac in java...</dc:creator><author>dyerac in java...</author><pubDate>Mon, 09 Jul 2007 12:34:00 GMT</pubDate><guid>http://www.blogjava.net/dyerac/articles/129134.html</guid><wfw:comment>http://www.blogjava.net/dyerac/comments/129134.html</wfw:comment><comments>http://www.blogjava.net/dyerac/articles/129134.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/dyerac/comments/commentRss/129134.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/dyerac/services/trackbacks/129134.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;现在你可以定义自己的语言！文档选项将此页作为电子邮件发送未显示需要 JavaScript 的文档选项级别: 初级JoAnn P. Brereton (brereton@us.ibm.com), 高级软件工程师，IBM2004 年 5 月 01 日JavaCC 是一个功能极其强大的&#8216;编译器的编译器&#8217...&nbsp;&nbsp;<a href='http://www.blogjava.net/dyerac/articles/129134.html'>阅读全文</a><img src ="http://www.blogjava.net/dyerac/aggbug/129134.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/dyerac/" target="_blank">dyerac in java...</a> 2007-07-09 20:34 <a href="http://www.blogjava.net/dyerac/articles/129134.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(zt)利用 DB2 UDB 来使用 Java CachedRowSet 实现</title><link>http://www.blogjava.net/dyerac/articles/129133.html</link><dc:creator>dyerac in java...</dc:creator><author>dyerac in java...</author><pubDate>Mon, 09 Jul 2007 12:32:00 GMT</pubDate><guid>http://www.blogjava.net/dyerac/articles/129133.html</guid><wfw:comment>http://www.blogjava.net/dyerac/comments/129133.html</wfw:comment><comments>http://www.blogjava.net/dyerac/articles/129133.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/dyerac/comments/commentRss/129133.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/dyerac/services/trackbacks/129133.html</trackback:ping><description><![CDATA[<p>&#160;</p>
<p style="FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: arial; mso-outline-level: 1">利用 DB2 UDB 来使用 Java CachedRowSet 实现</p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 9pt; MARGIN: 0in; COLOR: #666666; FONT-FAMILY: verdana; mso-outline-level: 1">断开连接</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=18 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image002.gif" width=192></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image003.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="javascript:document.email.submit();"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>将此页作为电子邮件发送</u></span></a></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><u><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></u></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><u><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></u></p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: #ff3300; FONT-FAMILY: Verdana; mso-outline-level: 1">未显示需要 JavaScript 的文档选项</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image004.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#download"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>样例代码</u></span></a></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">级别: 初级</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#author"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>Kulvir Singh Bhogal</u></span></a><span style="COLOR: black; FONT-FAMILY: Arial"> (</span><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#107;&#98;&#104;&#111;&#103;&#97;&#108;&#64;&#117;&#115;&#46;&#105;&#98;&#109;&#46;&#99;&#111;&#109;&#63;&#115;&#117;&#98;&#106;&#101;&#99;&#116;&#61;&#21033;&#29992;&#37;&#50;&#48;&#68;&#66;&#50;&#37;&#50;&#48;&#85;&#68;&#66;&#37;&#50;&#48;&#26469;&#20351;&#29992;&#37;&#50;&#48;&#74;&#97;&#118;&#97;&#37;&#50;&#48;&#67;&#97;&#99;&#104;&#101;&#100;&#82;&#111;&#119;&#83;&#101;&#116;&#37;&#50;&#48;&#23454;&#29616;"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>kbhogal@us.ibm.com</u></span></a><span style="COLOR: black; FONT-FAMILY: Arial">), IBM Software Services for WebSphere, IBM Corporation</span></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">2004 年 8 月 01 日</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 2">高速缓存行集（Cached Row Set）是 Java&#8482; 1.5 提供的一项新功能，源自 JSR114 的努力。这一新功能使您可以拥有一个可串行化的断开连接的对象。这意味着您可以连接到数据库，以结果集的形式取得数据，释放连接并在本地操纵这些数据，然后恢复连接以完成事务，这样可以大大减少对连接和服务器资源的使用。本文展示这一切是如何利用 DB2&#174; Universal Database&#8482; 来实现的，并包含示例代码。</p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">简介</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">应该把数据库连接看作是一种被应用程序广泛使用的宝贵资源。尤其是在高流量的 Web 站点中，客户机应该使用由应用程序服务器（例如，IBM WebSphere&#174; Application Server）管理的数据库连接池中的数据库连接，并在其必要的事务发生之后释放数据库连接，这一点很重要。</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; mso-outline-level: 1"><span style="FONT-FAMILY: Arial">在许多情况下，事务会话会持续较长一段时间，占用着数据库连接，直到完成。过去，Java 程序员一直使用没有高速缓存解决方案的 JDBC API。高速缓存解决方案允许用户在本地高速缓存结果集，释放连接，操纵结果集数据，并在稍后的时间点与数据库同步。在本文中，我将介绍 </span><span style="FONT-FAMILY: courier">javax.sql.RowSet</span><span style="FONT-FAMILY: Arial"> 接口的实现，该实现允许我们实现高速缓存解决方案。 </span></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">获得 RowSet 实现</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><span style="COLOR: black; FONT-FAMILY: courier">javax.sql.RowSet</span><span style="COLOR: black; FONT-FAMILY: Arial"> 接口是在 J2SE Version 1.4 中引入到 Java 语言的。随着 Java 2 Standard Edition version 1.5 的发布，我们将会看到该接口的增强和绑定到语言的接口实现。 </span><span style="COLOR: black; FONT-FAMILY: courier">RowSet</span><span style="COLOR: black; FONT-FAMILY: Arial"> 接口是 </span><a href="http://www.jcp.org/en/jsr/detail?id=114"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>JSR 114</u></span></a><span style="COLOR: black; FONT-FAMILY: Arial">的结果，JSR 114 勾勒了一个方案，也就是&#8220;将表格数据与数据源分隔开&#8230;&#8230;&#8221;，从而增加&#8220;应用程序的可扩展性和编程模型的灵活性&#8221;。我使用 beta 版本的 Tiger (Java 1.5) 来进行开发。如果您使用的是以前版本的 Java，应该尝试 RowSet 的参考实现，可以 </span><a href="http://java.sun.com/products/jdbc/download.html#rowset1_0"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>从 Sun</u></span></a><a href="http://java.sun.com/products/jdbc/download.html#rowset1_0"><span style="COLOR: blue; FONT-FAMILY: Arial"><u> </u></span></a><a href="http://java.sun.com/products/jdbc/download.html#rowset1_0"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>Microsystems 下载参考实现</u></span></a><span style="COLOR: black; FONT-FAMILY: Arial">。对于我们的例子，我们将利用 </span><span style="COLOR: black; FONT-FAMILY: courier">CachedRowSet</span><span style="COLOR: black; FONT-FAMILY: Arial"> 实现。 </span></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">您以前看到过 RowSet 实现</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; mso-outline-level: 1"><span style="FONT-FAMILY: Arial">本文中突出展示的 </span><span style="FONT-FAMILY: courier">CachedRowSetImpl</span><span style="FONT-FAMILY: Arial"> 类是 </span><span style="FONT-FAMILY: courier">javax.sql.Rowset</span><span style="FONT-FAMILY: Arial"> 接口的一个实现。 </span><span style="FONT-FAMILY: courier">javax.sql.Rowset</span><span style="FONT-FAMILY: Arial"> 扩展了 </span><span style="FONT-FAMILY: courier">java.sql.ResultSet</span><span style="FONT-FAMILY: Arial"> 接口。因此，如果您熟悉 </span><span style="FONT-FAMILY: courier">javax.sql.ResultSet</span><span style="FONT-FAMILY: Arial"> 接口，我是这样假设的，那么您会认出 </span><span style="FONT-FAMILY: courier">CachedRowSetImpl</span><span style="FONT-FAMILY: Arial"> 提供的许多方法。 </span><span style="FONT-FAMILY: courier">java.sql.RowSet</span><span style="FONT-FAMILY: Arial"> 接口提供您在标准 JDBC 2.0 ResultSet 中看到过的所有方法；附加价值是，我们不需要连续使用数据库连接。就相当于我们可以走进商店，取走商品目录之后进行挑选，然后再带着填好的订单回到商店。 </span></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">使用 CachedRowSet</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; mso-outline-level: 1"><span style="FONT-FAMILY: courier">CachedRowSet</span><span style="FONT-FAMILY: Arial"> 是一个 JavaBean。您可以通过使用现有的 </span><span style="FONT-FAMILY: courier">ResultSet</span><span style="FONT-FAMILY: Arial"> 对象，或者通过指定连接连接信息和 SQL 查询，来填充 </span><span style="FONT-FAMILY: courier">CachedRowSet</span><span style="FONT-FAMILY: Arial"> 。我们采用后一种方法。但是，首先为我们的沙箱创建一个小的 DB2 数据库。 </span></p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">db2 =&gt; create database cachedex<br></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">我假设在您的机器上具有一个叫做 db2admin 的用户，口令为 db2admin：</p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">db2=&gt; connect to cachedex user db2admin using db2admin<br></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">创建一个名为 cachetbl 的表：</p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">db2=&gt; create table cachetbl (id int primary key not null, firstname varchar(40) not null, lastname varchar(40) not null)<br></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">然后用以下行填充该表：</p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">insert into cachetbl values (12345,&#8217;Kulvir&#8217;,&#8217;Bhogal&#8217;)<br>insert into cachetbl values (23456,&#8217;Meet&#8217;,&#8217;Feona&#8217;)<br>insert into cachetbl values (34567,&#8217;Bicky&#8217;,&#8217;Singh&#8217;)<br></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><span style="COLOR: black; FONT-FAMILY: Arial">我提供的示例代码建立一个到 DB2 的直接连接。如果您是通过由应用程序服务器（比如 IBM WebSphere Application Server）管理的池获得数据库连接，那么可以使用 </span><span style="COLOR: black; FONT-FAMILY: courier">execute</span><span style="COLOR: black; FONT-FAMILY: Arial"> 方法的另一种形式，该方法利用 </span><span style="COLOR: black; FONT-FAMILY: courier">java.sql.Connection </span><span style="COLOR: black; FONT-FAMILY: Arial">对象作为参数。通过阅读我的相关主题的文章 </span><a href="http://www.ftponline.com/javapro/2004_01/online/j2ee_sbhogal_01_28_04/"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>Using</u></span></a><a href="http://www.ftponline.com/javapro/2004_01/online/j2ee_sbhogal_01_28_04/"><span style="COLOR: blue; FONT-FAMILY: Arial"><u> </u></span></a><a href="http://www.ftponline.com/javapro/2004_01/online/j2ee_sbhogal_01_28_04/"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>Data Sources the Right Way</u></span></a><span style="COLOR: black; FONT-FAMILY: Arial">，可以更多地了解使用数据源 —— 一种 J2EE 最佳实践。 </span></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">可串行化</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; mso-outline-level: 1"><span style="FONT-FAMILY: courier">javax.sql.RowSet</span><span style="FONT-FAMILY: Arial"> 的实现可以被串行化。对于处理 Enterprise Java Beans 的程序员来说，这是一个比较好的消息。标准 JDBC 2.0 </span><span style="FONT-FAMILY: courier">ResultSets</span><span style="FONT-FAMILY: Arial"> 是不可串行化的，这使得是否需要使用定制对象，以便 </span><span style="FONT-FAMILY: courier">ResultSet</span><span style="FONT-FAMILY: Arial"> 数据可以被发送回 EJB 设置中的客户机以进行操纵或查询，成了一个争论。有了 </span><span style="FONT-FAMILY: courier">RowSet</span><span style="FONT-FAMILY: Arial"> 实现的出现，我们就可以串行化 </span><span style="FONT-FAMILY: courier">ResultSets</span><span style="FONT-FAMILY: Arial"> ，将它们发送到我们的客户机，然后我们的客户机可以读取和更新 </span><span style="FONT-FAMILY: courier">ResultSet</span><span style="FONT-FAMILY: Arial"> ，并将它发送回服务器。 </span></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">可滚动</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; mso-outline-level: 1"><span style="FONT-FAMILY: courier">CachedRowSetImpl</span><span style="FONT-FAMILY: Arial"> 实现是可滚动的，所以允许您在 </span><span style="FONT-FAMILY: courier">RowSet</span><span style="FONT-FAMILY: Arial"> 给出的记录集中向前和向后滚动。这在 JDBC 2.0 </span><span style="FONT-FAMILY: courier">ResultSet</span><span style="FONT-FAMILY: Arial"> 中是允许的。但是以前，在滚动发生期间必须维护一个会话。现在，我们可以在离线数据中滚动了。 </span></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">作一个数据完整性的乐天派</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; mso-outline-level: 1"><span style="FONT-FAMILY: Arial">您可能会想，如果在客户机 A 将更改与数据库服务器进行同步之前，客户机 A 上缓存的数据被另一个客户机（客户机 B）操纵，那将会发生什么事情。 </span><span style="FONT-FAMILY: courier">CachedRowSet</span><span style="FONT-FAMILY: Arial"> 的默认实现不对数据库服务器维护锁。参考实现使用乐观的同步。具体地说就是，如果客户机 A 试图操纵的数据在数据库服务器上没有更改，那么更新将会被数据库接受。但是，如果在次期间目标数据被更改，就会抛出一个同步异常。注意，这种乐观方法也是参考实现处理并发性的方式。其他实现可能采取不同的并发策略；规范并不强制要求使用某个特定的并发模型。 </span></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">查看起作用的东西</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><span style="COLOR: black; FONT-FAMILY: Arial">我提供了一个 </span><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#download"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>示例程序</u></span></a><span style="COLOR: black; FONT-FAMILY: Arial">，该程序演示了 </span><span style="COLOR: black; FONT-FAMILY: courier">CachedRowSetImpl</span><span style="COLOR: black; FONT-FAMILY: Arial"> 提供的一些功能。这些代码都可以在文件 </span><span style="COLOR: black; FONT-FAMILY: courier">DisconnectedExample.java</span><span style="COLOR: black; FONT-FAMILY: Arial"> 中找到。我将解释该程序的一些部分，以便您可以理解我想要传达的内容。注意，要运行示例应用程序，需要在运行时类路径中包含 DB2 Universal JDBC 驱动程序 (db2jcc.jar)。还需要包含 db2jcc_license_cu.jar 文件。关于设置环境的更多信息，请参考我的文章： </span><a href="http://www.informit.com/articles/article.asp?p=170336"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>Hooking Up with DB2</u></span></a><a href="http://www.informit.com/articles/article.asp?p=170336"><span style="COLOR: blue; FONT-FAMILY: Arial"><u> </u></span></a><a href="http://www.informit.com/articles/article.asp?p=170336"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>Universal Database Version 8 using Java</u></span></a><span style="COLOR: black; FONT-FAMILY: Arial">。 </span></p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">Class.forName("com.ibm.db2.jcc.DB2Driver");<br>CachedRowSet crs = new CachedRowSetImpl();<br>crs.setUsername("db2admin"); crs.setPassword("db2admin"); crs.setUrl("jdbc:db2://localhost:50000/cachedex"); crs.setCommand("SELECT id,firstname,lastname from cachetbl");<br>crs.execute();<br>System.out.println("---------------------------");<br>// display size of cached row set<br>System.out.println("Size: " + crs.size() + " records");<br>// display records in cachedrowset<br>while (crs.next())<br>{<br>System.out.println(crs.getRow() + " - " + crs.getString("firstname") + " " + crs.getString("lastname"));<br>}<br>System.out.println("---------------------------");<br></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; mso-outline-level: 1"><span style="FONT-FAMILY: Arial">在上面的代码中，我创建了一个 </span><span style="FONT-FAMILY: courier">com.sun.rowset.CachedRowSetImpl</span><span style="FONT-FAMILY: Arial"> 对象。注意我是如何为 </span><span style="FONT-FAMILY: courier">CachedRowsetImpl</span><span style="FONT-FAMILY: Arial"> 对象指定数据库连接信息的。我用 </span><span style="FONT-FAMILY: courier">setCommand</span><span style="FONT-FAMILY: Arial"> 方法来指定想要执行的查询。当发出 </span><span style="FONT-FAMILY: courier">execute</span><span style="FONT-FAMILY: Arial"> 方法时，就会填充 </span><span style="FONT-FAMILY: courier">CachedRowSetImpl</span><span style="FONT-FAMILY: Arial"> 对象。就是在该方法调用中，我们获得然后再关闭数据库连接。然后我可以在对象中迭代，并使用 getter 方法来抽取和报告其中包含的数据。 </span></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; mso-outline-level: 1"><span style="FONT-FAMILY: Arial">正如前面所提到的， </span><span style="FONT-FAMILY: courier">CachedRowSetImpl</span><span style="FONT-FAMILY: Arial"> 对象是可滚动的。下面的代码例示了这项功能，我在该代码中使用 </span><span style="FONT-FAMILY: courier">last()</span><span style="FONT-FAMILY: Arial"> 和 </span><span style="FONT-FAMILY: courier">previous()</span><span style="FONT-FAMILY: Arial"> 方法在数据间前后滚动。 </span></p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">System.out.println("Showcase scrollability");<br>// move backwards through rowset<br>// scroll to last row<br>crs.last();<br>// iterate to next row<br>while (crs.previous())<br>{<br>// report current row contents<br>System.out.println(crs.getRow() + " - " + crs.getString("lastname"));<br>}<br></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; mso-outline-level: 1"><span style="FONT-FAMILY: Arial">方法 </span><span style="FONT-FAMILY: courier">first()</span><span style="FONT-FAMILY: Arial"> 和 </span><span style="FONT-FAMILY: courier">next()</span><span style="FONT-FAMILY: Arial"> 也可用于在数据间滚动。 </span></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; mso-outline-level: 1"><span style="FONT-FAMILY: Arial">为了演示从数据库断开连接的能力，示例程序设计为暂停，并让您实际地执行生动的操作，即停止 DB2，以真正了解 </span><span style="FONT-FAMILY: courier">CachedRowSet</span><span style="FONT-FAMILY: Arial"> 功能的优势。在继续操作（由完成 </span><span style="FONT-FAMILY: courier">BufferedReader</span><span style="FONT-FAMILY: Arial"> 对象的一个简单 </span><span style="FONT-FAMILY: courier">readLine</span><span style="FONT-FAMILY: Arial"> 而推动）之前，应用程序等待按下任意键。当示例程序提示您停止 DB2 时，可以在一个新的 DB2 命令行处理器窗口中使用下面的命令： </span></p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">db2 force applications all<br></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">然后再使用</p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">db2stop<br></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">来强迫 DB2 的停止。</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">尽管数据库已经停止，但是下面的代码必须执行：</p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">System.out.println("Demonstration of how to update cached row set"); crs.updateString("lastname","Kaur");<br>// commit changes to cached portion of rowset<br>crs.updateRow();<br>System.out.println(crs.getRow() + " - " + crs.getString("lastname"));<br></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; mso-outline-level: 1"><span style="FONT-FAMILY: Arial">在上面的代码中，我们更改了当前行条目的 last name 字段。注意，对 </span><span style="FONT-FAMILY: courier">CachedRowSetImpl</span><span style="FONT-FAMILY: Arial"> 对象使用了 </span><span style="FONT-FAMILY: courier">updateRow()</span><span style="FONT-FAMILY: Arial"> 方法，以记录对该对象的更改。在这一时间点，数据库还没有被更新。正如您可以回想起的，数据库 </span><span style="FONT-STYLE: italic; FONT-FAMILY: Arial">甚至没有运行</span><span style="FONT-FAMILY: Arial">。应用程序将提示您启动 DB2。为此，可以从 DB2 命令行处理器发出下面的命令： </span></p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">db2start<br></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">在 DB2 启动之后，按任意键继续示例应用程序。</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">此时，下面的代码将会运行，这将有效地在数据库中同步更改。</p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">crs.acceptChanges();<br></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"><span style="COLOR: black">通过对 cachetbl 执行一个选择查询，可以确认数据库实际上已经更新，如 </span><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#fig1"><span style="COLOR: blue"><u>图 1</u></span></a><span style="COLOR: black">所示： </span></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">图 1. 确认更新更改已经与数据库同步</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=192 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image007.gif" width=576></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; mso-outline-level: 1"><span style="FONT-FAMILY: Arial">为了结束我们对 </span><span style="FONT-FAMILY: courier">CachedRowSet</span><span style="FONT-FAMILY: Arial"> 的动手研究，示例应用程序还演示了如何插入和删除行。要执行插入，必须将光标移动到一个叫做&#8220;插入行&#8221;的特殊位置。在这里，我们使用更新方法来填充新行。当我们使用 </span><span style="FONT-FAMILY: courier">insertRow()</span><span style="FONT-FAMILY: Arial"> 方法时， </span><span style="FONT-FAMILY: courier">CachedRowSetImpl</span><span style="FONT-FAMILY: Arial"> 对象就会被更新。当执行 </span><span style="FONT-FAMILY: courier">acceptChanges()</span><span style="FONT-FAMILY: Arial"> 方法时，更改就被持久地存储到数据库中。 </span></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">以下代码演示了这一点：</p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">// move the cursor to a blank row crs.moveToInsertRow();<br>// populate the new row<br>crs.updateInt(1,01234);<br>crs.updateString(2,"Judith");<br>crs.updateString(3,"Smith");<br>// insert the new row<br>crs.insertRow();<br>// move cursor back to previous position<br>crs.moveToCurrentRow();<br>// synchronize changes to database<br>crs.acceptChanges();<br></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">在插入完成之后程序将会暂停，以允许您在数据库中查询插入。</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; mso-outline-level: 1"><span style="FONT-FAMILY: Arial">从对象删除行相当直观。只需要将光标定位到想要删除的地方，然后使用 </span><span style="FONT-FAMILY: courier">deleteRow</span><span style="FONT-FAMILY: Arial"> 方法即可。跟前面一样，需要一个后续的同步，以将更改持久存储到数据库中： </span></p>
<p style="FONT-SIZE: 8.25pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: 'Andale Mono'; mso-outline-level: 1">// delete row (where the cursor is currently positioned)<br>crs.deleteRow();<br>// synchronize changes to database<br>crs.acceptChanges();<br></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">只是一种实现</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; mso-outline-level: 1"><span style="FONT-FAMILY: Arial">读者应该注意， </span><span style="FONT-FAMILY: courier">javax.sql.RowSet</span><span style="FONT-FAMILY: Arial"> 接口的 </span><span style="FONT-FAMILY: courier">CachedRowSetImpl</span><span style="FONT-FAMILY: Arial"> 实现只是一种实现（更确切地说是参考实现）。其他供应商也拥有 </span><span style="FONT-FAMILY: courier">RowSet</span><span style="FONT-FAMILY: Arial"> 的实现，这些实现与参考实现处理问题（比如数据完整性）的方式可能会不同（例如，第三方实现可能会对服务器进行锁维护）。 </span></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">普遍含义</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; mso-outline-level: 1"><span style="FONT-FAMILY: Arial">可更新的&#8220;断开连接的&#8221; </span><span style="FONT-FAMILY: courier">ResultSets</span><span style="FONT-FAMILY: Arial"> 在普遍世界中具有各种含义，其中，网络连接可以是断断续续的。使用 </span><span style="FONT-FAMILY: courier">CachedRowSet</span><span style="FONT-FAMILY: Arial"> 可以允许客户机在连接可用时将数据缓存在本地，然后重新连接，以同步对数据执行的更改。 </span></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">谨慎使用</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">断开连接具有其优点。但是用户必须理解，断开连接的对象是保存在内存中。因此，不应该使用具有大型结果集的方法。大型结果集的相应更新和滚动会是耗资源的操作。</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">结束语</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; mso-outline-level: 1"><span style="FONT-FAMILY: Arial">与标准 JDBC 2.0 </span><span style="FONT-FAMILY: courier">ResultSet</span><span style="FONT-FAMILY: Arial"> 不一样，利用 </span><span style="FONT-FAMILY: courier">CachedRowSet</span><span style="FONT-FAMILY: Arial"> 不再需要连续使用数据库连接。因为数据库连接池中的数据库连接是宝贵的资源，所以连接数据库、断开连接，然后重新连接以便同步数据库，这种能力无疑是非常受欢迎的。 </span></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">致谢</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">作者感谢 IBM Life Sciences Development 的 Richard Dettinger 对本文的审稿。</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image005.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image006.gif" width=16></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#main"><span style="FONT-WEIGHT: bold; COLOR: blue; FONT-FAMILY: Arial"><u>回页首</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">下载</p>
<p style="MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1"><span style="FONT-SIZE: 9pt">名字</span><span style="FONT-SIZE: 10pt"> </span><span style="FONT-SIZE: 9pt">大小</span><span style="FONT-SIZE: 10pt"> </span><span style="FONT-SIZE: 9pt">下载方法</span></p>
<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">DisconnectedExample.java</p>
<p style="FONT-SIZE: 9pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">4.03 KB</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/DisconnectedExample.java"><span style="FONT-WEIGHT: bold; FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>HTTP</u></span></a></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><u><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></u></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><u><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image008.gif" width=16></u></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.ibm.com/developerworks/cn/whichmethod.html"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>关于下载方法的信息</u></span></a></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><u><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></u></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><u><img height=16 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image009.gif" width=16></u></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; mso-outline-level: 1"><a href="http://www.adobe.com/products/acrobat/readstep2.html"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>Get Adobe&#174;</u></span></a><a href="http://www.adobe.com/products/acrobat/readstep2.html"><span style="COLOR: blue; FONT-FAMILY: Arial"><u> </u></span></a><a href="http://www.adobe.com/products/acrobat/readstep2.html"><span style="COLOR: blue; FONT-FAMILY: Arial"><u>Reader&#174;</u></span></a></p>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">参考资料 </p>
<ul style="MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in; MARGIN-LEFT: 1in; DIRECTION: ltr; unicode-bidi: embed" type=disc>
    <li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 2"><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">您可以参阅本文在 developerWorks 全球站点上的 </span><a href="http://www.ibm.com/developerworks/db2/library/techarticle/dm-0406bhogal/index.html?S_TACT=105AGX52&amp;S_CMP=cn-a-db2"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>英文原文</u></span></a><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">. </span></li>
    <li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 2"><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">下载本文中使用的 </span><a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html#download"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>源代码</u></span></a><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">。 </span></li>
    <li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 2"><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">文章 </span><a href="http://www.ftponline.com/javapro/2004_01/online/j2ee_sbhogal_01_28_04/"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>Using</u></span></a><a href="http://www.ftponline.com/javapro/2004_01/online/j2ee_sbhogal_01_28_04/"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u> </u></span></a><a href="http://www.ftponline.com/javapro/2004_01/online/j2ee_sbhogal_01_28_04/"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>Data Sources the Right Way</u></span></a><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">（ </span><span style="FONT-SIZE: 9pt; COLOR: black; FONT-STYLE: italic; FONT-FAMILY: Arial">Java Pro</span><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">，2004 年 2 月），告诉您如何通过 IBM WebSphere Application Server 设置 5.0 版本数据源。 </span></li>
    <li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 2"><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">文章 </span><a href="http://www.informit.com/articles/article.asp?p=170336"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>Hooking</u></span></a><a href="http://www.informit.com/articles/article.asp?p=170336"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u> </u></span></a><a href="http://www.informit.com/articles/article.asp?p=170336"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>Up with DB2 Universal Database Version 8 using Java</u></span></a><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">（ </span><span style="FONT-SIZE: 9pt; COLOR: black; FONT-STYLE: italic; FONT-FAMILY: Arial">InformIT</span><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">，2004 年 4 月），告诉您 DB2 UDB 和 JDBC 如何入门。 </span></li>
    <li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 2"><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">了解更多的 </span><a href="http://www.jcp.org/en/jsr/detail?id=114"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>JSR 114 JDBC rowset</u></span></a><a href="http://www.jcp.org/en/jsr/detail?id=114"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u> </u></span></a><a href="http://www.jcp.org/en/jsr/detail?id=114"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>实现</u></span></a><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">。 </span></li>
    <li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 2"><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">从 Sun Microsystems 下载 </span><a href="http://java.sun.com/products/jdbc/download.html#rowset1_0"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>JDBC 参考实现</u></span></a><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">。 </span></li>
    <li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 2"><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">检查 </span><a href="http://www.ibm.com/developerworks/java/?S_TACT=105AGX52&amp;S_CMP=cn-a-db2"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>developerWorks</u></span></a><a href="http://www.ibm.com/developerworks/java/?S_TACT=105AGX52&amp;S_CMP=cn-a-db2"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u> </u></span></a><a href="http://www.ibm.com/developerworks/java/?S_TACT=105AGX52&amp;S_CMP=cn-a-db2"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>Java 技术</u></span></a><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">，了解关于开发 Java 应用程序的更多信息和资源。 </span></li>
    <li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 2"><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">检查 developerWorks DB2 </span><a href="http://www.ibm.com/developerworks/db2/zones/java/?S_TACT=105AGX52&amp;S_CMP=cn-a-db2"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>Java</u></span></a><a href="http://www.ibm.com/developerworks/db2/zones/java/?S_TACT=105AGX52&amp;S_CMP=cn-a-db2"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u> </u></span></a><a href="http://www.ibm.com/developerworks/db2/zones/java/?S_TACT=105AGX52&amp;S_CMP=cn-a-db2"><span style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial"><u>技术</u></span></a><span style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial">页面，了解关于开发特定于 DB2 的 Java 应用程序的信息和资源。 </span></li>
</ul>
<p style="FONT-WEIGHT: bold; FONT-SIZE: 18.75pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">关于作者</p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=141 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image010.gif" width=100></p>
<p style="MARGIN: 0in; mso-outline-level: 1"><img height=1 src="file:///C:/DOCUME~1/ROGER/LOCALS~1/Temp/msohtml1/01/clip_image001.gif" width=1></p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; COLOR: black; FONT-FAMILY: Arial; mso-outline-level: 1">Kulvir Singh Bhogal 是一名 IBM 顾问，他为全国性的客户网站设计和实现以 Java 为中心的解决方案。 .</p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: 宋体; mso-outline-level: 1">&nbsp;</p>
<p style="FONT-SIZE: 8pt; MARGIN: 0in; COLOR: #666666; FONT-FAMILY: 宋体; mso-outline-level: 1">源文档 &lt;<a href="http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html"><u><font color=#800080>http://www.ibm.com/developerworks/cn/db2/library/techarticles/dm-0406bhogal/index.html</font></u></a>&gt; </p>
<p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: 宋体; mso-outline-level: 1">&nbsp;</p>
<img src ="http://www.blogjava.net/dyerac/aggbug/129133.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/dyerac/" target="_blank">dyerac in java...</a> 2007-07-09 20:32 <a href="http://www.blogjava.net/dyerac/articles/129133.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>