﻿<?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-Collus</title><link>http://www.blogjava.net/Collus/</link><description>惜秦怀古</description><language>zh-cn</language><lastBuildDate>Sun, 03 May 2026 10:49:03 GMT</lastBuildDate><pubDate>Sun, 03 May 2026 10:49:03 GMT</pubDate><ttl>60</ttl><item><title>动态跟踪Java代码的执行状况工具--BTrace</title><link>http://www.blogjava.net/Collus/articles/320789.html</link><dc:creator>良帅</dc:creator><author>良帅</author><pubDate>Thu, 13 May 2010 05:00:00 GMT</pubDate><guid>http://www.blogjava.net/Collus/articles/320789.html</guid><wfw:comment>http://www.blogjava.net/Collus/comments/320789.html</wfw:comment><comments>http://www.blogjava.net/Collus/articles/320789.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Collus/comments/commentRss/320789.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Collus/services/trackbacks/320789.html</trackback:ping><description><![CDATA[非常强烈的推荐下BTrace这个工具，用了后不得不说太强大了，BTrace简单来说，就是能在不改动当前程序的情况下，运行时的去监控Java程序的
执行状况，例如可以做到内存状况的监控、方法调用的监控等等，官方网站上有非常多详细的例子，我不说太多，只在下面举一个简单的例子来说明它的作
用，BTrace的User Guide请见：<a href="http://kenai.com/projects/btrace/pages/UserGuide">http://kenai.com/projects/btrace/pages/UserGuide</a>。<br />
对于运行中的Java程序，尤其是出了问题的程序，会需要跟踪其执行状况，例如传入的参数是什么、执行了多少时间，返回的对象是什么，抛出了什么异常，传
统的做法只能是把程序改一遍，加上一堆log，一个例子来展示下用BTrace的情况下，怎么来跟踪一个方法的执行时间：<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">@BTrace </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> MethodResponseTime {<br />    <br />    @TLS </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> startTime;<br />    <br />    @OnMethod(clazz</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">类名</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,method</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">方法名</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> onCall(){<br />        println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">enter this method</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        startTime</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">timeMillis();<br />    }<br />    <br />    @OnMethod(clazz</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">类名</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,method</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">方法名</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,location</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">@Location(Kind.RETURN))<br />    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> onReturn(){<br />        println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">method end!</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        println(strcat(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Time taken ms</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,str(timeMillis()</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">startTime)));<br />    }<br />    <br />}</span></div>用btrace执行上面的代码，就可以动态的监控任意的目前运行的Java程序中某类的某方法的执行时间，执行上面代码的方式如下（jdk 6+）：<br />
btrace [pid] MethodResponseTime.class<br /><br />
还有例如获取调用参数、调用者的对象实例以及返回值等请参看User Guide。<br /><br />
btrace为了保持JVM运行的安全性，因此做了很多的限制，例如不能抛出异常、修改传入的参数的值、修改返回值等，基本是一个只读的动态分析代码运行
状况的工具，但仍然是非常的有用，其实现机制是attach api + asm +  instrumentation。<img src ="http://www.blogjava.net/Collus/aggbug/320789.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Collus/" target="_blank">良帅</a> 2010-05-13 13:00 <a href="http://www.blogjava.net/Collus/articles/320789.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>StingUtil</title><link>http://www.blogjava.net/Collus/articles/320748.html</link><dc:creator>良帅</dc:creator><author>良帅</author><pubDate>Wed, 12 May 2010 14:04:00 GMT</pubDate><guid>http://www.blogjava.net/Collus/articles/320748.html</guid><wfw:comment>http://www.blogjava.net/Collus/comments/320748.html</wfw:comment><comments>http://www.blogjava.net/Collus/articles/320748.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Collus/comments/commentRss/320748.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Collus/services/trackbacks/320748.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: JAVA StringUitl												Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->				import				 java.io.IOException;				import				 java.io...&nbsp;&nbsp;<a href='http://www.blogjava.net/Collus/articles/320748.html'>阅读全文</a><img src ="http://www.blogjava.net/Collus/aggbug/320748.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Collus/" target="_blank">良帅</a> 2010-05-12 22:04 <a href="http://www.blogjava.net/Collus/articles/320748.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>FileUtil</title><link>http://www.blogjava.net/Collus/articles/320747.html</link><dc:creator>良帅</dc:creator><author>良帅</author><pubDate>Wed, 12 May 2010 14:03:00 GMT</pubDate><guid>http://www.blogjava.net/Collus/articles/320747.html</guid><wfw:comment>http://www.blogjava.net/Collus/comments/320747.html</wfw:comment><comments>http://www.blogjava.net/Collus/articles/320747.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Collus/comments/commentRss/320747.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Collus/services/trackbacks/320747.html</trackback:ping><description><![CDATA[
		<b>
				<font color="#0000ff">JAVA FileUtil<br /><br /></font>
		</b>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> java.io.</span>
				<span style="color: rgb(0, 0, 0);">*</span>
				<span style="color: rgb(0, 0, 0);">;<br /><br /></span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />* FileUtil. Simple file operation class.<br />*<br />* </span>
				<span style="color: rgb(128, 128, 128);">@author</span>
				<span style="color: rgb(0, 128, 0);"> BeanSoft<br />*<br /></span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">class</span>
				<span style="color: rgb(0, 0, 0);"> FileUtil {<br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * The buffer.<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">protected</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">byte</span>
				<span style="color: rgb(0, 0, 0);"> buf[] </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">byte</span>
				<span style="color: rgb(0, 0, 0);">[</span>
				<span style="color: rgb(0, 0, 0);">1024</span>
				<span style="color: rgb(0, 0, 0);">];<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * Read content from local file. FIXME How to judge UTF-8 and GBK, the<br />     * correct code should be: FileReader fr = new FileReader(new<br />     * InputStreamReader(fileName, "ENCODING")); Might let the user select the<br />     * encoding would be a better idea. While reading UTF-8 files, the content<br />     * is bad when saved out.<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName -<br />     *            local file name to read<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> Exception<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> String readFileAsString(String fileName) </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> Exception {<br />        String content </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> String(readFileBinary(fileName));<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> content;<br />    }<br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * 读取文件并返回为给定字符集的字符串.<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> encoding<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> Exception<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> String readFileAsString(String fileName, String encoding) </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> Exception {<br />        String content </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> String(readFileBinary(fileName), encoding);<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> content;<br />    }<br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * 读取文件并返回为给定字符集的字符串.<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> encoding<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> Exception<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> String readFileAsString(InputStream in) </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> Exception {<br />        String content </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> String(readFileBinary(in));<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> content;<br />    }<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * Read content from local file to binary byte array.<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName -<br />     *            local file name to read<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> Exception<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">byte</span>
				<span style="color: rgb(0, 0, 0);">[] readFileBinary(String fileName) </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> Exception {<br />        FileInputStream fin </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> FileInputStream(fileName);<br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> readFileBinary(fin);<br />    }<br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * 从输入流读取数据为二进制字节数组.<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> streamIn<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> IOException<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">byte</span>
				<span style="color: rgb(0, 0, 0);">[] readFileBinary(InputStream streamIn) </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> IOException {<br />        BufferedInputStream in </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> BufferedInputStream(streamIn);<br /><br />        ByteArrayOutputStream out </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> ByteArrayOutputStream(</span>
				<span style="color: rgb(0, 0, 0);">10240</span>
				<span style="color: rgb(0, 0, 0);">);<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> len;<br />        </span>
				<span style="color: rgb(0, 0, 255);">while</span>
				<span style="color: rgb(0, 0, 0);"> ((len </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> in.read(buf)) </span>
				<span style="color: rgb(0, 0, 0);">&gt;=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">0</span>
				<span style="color: rgb(0, 0, 0);">)<br />            out.write(buf, </span>
				<span style="color: rgb(0, 0, 0);">0</span>
				<span style="color: rgb(0, 0, 0);">, len);<br />        in.close();<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> out.toByteArray();       <br />    }<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * Write string content to local file.<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName -<br />     *            local file name will write to<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> content<br />     *            String text<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);"> true if success<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> IOException<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">boolean</span>
				<span style="color: rgb(0, 0, 0);"> writeFileString(String fileName, String content)<br />            </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> IOException {<br />        FileWriter fout </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> FileWriter(fileName);<br />        fout.write(content);<br />        fout.close();<br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">true</span>
				<span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * Write string content to local file using given character encoding.<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName -<br />     *            local file name will write to<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> content<br />     *            String text<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> encoding<br />     *            the encoding<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);"> true if success<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> IOException<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">boolean</span>
				<span style="color: rgb(0, 0, 0);"> writeFileString(String fileName, String content,<br />            String encoding) </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> IOException {<br />        OutputStreamWriter fout </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> OutputStreamWriter(</span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> FileOutputStream(<br />                fileName), encoding);<br /><br />        fout.write(content);<br />        fout.close();<br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">true</span>
				<span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * Write binary byte array to local file.<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName -<br />     *            local file name will write to<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> content<br />     *            binary byte array<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);"> true if success<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> IOException<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">boolean</span>
				<span style="color: rgb(0, 0, 0);"> writeFileBinary(String fileName, </span>
				<span style="color: rgb(0, 0, 255);">byte</span>
				<span style="color: rgb(0, 0, 0);">[] content)<br />            </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> IOException {<br />        FileOutputStream fout </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> FileOutputStream(fileName);<br />        fout.write(content);<br />        fout.close();<br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">true</span>
				<span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * 检查文件名是否合法.文件名字不能包含字符\/:*?"&lt;&gt;|<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName文件名,不包含路径<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);"> boolean is valid file name<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">boolean</span>
				<span style="color: rgb(0, 0, 0);"> isValidFileName(String fileName) {<br />        </span>
				<span style="color: rgb(0, 0, 255);">boolean</span>
				<span style="color: rgb(0, 0, 0);"> isValid </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">true</span>
				<span style="color: rgb(0, 0, 0);">;<br />        String errChar </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">\\/:*?\</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">&lt;&gt;|</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">; //</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">        </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);"> (fileName </span>
				<span style="color: rgb(0, 0, 0);">==</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">null</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">||</span>
				<span style="color: rgb(0, 0, 0);"> fileName.length() </span>
				<span style="color: rgb(0, 0, 0);">==</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">0</span>
				<span style="color: rgb(0, 0, 0);">) {<br />            isValid </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">false</span>
				<span style="color: rgb(0, 0, 0);">;<br />        } </span>
				<span style="color: rgb(0, 0, 255);">else</span>
				<span style="color: rgb(0, 0, 0);"> {<br />            </span>
				<span style="color: rgb(0, 0, 255);">for</span>
				<span style="color: rgb(0, 0, 0);"> (</span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> i </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">0</span>
				<span style="color: rgb(0, 0, 0);">; i </span>
				<span style="color: rgb(0, 0, 0);">&lt;</span>
				<span style="color: rgb(0, 0, 0);"> errChar.length(); i</span>
				<span style="color: rgb(0, 0, 0);">++</span>
				<span style="color: rgb(0, 0, 0);">) {<br />                </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);"> (fileName.indexOf(errChar.charAt(i)) </span>
				<span style="color: rgb(0, 0, 0);">!=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">-</span>
				<span style="color: rgb(0, 0, 0);">1</span>
				<span style="color: rgb(0, 0, 0);">) {<br />                    isValid </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">false</span>
				<span style="color: rgb(0, 0, 0);">;<br />                    </span>
				<span style="color: rgb(0, 0, 255);">break</span>
				<span style="color: rgb(0, 0, 0);">;<br />                }<br />            }<br />        }<br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> isValid;<br />    }<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * 把非法文件名转换为合法文件名.<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> String replaceInvalidFileChars(String fileName) {<br />        StringBuffer out </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> StringBuffer();<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">for</span>
				<span style="color: rgb(0, 0, 0);"> (</span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> i </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">0</span>
				<span style="color: rgb(0, 0, 0);">; i </span>
				<span style="color: rgb(0, 0, 0);">&lt;</span>
				<span style="color: rgb(0, 0, 0);"> fileName.length(); i</span>
				<span style="color: rgb(0, 0, 0);">++</span>
				<span style="color: rgb(0, 0, 0);">) {<br />            </span>
				<span style="color: rgb(0, 0, 255);">char</span>
				<span style="color: rgb(0, 0, 0);"> ch </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> fileName.charAt(i);<br />            </span>
				<span style="color: rgb(0, 128, 0);">//</span>
				<span style="color: rgb(0, 128, 0);"> Replace invlid chars: \\/:*?\"&lt;&gt;|</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">            </span>
				<span style="color: rgb(0, 0, 255);">switch</span>
				<span style="color: rgb(0, 0, 0);"> (ch) {<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">\\</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">/</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">*</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">?</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">\"</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">&lt;</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">|</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />                out.append(</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">_</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">);<br />                </span>
				<span style="color: rgb(0, 0, 255);">break</span>
				<span style="color: rgb(0, 0, 0);">;<br />            </span>
				<span style="color: rgb(0, 0, 255);">default</span>
				<span style="color: rgb(0, 0, 0);">:<br />                out.append(ch);<br />            }<br />        }<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> out.toString();<br />    }<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * Convert a given file name to a URL(URI) string.<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName -<br />     *            the file to parse<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);"> - URL string<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> String filePathToURL(String fileName) {<br />        String fileUrl </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> File(fileName).toURI().toString();<br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> fileUrl;<br />    }<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * Write string content to local file.<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName -<br />     *            local file name will write to<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> content<br />     *            String text<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);"> true if success<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> IOException<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">boolean</span>
				<span style="color: rgb(0, 0, 0);"> appendFileString(String fileName, String content)<br />            </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> IOException {<br />        OutputStreamWriter fout </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> OutputStreamWriter(</span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> FileOutputStream(<br />                fileName, </span>
				<span style="color: rgb(0, 0, 255);">true</span>
				<span style="color: rgb(0, 0, 0);">), </span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">GBK</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">);<br /><br />        fout.write(content);<br />        fout.close();<br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">true</span>
				<span style="color: rgb(0, 0, 0);">;<br />    }<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">void</span>
				<span style="color: rgb(0, 0, 0);"> main(String[] args) {<br />        System.out.println(replaceInvalidFileChars(</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">http://www.abc.com/</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">));<br />    }<br />}</span>
		</div>
		<br />
		<br />
		<br />
<img src ="http://www.blogjava.net/Collus/aggbug/320747.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Collus/" target="_blank">良帅</a> 2010-05-12 22:03 <a href="http://www.blogjava.net/Collus/articles/320747.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Oracle 打开游标超过最大数问题</title><link>http://www.blogjava.net/Collus/articles/318562.html</link><dc:creator>良帅</dc:creator><author>良帅</author><pubDate>Fri, 16 Apr 2010 12:37:00 GMT</pubDate><guid>http://www.blogjava.net/Collus/articles/318562.html</guid><wfw:comment>http://www.blogjava.net/Collus/comments/318562.html</wfw:comment><comments>http://www.blogjava.net/Collus/articles/318562.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Collus/comments/commentRss/318562.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Collus/services/trackbacks/318562.html</trackback:ping><description><![CDATA[Java开发中，使用Oracle数据库的时候，经常会碰到有ORA-01000: maximum open cursors exceeded.的错误。 <br /><br />
实际上，这个错误的原因，主要还是代码问题引起的。 <br />
ORA-01000: maximum open cursors exceeded，表示已经达到一个进程打开的最大游标数。 <br /><br /><strong><font size="3" color="#ff0000"><span style="color: rgb(7, 22, 240);">   这样的错误很容易出现在Java代码中的主要原因是：Java代码在执
行conn.createStatement()和conn.prepareStatement()的时候，实际上都是相当与在数据库中打开了一个
cursor。尤其是，如果你的createStatement和prepareStatement是在一个循环里面的话，就会非常容易出现这个问题。因
为游标一直在不停的打开，而且没有关闭。</span><br /><br /></font></strong>一般来说，在写Java代码的时候，createStatement和prepareStatement都应该要放在循环外面，而且使用了这些
Statment后，及时关闭。最好是在执行了一次executeQuery、executeUpdate等之后，如果不需要使用结果集
（ResultSet）的数据，就马上将Statment关闭。 <br /><br />
对于出现ORA-01000错误这种情况，单纯的加大open_cursors并不是好办法，那只是治标不治本。实际上，代码中的隐患并没有解除。 <br />
而且，绝大部分情况下，open_cursors只需要设置一个比较小的值，就足够使用了，除非有非常特别的要求。
<p>oracle 9i 默认的open_cursors=300  </p><p><br /></p><p><span style="font-size: 24pt;"><strong><span style="color: rgb(39, 8, 255);"><span style="font-size: 18pt;">一、看有问题的代码</span></span></strong></span></p><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.sql.Connection;<br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.sql.DriverManager;<br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.sql.SQLException;<br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.sql.Statement;<br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Test {<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Connection getConnection() {<br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">    String url </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">jdbc:oracle:thin:@localhost:1521:ora9i</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    String user </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">scott</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">    String password </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">tiger</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">    Connection con </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">      Class.forName(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">oracle.jdbc.driver.OracleDriver</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">).newInstance();<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">      con </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> DriverManager.getConnection(url, user, password);<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">    } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception e) {<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">      e.printStackTrace();<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">    }<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> con;<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">  }<br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">  </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main(String[] args) </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> SQLException {<br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> a </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">13819100000L</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">13819100600L</span><span style="color: rgb(0, 0, 0);">; </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 问题点</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">    Connection con </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">25</span> <span style="color: rgb(0, 0, 0);">    Statement stmt </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">26</span> <span style="color: rgb(0, 0, 0);">    Test insert </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Test();<br /></span><span style="color: rgb(0, 128, 128);">27</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br /></span><span style="color: rgb(0, 128, 128);">28</span> <span style="color: rgb(0, 0, 0);">      con </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> insert.getConnection();<br /></span><span style="color: rgb(0, 128, 128);">29</span> <span style="color: rgb(0, 0, 0);">      </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> a; c </span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);"> b; c</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">) {<br /></span><span style="color: rgb(0, 128, 128);">30</span> <span style="color: rgb(0, 0, 0);">        String sql </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">insert into telepnum values(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> c </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">31</span> <span style="color: rgb(0, 0, 0);">        stmt </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> con.createStatement(); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 这里是问题的所在</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">32</span> <span style="color: rgb(0, 0, 0);">        stmt.executeUpdate(sql);<br /></span><span style="color: rgb(0, 128, 128);">33</span> <span style="color: rgb(0, 0, 0);">      }<br /></span><span style="color: rgb(0, 128, 128);">34</span> <span style="color: rgb(0, 0, 0);">      System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">OK</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">35</span> <span style="color: rgb(0, 0, 0);">    } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception e) {<br /></span><span style="color: rgb(0, 128, 128);">36</span> <span style="color: rgb(0, 0, 0);">      e.printStackTrace();<br /></span><span style="color: rgb(0, 128, 128);">37</span> <span style="color: rgb(0, 0, 0);">    } </span><span style="color: rgb(0, 0, 255);">finally</span><span style="color: rgb(0, 0, 0);"> {<br /></span><span style="color: rgb(0, 128, 128);">38</span> <span style="color: rgb(0, 0, 0);">      </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (con </span><span style="color: rgb(0, 0, 0);">!=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">) {<br /></span><span style="color: rgb(0, 128, 128);">39</span> <span style="color: rgb(0, 0, 0);">        con.close();<br /></span><span style="color: rgb(0, 128, 128);">40</span> <span style="color: rgb(0, 0, 0);">      }<br /></span><span style="color: rgb(0, 128, 128);">41</span> <span style="color: rgb(0, 0, 0);">    }<br /></span><span style="color: rgb(0, 128, 128);">42</span> <span style="color: rgb(0, 0, 0);">  }<br /></span><span style="color: rgb(0, 128, 128);">43</span> <span style="color: rgb(0, 0, 0);">}</span></div><br /><span style="font-size: 24pt;"><strong><span style="color: rgb(39, 8, 255);"><span style="font-size: 18pt;">二、分析</span></span></strong></span><br /><br />
在循环里面每次都
<strong><span style="color: rgb(7, 22, 240);"><span style="font-size: 18pt;"><strong><span style="color: rgb(7, 22, 240);">stmt = con.createStatement();</span></strong></span></span></strong>
而没有释放，这样每个都占用了一个服务器的游标资源，最后造成失败
<br /><br /><span style="font-size: 24pt;"><strong><span style="color: rgb(39, 8, 255);"><span style="font-size: 18pt;">三、解决方案</span></span></strong></span><br /><br />
1、增加关闭语句
<br />
 <br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 128);">1</span> <span style="color: rgb(0, 0, 0);">  con </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> insert.getConnection();<br /></span><span style="color: rgb(0, 128, 128);">2</span> <span style="color: rgb(0, 0, 0);">      </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> a; c </span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);"> b; c</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">) {<br /></span><span style="color: rgb(0, 128, 128);">3</span> <span style="color: rgb(0, 0, 0);">        String sql </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">insert into telepnum values(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> c </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">4</span> <span style="color: rgb(0, 0, 0);">        stmt </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> con.createStatement(); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 这里是问题的所在</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">5</span> <span style="color: rgb(0, 0, 0);">        stmt.executeUpdate(sql);<br /></span><span style="color: rgb(0, 128, 128);">6</span> <span style="color: rgb(0, 0, 0);">        stmt.close(); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 用完了就关闭好了</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">7</span> <span style="color: rgb(0, 0, 0);">      }</span></div><br />
2、将这句话移动到循环外面，推荐用这个<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 128);">1</span> <span style="color: rgb(0, 0, 0);">con </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> insert.getConnection();<br /></span><span style="color: rgb(0, 128, 128);">2</span> <span style="color: rgb(0, 0, 0);">      stmt </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> con.createStatement(); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 移动到这里，Statemet是可以重用的</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">3</span> <span style="color: rgb(0, 0, 0);">      </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> a; c </span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);"> b; c</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">) {<br /></span><span style="color: rgb(0, 128, 128);">4</span> <span style="color: rgb(0, 0, 0);">        String sql </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">insert into telepnum values(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> c </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">5</span> <span style="color: rgb(0, 0, 0);">        stmt.executeUpdate(sql);<br /></span><span style="color: rgb(0, 128, 128);">6</span> <span style="color: rgb(0, 0, 0);">      }<br /></span><span style="color: rgb(0, 128, 128);">7</span> <span style="color: rgb(0, 0, 0);">      stmt.close(); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 用完了就关闭好了</span></div><br />
3、改装成批量更新<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: rgb(0, 128, 128);">1</span> <span style="color: rgb(0, 0, 0);">con </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> insert.getConnection();<br /></span><span style="color: rgb(0, 128, 128);">2</span> <span style="color: rgb(0, 0, 0);">      con.setAutoCommit(</span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">3</span> <span style="color: rgb(0, 0, 0);">      stmt </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> con.createStatement(); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 移动到这里，Statemet是可以重用的</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">4</span> <span style="color: rgb(0, 0, 0);">      </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> a; c </span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);"> b; c</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">) {<br /></span><span style="color: rgb(0, 128, 128);">5</span> <span style="color: rgb(0, 0, 0);">        String sql </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">insert into telepnum values(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> c </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">6</span> <span style="color: rgb(0, 0, 0);">        stmt.addBatch(sql);<br /></span><span style="color: rgb(0, 128, 128);">7</span> <span style="color: rgb(0, 0, 0);">      }<br /></span><span style="color: rgb(0, 128, 128);">8</span> <span style="color: rgb(0, 0, 0);">      stmt.executeBatch();<br /></span><span style="color: rgb(0, 128, 128);">9</span> <span style="color: rgb(0, 0, 0);">      con.commit();</span></div><br /><br /><span style="font-size: 24pt;"><strong><span style="color: rgb(39, 8, 255);"><span style="font-size: 18pt;">四、总结</span></span></strong></span><br /><br />
    鉴于上面的问题，在做基类的时候，在对数据进行DML操作的时候，尽量不要让基类返回Statement,而应该在基类直接进行关闭。在做查询的时候，可
以把statement留给程序员自己进行手动关闭，关闭的方法为：给ResultSet一个方法可以得到Statement，然后再关闭
Statement。个人认为这种方法是比较妥当的。<br /><img src ="http://www.blogjava.net/Collus/aggbug/318562.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Collus/" target="_blank">良帅</a> 2010-04-16 20:37 <a href="http://www.blogjava.net/Collus/articles/318562.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JAVA 时间操作大全</title><link>http://www.blogjava.net/Collus/articles/318152.html</link><dc:creator>良帅</dc:creator><author>良帅</author><pubDate>Tue, 13 Apr 2010 05:22:00 GMT</pubDate><guid>http://www.blogjava.net/Collus/articles/318152.html</guid><wfw:comment>http://www.blogjava.net/Collus/comments/318152.html</wfw:comment><comments>http://www.blogjava.net/Collus/articles/318152.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Collus/comments/commentRss/318152.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Collus/services/trackbacks/318152.html</trackback:ping><description><![CDATA[
		<div align="center">
				<font style="background-color: rgb(255, 255, 255);">
						<font color="#ff0000">
								<span style="color: rgb(8, 116, 255);">
										<span style="font-size: 24pt;">
												<span style="font-family: 黑体;">Java 时间操作大全</span>
										</span>
								</span>
						</font>
				</font>
				<br />
				<font style="background-color: rgb(255, 255, 255);">
						<font color="#ff0000">
						</font>
				</font>
		</div>
		<font style="background-color: rgb(255, 255, 255);">
				<font color="#ff0000">
						<span style="color: rgb(8, 116, 255);">
								<br />
函数列表：<br /></span>
				</font>
		</font>
		<br />
		<font style="background-color: rgb(255, 255, 255);">
				<font color="#ff0000">
						<span style="color: rgb(8, 116, 255);">
								<br />
/**<br />
    * 获取现在时间<br />
    * <br />
    * @return 返回时间类型 <font style="background-color: rgb(255, 255, 0);">yyyy-MM-dd HH:mm:ss<br /></font>    */</span>
				</font>
				<br />
		</font>public static Date getNowDate() {<br />
    Date currentTime = new Date();<br />
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");<br />
    String dateString = formatter.format(currentTime);<br />
    ParsePosition pos = new ParsePosition(8);<br />
    Date currentTime_2 = formatter.parse(dateString, pos);<br />
    return currentTime_2;<br />
}<br /><br /><font style="background-color: rgb(255, 255, 255);"><font color="#ff0000"><span style="color: rgb(8, 116, 255);">/**<br />
    * 获取现在时间<br />
    * <br />
    * @return返回短时间格式 <font style="background-color: rgb(255, 255, 0);">yyyy-MM-dd</font><br />
    */</span></font><br /></font>public static Date getNowDateShort() {<br />
    Date currentTime = new Date();<br />
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");<br />
    String dateString = formatter.format(currentTime);<br />
    ParsePosition pos = new ParsePosition(8);<br />
    Date currentTime_2 = formatter.parse(dateString, pos);<br />
    return currentTime_2;<br />
}<br /><br /><p><font color="#ff0000"><span style="color: rgb(8, 116, 255);">/**<br />
    * 获取现在时间<br />
    * <br />
    * @return返回字符串格式 <font style="background-color: rgb(255, 255, 0);">yyyy-MM-dd HH:mm:ss</font><br />
    */</span><br /></font>public static String getStringDate() {<br />
    Date currentTime = new Date();<br />
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");<br />
    String dateString = formatter.format(currentTime);<br />
    return dateString;<br />
}</p><p><font color="#ff0000">/**<br />
    * 获取现在时间<br />
    * <br />
    * @return 返回短时间字符串格式<font style="background-color: rgb(255, 255, 0);">yyyy-MM-dd</font><br />
    */</font><br />
public static String getStringDateShort() {<br />
    Date currentTime = new Date();<br />
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");<br />
    String dateString = formatter.format(currentTime);<br />
    return dateString;<br />
}</p><p><font color="#ff0000">/**<br />
    * 获取时间 小时:分;秒 <font style="background-color: rgb(255, 255, 0);">HH:mm:ss</font><br />
    * <br />
    * @return<br />
    */</font><br />
public static String getTimeShort() {<br />
    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");<br />
    Date currentTime = new Date();<br />
    String dateString = formatter.format(currentTime);<br />
    return dateString;<br />
}</p><p><font color="#ff0000">/**<br />
    * 将长时间格式字符串转换为时间 <font style="background-color: rgb(255, 255, 0);">yyyy-MM-dd HH:mm:ss</font><br />
    * <br />
    * @param strDate<br />
    * @return<br />
    */<br /></font>public static Date strToDateLong(String strDate) {<br />
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");<br />
    ParsePosition pos = new ParsePosition(0);<br />
    Date strtodate = formatter.parse(strDate, pos);<br />
    return strtodate;<br />
}</p><p><font color="#ff0000">/**<br />
    * 将长时间格式时间转换为字符串 <font style="background-color: rgb(255, 255, 0);">yyyy-MM-dd HH:mm:ss<br /></font>    * <br />
    * @param dateDate<br />
    * @return<br />
    */</font><br />
public static String dateToStrLong(java.util.Date dateDate) {<br />
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");<br />
    String dateString = formatter.format(dateDate);<br />
    return dateString;<br />
}</p><p><font color="#ff0000">/**<br />
    * 将短时间格式时间转换为字符串 <font style="background-color: rgb(255, 255, 0);">yyyy-MM-dd</font><br />
    * <br />
    * @param dateDate<br />
    * @param k<br />
    * @return<br />
    */<br /></font>public static String dateToStr(java.util.Date dateDate) {<br />
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");<br />
    String dateString = formatter.format(dateDate);<br />
    return dateString;<br />
}</p><p><font color="#ff0000">/**<br />
    * 将短时间格式字符串转换为时间 <font style="background-color: rgb(255, 255, 0);">yyyy-MM-dd</font><br />
    * <br />
    * @param strDate<br />
    * @return<br />
    */</font><br />
public static Date strToDate(String strDate) {<br />
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");<br />
    ParsePosition pos = new ParsePosition(0);<br />
    Date strtodate = formatter.parse(strDate, pos);<br />
    return strtodate;<br />
}</p><p><font color="#ff0000">/**<br />
    * 得到现在时间<br />
    * <br />
    * @return<br />
    */</font><br />
public static Date getNow() {<br />
    Date currentTime = new Date();<br />
    return currentTime;<br />
}</p><p><font color="#ff0000">/**<br />
    * 提取一个月中的最后一天<br />
    * <br />
    * @param day<br />
    * @return<br />
    */</font><br />
public static Date getLastDate(long day) {<br />
    Date date = new Date();<br />
    long date_3_hm = date.getTime() - 3600000 * 34 * day;<br />
    Date date_3_hm_date = new Date(date_3_hm);<br />
    return date_3_hm_date;<br />
}</p><p><font color="#ff0000">/**<br />
    * 得到现在时间<br />
    * <br />
    * @return 字符串 yyyyMMdd HHmmss<br />
    */</font><br />
public static String getStringToday() {<br />
    Date currentTime = new Date();<br />
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");<br />
    String dateString = formatter.format(currentTime);<br />
    return dateString;<br />
}</p><p><font color="#ff0000">/**<br />
    * 得到现在小时<br />
    */</font><br />
public static String getHour() {<br />
    Date currentTime = new Date();<br />
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");<br />
    String dateString = formatter.format(currentTime);<br />
    String hour;<br />
    hour = dateString.substring(11, 13);<br />
    return hour;<br />
}</p><p><font color="#ff0000">/**<br />
    * 得到现在分钟<br />
    * <br />
    * @return<br />
    */</font><br />
public static String getTime() {<br />
    Date currentTime = new Date();<br />
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");<br />
    String dateString = formatter.format(currentTime);<br />
    String min;<br />
    min = dateString.substring(14, 16);<br />
    return min;<br />
}</p><p><font color="#ff0000">/**<br />
    * 根据用户传入的时间表示格式，返回当前时间的格式 如果是yyyyMMdd，注意字母y不能大写。<br />
    * <br />
    * @param sformat<br />
    *              <font color="#ffff00">y</font><font style="background-color: rgb(255, 255, 0);" color="#ff0000">yyyMMddhhmmss<br /></font>    * @return<br />
    */<br /></font>public static String getUserDate(String sformat) {<br />
    Date currentTime = new Date();<br />
    SimpleDateFormat formatter = new SimpleDateFormat(sformat);<br />
    String dateString = formatter.format(currentTime);<br />
    return dateString;<br />
}</p><p><font color="#ff0000">/**<br />
    * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式，返回字符型的分钟<br />
    */</font><br />
public static String getTwoHour(String st1, String st2) {<br />
    String[] kk = null;<br />
    String[] jj = null;<br />
    kk = st1.split(":");<br />
    jj = st2.split(":");<br />
    if (Integer.parseInt(kk[0]) &lt; Integer.parseInt(jj[0]))<br />
     return "0";<br />
    else {<br />
     double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1]) / 60;<br />
     double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1]) / 60;<br />
     if ((y - u) &gt; 0)<br />
      return y - u + "";<br />
     else<br />
      return "0";<br />
    }<br />
}</p><p><font color="#ff0000">/**<br />
    * 得到二个日期间的间隔天数<br />
    */</font><br />
public static String getTwoDay(String sj1, String sj2) {<br />
    SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");<br />
    long day = 0;<br />
    try {<br />
     java.util.Date date = myFormatter.parse(sj1);<br />
     java.util.Date mydate = myFormatter.parse(sj2);<br />
     day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);<br />
    } catch (Exception e) {<br />
     return "";<br />
    }<br />
    return day + "";<br />
}</p><p><font color="#ff0000">/**<br />
    * 时间前推或后推分钟,其中JJ表示分钟.<br />
    */</font><br />
public static String getPreTime(String sj1, String jj) {<br />
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");<br />
    String mydate1 = "";<br />
    try {<br />
     Date date1 = format.parse(sj1);<br />
     long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;<br />
     date1.setTime(Time * 1000);<br />
     mydate1 = format.format(date1);<br />
    } catch (Exception e) {<br />
    }<br />
    return mydate1;<br />
}</p><p><font color="#ff0000">/**<br />
    * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数<br />
    */</font><br />
public static String getNextDay(String nowdate, String delay) {<br />
    try{<br />
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");<br />
    String mdate = "";<br />
    Date d = strToDate(nowdate);<br />
    long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60;<br />
    d.setTime(myTime * 1000);<br />
    mdate = format.format(d);<br />
    return mdate;<br />
    }catch(Exception e){<br />
     return "";<br />
    }<br />
}</p><p><font color="#ff0000">/**<br />
    * 判断是否润年<br />
    * <br />
    * @param ddate<br />
    * @return<br />
    */</font><br />
public static boolean isLeapYear(String ddate) {</p><p><font color="#ff0000">    /**<br />
     * 详细设计： 1.被400整除是闰年，否则： 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年<br />
     * 3.能被4整除同时能被100整除则不是闰年<br />
     */</font><br />
    Date d = strToDate(ddate);<br />
    GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();<br />
    gc.setTime(d);<br />
    int year = gc.get(Calendar.YEAR);<br />
    if ((year % 400) == 0)<br />
     return true;<br />
    else if ((year % 4) == 0) {<br />
     if ((year % 100) == 0)<br />
      return false;<br />
     else<br />
      return true;<br />
    } else<br />
     return false;<br />
}</p><p><font color="#ff0000">/**<br />
    * 返回美国时间格式 26 Apr 2006<br />
    * <br />
    * @param str<br />
    * @return<br />
    */</font><br />
public static String getEDate(String str) {<br />
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");<br />
    ParsePosition pos = new ParsePosition(0);<br />
    Date strtodate = formatter.parse(str, pos);<br />
    String j = strtodate.toString();<br />
    String[] k = j.split(" ");<br />
    return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);<br />
}</p><p><font color="#ff0000">/**<br />
    * 获取一个月的最后一天<br />
    * <br />
    * @param dat<br />
    * @return<br />
    */<br /></font>public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd<br />
    String str = dat.substring(0, 8);<br />
    String month = dat.substring(5, 7);<br />
    int mon = Integer.parseInt(month);<br />
    if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {<br />
     str += "31";<br />
    } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {<br />
     str += "30";<br />
    } else {<br />
     if (isLeapYear(dat)) {<br />
      str += "29";<br />
     } else {<br />
      str += "28";<br />
     }<br />
    }<br />
    return str;<br />
}</p><p><font color="#ff0000">/**<br />
    * 判断二个时间是否在同一个周<br />
    * <br />
    * @param date1<br />
    * @param date2<br />
    * @return<br />
    */</font><br />
public static boolean isSameWeekDates(Date date1, Date date2) {<br />
    Calendar cal1 = Calendar.getInstance();<br />
    Calendar cal2 = Calendar.getInstance();<br />
    cal1.setTime(date1);<br />
    cal2.setTime(date2);<br />
    int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);<br />
    if (0 == subYear) {<br />
     if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))<br />
      return true;<br />
    } else if (1 == subYear &amp;&amp; 11 == cal2.get(Calendar.MONTH)) {<br />
     // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周<br />
     if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))<br />
      return true;<br />
    } else if (-1 == subYear &amp;&amp; 11 == cal1.get(Calendar.MONTH)) {<br />
     if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))<br />
      return true;<br />
    }<br />
    return false;<br />
}</p><p><font color="#ff0000">/**<br />
    * 产生周序列,即得到当前时间所在的年度是第几周<br />
    * <br />
    * @return<br />
    */</font><br />
public static String getSeqWeek() {<br />
    Calendar c = Calendar.getInstance(Locale.CHINA);<br />
    String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));<br />
    if (week.length() == 1)<br />
     week = "0" + week;<br />
    String year = Integer.toString(c.get(Calendar.YEAR));<br />
    return year + week;<br />
}</p><p><font color="#ff0000">/**<br />
    * 获得一个日期所在的周的星期几的日期，如要找出2002年2月3日所在周的星期一是几号<br />
    * <br />
    * @param sdate<br />
    * @param num<br />
    * @return<br />
    */<br /></font>public static String getWeek(String sdate, String num) {<br />
    // 再转换为时间<br />
    Date dd = VeDate.strToDate(sdate);<br />
    Calendar c = Calendar.getInstance();<br />
    c.setTime(dd);<br />
    if (num.equals("1")) // 返回星期一所在的日期<br />
     c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);<br />
    else if (num.equals("2")) // 返回星期二所在的日期<br />
     c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);<br />
    else if (num.equals("3")) // 返回星期三所在的日期<br />
     c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);<br />
    else if (num.equals("4")) // 返回星期四所在的日期<br />
     c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);<br />
    else if (num.equals("5")) // 返回星期五所在的日期<br />
     c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);<br />
    else if (num.equals("6")) // 返回星期六所在的日期<br />
     c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);<br />
    else if (num.equals("0")) // 返回星期日所在的日期<br />
     c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);<br />
    return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());<br />
}</p><p><font color="#ff0000">/**<br />
    * 根据一个日期，返回是星期几的字符串<br />
    * <br />
    * @param sdate<br />
    * @return<br />
    */</font><br />
public static String getWeek(String sdate) {<br />
    // 再转换为时间<br />
    Date date = VeDate.strToDate(sdate);<br />
    Calendar c = Calendar.getInstance();<br />
    c.setTime(date);<br />
    // int hour=c.get(Calendar.DAY_OF_WEEK);<br />
    // hour中存的就是星期几了，其范围 1~7<br />
    // 1=星期日 7=星期六，其他类推<br />
    return new SimpleDateFormat("EEEE").format(c.getTime());<br />
}<br />
public static String getWeekStr(String sdate){<br />
    String str = "";<br />
    str = VeDate.getWeek(sdate);<br />
    if("1".equals(str)){<br />
     str = "星期日";<br />
    }else if("2".equals(str)){<br />
     str = "星期一";<br />
    }else if("3".equals(str)){<br />
     str = "星期二";<br />
    }else if("4".equals(str)){<br />
     str = "星期三";<br />
    }else if("5".equals(str)){<br />
     str = "星期四";<br />
    }else if("6".equals(str)){<br />
     str = "星期五";<br />
    }else if("7".equals(str)){<br />
     str = "星期六";<br />
    }<br />
    return str;<br />
}</p><p><font color="#ff0000">/**<br />
    * 两个时间之间的天数<br />
    * <br />
    * @param date1<br />
    * @param date2<br />
    * @return<br />
    */</font><br />
public static long getDays(String date1, String date2) {<br />
    if (date1 == null || date1.equals(""))<br />
     return 0;<br />
    if (date2 == null || date2.equals(""))<br />
     return 0;<br />
    // 转换为标准时间<br />
    SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");<br />
    java.util.Date date = null;<br />
    java.util.Date mydate = null;<br />
    try {<br />
     date = myFormatter.parse(date1);<br />
     mydate = myFormatter.parse(date2);<br />
    } catch (Exception e) {<br />
    }<br />
    long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);<br />
    return day;<br />
}</p><p><font color="#ff0000">/**<br />
    * 形成如下的日历 ， 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间<br />
    * 此函数返回该日历第一行星期日所在的日期<br />
    * <br />
    * @param sdate<br />
    * @return<br />
    */<br /></font>public static String getNowMonth(String sdate) {<br />
    // 取该时间所在月的一号<br />
    sdate = sdate.substring(0, 8) + "01";</p><p>    // 得到这个月的1号是星期几<br />
    Date date = VeDate.strToDate(sdate);<br />
    Calendar c = Calendar.getInstance();<br />
    c.setTime(date);<br />
    int u = c.get(Calendar.DAY_OF_WEEK);<br />
    String newday = VeDate.getNextDay(sdate, (1 - u) + "");<br />
    return newday;<br />
}</p><p><font color="#ff0000">/**<br />
    * 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数<br />
    * <br />
    * @param k<br />
    *              表示是取几位随机数，可以自己定<br />
    */</font></p><p>public static String getNo(int k) {</p><p>    return getUserDate("yyyyMMddhhmmss") + getRandom(k);<br />
}</p><p><font color="#ff0000">/**<br />
    * 返回一个随机数<br />
    * <br />
    * @param i<br />
    * @return<br />
    */</font><br />
public static String getRandom(int i) {<br />
    Random jjj = new Random();<br />
    // int suiJiShu = jjj.nextInt(9);<br />
    if (i == 0)<br />
     return "";<br />
    String jj = "";<br />
    for (int k = 0; k &lt; i; k++) {<br />
     jj = jj + jjj.nextInt(9);<br />
    }<br />
    return jj;<br />
}</p><p><br /></p><br /><br /><img src ="http://www.blogjava.net/Collus/aggbug/318152.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Collus/" target="_blank">良帅</a> 2010-04-13 13:22 <a href="http://www.blogjava.net/Collus/articles/318152.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>