??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲福利一区二区精品秒拍,亚洲国产综合91精品麻豆,亚洲精品欧洲精品http://www.tkk7.com/swzhebei/zh-cnSun, 11 May 2025 15:56:09 GMTSun, 11 May 2025 15:56:09 GMT60转蝲QSpring MVC 3 深入ȝhttp://www.tkk7.com/swzhebei/archive/2013/07/19/401758.html透明的鱼透明的鱼Fri, 19 Jul 2013 07:57:00 GMThttp://www.tkk7.com/swzhebei/archive/2013/07/19/401758.htmlhttp://www.tkk7.com/swzhebei/comments/401758.htmlhttp://www.tkk7.com/swzhebei/archive/2013/07/19/401758.html#Feedback0http://www.tkk7.com/swzhebei/comments/commentRss/401758.htmlhttp://www.tkk7.com/swzhebei/services/trackbacks/401758.html阅读全文

透明的鱼 2013-07-19 15:57 发表评论
]]>
删除数据库中重复的记?/title><link>http://www.tkk7.com/swzhebei/archive/2012/12/05/392502.html</link><dc:creator>透明的鱼</dc:creator><author>透明的鱼</author><pubDate>Wed, 05 Dec 2012 08:30:00 GMT</pubDate><guid>http://www.tkk7.com/swzhebei/archive/2012/12/05/392502.html</guid><wfw:comment>http://www.tkk7.com/swzhebei/comments/392502.html</wfw:comment><comments>http://www.tkk7.com/swzhebei/archive/2012/12/05/392502.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/swzhebei/comments/commentRss/392502.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/swzhebei/services/trackbacks/392502.html</trackback:ping><description><![CDATA[<p>1.delete from 表名 where ID in (select id from 表名 group by id having count(id) >1)</p> <p>  and rowid not in (select min(rowid) from 表名 group by id having count(id )>1)<br /></p><img src ="http://www.tkk7.com/swzhebei/aggbug/392502.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/swzhebei/" target="_blank">透明的鱼</a> 2012-12-05 16:30 <a href="http://www.tkk7.com/swzhebei/archive/2012/12/05/392502.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用POI对excel表数据进行操作时出现了该问题http://www.tkk7.com/swzhebei/archive/2012/09/25/388476.html透明的鱼透明的鱼Tue, 25 Sep 2012 01:48:00 GMThttp://www.tkk7.com/swzhebei/archive/2012/09/25/388476.htmlhttp://www.tkk7.com/swzhebei/comments/388476.htmlhttp://www.tkk7.com/swzhebei/archive/2012/09/25/388476.html#Feedback0http://www.tkk7.com/swzhebei/comments/commentRss/388476.htmlhttp://www.tkk7.com/swzhebei/services/trackbacks/388476.html     解决办法Q?/p>

       判断d的单元格是否为HSSFCell.CELL_TYPE_NUMERICcdQ然后利用cell.getNumericCellValue()Q读取该单元格的数据?/p>

    getNumericCellValue()d的数据类型是double型,

   因此Q需要重新进行数据{换:HSSFDateUtil.getJavaDate(d).toLocaleString()。其中d出的doublecd数据?/p>

 xQ则成功的将excel表内的数据读取出来?/p>

 

单元格的格式d有以下几U:

HSSFCell.CELL_TYPE_BLANK;
HSSFCell.CELL_TYPE_BOOLEAN;
HSSFCell.CELL_TYPE_ERROR;
HSSFCell.CELL_TYPE_FORMULA;
HSSFCell.CELL_TYPE_NUMERIC;
HSSFCell.CELL_TYPE_STRING;



透明的鱼 2012-09-25 09:48 发表评论
]]>
转蝲Q正则表辑ּ学习---重复匚w http://www.tkk7.com/swzhebei/archive/2012/08/15/385550.html透明的鱼透明的鱼Wed, 15 Aug 2012 09:37:00 GMThttp://www.tkk7.com/swzhebei/archive/2012/08/15/385550.htmlhttp://www.tkk7.com/swzhebei/comments/385550.htmlhttp://www.tkk7.com/swzhebei/archive/2012/08/15/385550.html#Feedback0http://www.tkk7.com/swzhebei/comments/commentRss/385550.htmlhttp://www.tkk7.com/swzhebei/services/trackbacks/385550.htmlhttp://www.tkk7.com/zhanghu198901/archive/2012/08/12/385337.html

PSQ在所有例子中正则表达式匹配结果包含在源文本中的【和】之_有的例子会用java来实玎ͼ如果是java本n正则表达式的用法Q会在相应的地方说明。所有java例子都在JDK1.6.0_13下测试通过?/span>

 

一、有多少个匹?/span>

         前面几篇讲的都是匚w一个字W,但是一个字W或字符集合要匹配多ơ,应该怎么做呢Q比如要匚w一个电子邮件地址Q用之前说到的方法,可能有h会写出像\w@\w\.\wq样的正则表辑ּQ但q个只能匚w到像a@b.cq样的地址Q明显是不正的Q接下来来看看如何匚w电子邮g地址?/span>

         首先要知道电子邮件地址的组成:以字母数字或下划U开头的一l字W,后面跟@W号Q再后面是域名,即用户名@域名地址。不q这也跟具体的邮服务提供商有关Q有的在用户名中也允?字符?/span>

 

1、匹配一个或多个字符

         要想匚w同一个字W(或字W集合)的多ơ重复,只要单地l这个字W(或字W集合)加上一?字符作ؓ后缀可以了?匚w一个或多个字符Q至一个)。如Qa匚wa本nQa+匹配一个或多个q箋出现的aQ[0-9]+匚w多个q箋的数字?/span>

         注意Q在l一个字W集合加?后缀的时候,必须?攑֜字符集合的外面,否则׃是重复匹配了。如[0-9+]q样pC数字或+号了Q虽然语法上正确Q但不是我们惌的了?/span>

文本QHello, mhmyqn@qq.com or mhmyqn@126.com is my email.

正则表达式:\w+@(\w+\.)+\w+

l果QHello, 【mhmyqn@qq.com?or 【mhmyqn@126.com?is my email.

分析Q\w+可以匚w一个或多个字符Q而子表达?\w+\.)+可匹配像xxxx.edu.q样的字W串Q而最后不会是.字符l尾Q所以后面还会有一个\w+。像mhmyqn@xxxx.edu.cnq样的邮件地址也会匚w到?/span>

 

2、匹配零个或多个字符

         匚w零个或多个字W用元W?Q它的用法和+完全一P只要把它攑֜一下字W或字符集合的后面,可以匹配该字符Q或字符集合Q连l出现零ơ或多次。如正则表达式ab*c可以匚wac、abc、abbbbbc{?/span>

 

3、匹配零个或一个字W?/span>

         匚w零个或一个字W用元字符?。像上一说到的匚w一个空白行使用正则表达式\r\n\r\nQ但在Unix和Linux中不需要\rQ就可以使用元字W?Q\r?\n\r?\nq样既可匚wwindows中的I白行,也可匚wUnix和Linux中的I白行。下面来看一个匹配http或https协议的URL的例子:

文本QThe URL is http://www.mikan.com, to connect securely use https://www.mikan.cominstead.

正则表达式:https?://(\w+\.)+\w+

l果QThe URL is 【http://www.mikan.com? to connect securely use 【https://www.mikan.com?instead.

分析Q这个模式以https?开_表示?之前的一个字W可以有Q也可以没有Q所以它能匹配http或httpsQ后面部分和前一个例子一栗?/span>

 

二、匹配的重复ơ数

         正则表达式里???解决了很多问题,但是Q?/span>

         1Q??匚w的字W个数没有上限。我们无法ؓ它们匹配的字符个数讑֮一个最大倹{?/span>

         2Q???臛_匚w一个或零个字符。我们无法ؓ它们匹配的字符个数另行讑֮一个最倹{?/span>

         3Q如果只使用*?Q我们无法把它们匹配的字符个数讑֮Z个精的数字?/span>

         正则表达式里提供了一个用来设定重复次数的语法Q重复次数要用{和}字符来给出,把数值写在它们中间?/span>

        

         1、ؓ重复匚wơ数讑֮一个精?/span>

         如果想ؓ重复匚wơ数讑֮一个精的|把那个数字写在{和}之间卛_。如{4}表示它前面的那个字符Q或字符集合Q必d原始文本中连l重复出?ơ才是一个匹配,如果只出C3ơ,也不是一个匹配?/span>

         如前面几中说到的匹配页面中颜色的例子,可以用重复ơ数来匹配:#[[:xdigit:]]{6}?[0-9a-fA-F]{6}QPOSIX字符在java中是#\\p{XDigit}{6}?/span>

        

         2、ؓ重复匚wơ数讑֮一个区?/span>

         {}语法q可以用来ؓ重复匚wơ数讑֮一个区_也就是ؓ重复匚wơ数讑֮一个最值和最大倹{这U区间必M{n, m}q样的Ş式给出,其中n>=m>=0。如查日期格式是否正(不检查日期的有效性)的正则表辑ּQ如日期2012-08-12?012-8-12Q:\d{4}-\d{1,2}-\d{1,2}?/span>

        

         3、匹配至重复多次

         {}语法的最后一U用法是l出一个最的重复ơ数Q但不必l出最大重复次敎ͼQ如{3,}表示臛_重复3ơ。注意:{3,}中一定要有逗号Q而且逗号后不能有I格。否则会出错?/span>

         来看一个例子,使用正则表达式把所有金额大?100的金额找出来Q?/span>

文本Q?/span>

$25.36

$125.36

$205.0

$2500.44

$44.30

正则表达式:$\d{3,}\.\d{2}

l果Q?/span>

$25.36

?125.36?/span>

?205.0?/span>

?2500.44?/span>

$44.30

 

         +??可以表示成重复次敎ͼ

         +{h于{1,}

         *{h于{0,}

         ?{h于{0,1}

 

三、防止过度匹?/span>

         ?只能匚w零个或一个字W,{n}和{n,m}也有匚w重复ơ数的上限,但是??、{n,}都没有上限|q样有时会导致过度匹配的现象?/span>

         来看匚w一个html标签的例?/span>

文本Q?/span>

Yesterday is <b>history</b>,tomorrow is a <B>mystery</B>, but today is a <b>gift</b>.

正则表达式:<[Bb]>.*</[Bb]>

l果Q?/span>

Yesterday is ?lt;b>history</b>,tomorrow is a <B>mystery</B>, but today is a <b>gift</b>?

分析Q?lt;[Bb]>匚w<b>标签Q不区分大小写)Q?lt;/[Bb]>匚w</b>标签Q不区分大小写)。但l果却不是预期的那样有三个,W一?lt;/b>标签之后Q一直到最后一?lt;/b>之间的东西全部匹配出来了?/span>

         Z么会q样呢?因ؓ*?都是贪婪型的元字W,它们在匹配时的行为模式是多多益善Q它们会可能从一D|本的开头一直匹配到q段文本的末,而不是从q段文本的开头匹配到到W一个匹配时为止?/span>

         当不需要这U贪婪行为时Q可以用这些元字符的懒惰型版本。懒惰意思是匚w可能少的字W,与贪婪型相反。懒惰型元字W只需要给贪婪型元字符加上一?后缀卛_。下面是贪婪型元字符的对应懒惰型版本Q?/span>

         *       *?

         +       +?

         {n,}   {n,}?

         所以上面的例子中,正则表达式只需要改?lt;[Bb]>.*?</[Bb]>卛_Q结果如下:

<b>history</b>

<B>mystery</B>

<b>gift</b>

 

四、ȝ

         正则表达式的真下威力体现在重复次数匹配方面。这里介l了+??几种元字W的用法Q如果要_的确定匹配次敎ͼ使用{}。元字符分贪婪型和懒惰型两种Q在需要防止过度匹配的场合下,请用懒惰型元字W来构造正则表辑ּ




透明的鱼 2012-08-15 17:37 发表评论
]]>
转蝲Q正则表辑ּ学习--元字W的使用http://www.tkk7.com/swzhebei/archive/2012/08/15/385549.html透明的鱼透明的鱼Wed, 15 Aug 2012 09:35:00 GMThttp://www.tkk7.com/swzhebei/archive/2012/08/15/385549.htmlhttp://www.tkk7.com/swzhebei/comments/385549.htmlhttp://www.tkk7.com/swzhebei/archive/2012/08/15/385549.html#Feedback0http://www.tkk7.com/swzhebei/comments/commentRss/385549.htmlhttp://www.tkk7.com/swzhebei/services/trackbacks/385549.html转蝲Q?a href="http://www.tkk7.com/zhanghu198901/archive/2012/08/12/385337.html">http://www.tkk7.com/zhanghu198901/archive/2012/08/12/385337.html
PSQ在所有例子中正则表达式匹配结果包含在源文本中的【和】之_有的例子会用java来实玎ͼ如果是java本n正则表达式的用法Q会在相应的地方说明。所有java例子都在JDK1.6.0_13下测试通过?/span>

 

一、对Ҏ字符q行转义

         元字W是一些在正则表达式里有着Ҏ含义的字W。因为元字符在正则表辑ּ里有着Ҏ的含义,所以这些字W就无法用来代表它们本n。在元字W前面加上一个反斜杠可以对它进行{义,q样得到的{义序列将匚w那个字符本n而不是它Ҏ的元字符含义。如Q如果想要匹配[和]Q就必须对它q行转义Q\[和\]?/span>

         对元字符转义需要用到斜杠\字符Q这意味着\字符本向也是一个元字符Q要匚w\字符本nQ必{义成\\。如匚wwindows文g路径?/span>

 

二、匹配空白字W?/span>

         元字W大致可以分ZU:一U是用来匚w文本的(?Q,另一U是正则表达式的语法所要求的(如[和]Q?/span>

         在进行正则表辑ּ搜烦的时候,我们l常会遇到需要对原始文本中里的非打印I白字符q行匚w的情c比如说Q我们可能需要把所有的制表W找出来Q或者我们需要把换行W找出来Q这cdW很难被直接输入C个正则表辑ּ里,q时我们可以使用如下列出的特D元字符来输入它们:

         \b     回退Qƈ删除Q一个字W(Backspace键)

         \f      换页W?/span>

         \n     换行W?/span>

         \r      回RW?/span>

         \t      制表W(Tab键)

         \v      垂直制表W?/span>

 

         来看一个例子,把文件中的空白行LQ?/span>

文本Q?/span>

8 5 4 1 6 3  2 7 9

7 6 2 9 5 8  3 4 1

9 3 1 4 2 7  8 5 6

 

6 9 3 8 7 5  1 2 4

5 1 8 3 4 2  6 9 7

2 4 7 6 1 9  5 3 8

 

3 26  7 8 4 9 1 5                                                       

4 8 9 5 3 1  7 6 2

1 7 5 2 9 6  4 8 3

正则表达式:\r\n\r\n

分析Q\r\n匚w一个回?换行l合Qwindows操作pȝ中把它作为文本行的结束标{。用正则表辑ּ\r\n\r\nq行的搜索将匚w两个q箋的行标{,而这正好是空白行?/span>

         注意QUnix和Linux操作pȝ中只使用一个换行符来结束一个文本行Q换句话_在Unix或Linuxpȝ中匹配空白行只用\n\n卛_Q不需要加上\r。同旉用于windows和Unix/Linux的正则表辑ּ应该包括一个可先的\r和一个必d配的\nQ即\r?\n\r?\nQ这会在后面的文章中讲到?/span>

         Java代码如下Q?/span>

    public static void matchBlankLine() throws Exception{
        BufferedReader br 
= new BufferedReader(new FileReader(new File("E:/九宫?txt")));
        StringBuilder sb 
= new StringBuilder();
        
char[] cbuf = new char[1024];
        
int len = 0;
        
while(br.ready() && (len = br.read(cbuf)) > 0){
            br.read(cbuf);
            sb.append(cbuf, 
0, len);
        }
        String reg 
= "\r\n\r\n";
        System.out.println(
"原内容:\n" + sb.toString());
        System.out.println(
"处理后:-----------------------------");
        System.out.println(sb.toString().replaceAll(reg, 
"\r\n"));
    }

q行l果如下Q?/span>

原内容:

8 5 4  1 6 3 2 7 9

7 6 2  9 5 8 3 4 1

9 3 1  4 2 7 8 5 6

 

6 9 3  8 7 5 1 2 4

5 1 8  3 4 2 6 9 7

2 4 7  6 1 9 5 3 8

 

3 2 6  7 8 4 9 1 5

4 8 9  5 3 1 7 6 2

1 7 5  2 9 6 4 8 3

处理后:-----------------------------

8 5 4  1 6 3 2 7 9

7 6 2  9 5 8 3 4 1

9 3 1  4 2 7 8 5 6

6 9 3  8 7 5 1 2 4

5 1 8  3 4 2 6 9 7

2 4 7  6 1 9 5 3 8

3 2 6  7 8 4 9 1 5

4 8 9  5 3 1 7 6 2

1 7 5  2 9 6 4 8 3


三、匹配特定的字符cd

         字符集合Q匹配多个字W中的某一个)是最常见的匹配Ş式,而一些常用的字符集合可以用特D元字符来代ѝ这些元字符匚w的是某一cd的字W(cd字符Q,cd字符q不是必不可的Q因为可以通过逐一列D有关字符或通过定义一个字W区间来匚w某一cdW,但是使用它们构造出来的正则表达式简明易懂,在实际应用中很常用?/span>

 

         1、匹配数字与非数?/span>

         \d     M一个数字,{h于[0-9]或[0123456789]

         \D     M一个非数字Q等价于[^0-9]或[^0123456789]

 

         2、匹配字母和数字与非字母和数?/span>

         字母QA-Z不区分大写Q、数字、下划线是一U常用的字符集合Q可用如下类元字W:

         \w    M一个字母(不区分大写Q、数字、下划线Q等价于[0-9a-zA-Z_]

         \W    M一个非字母数字和下划线Q等价于[^0-9a-zA-Z_]

 

         3、匹配空白字W与非空白字W?/span>

         \s      M一下空白字W,{h于[\f\n\r\t\v]

         \S      M一下空白字W,{h于[^\f\n\r\t\v]

         注意Q退格元字符\b没有不在\s的范围之内?/span>

 

         4、匹配十六进制或八进制数?/span>

         十六q制Q用前缀\x来给出,如:\x0A对应于ASCII字符10Q换行符Q,其效果等价于\n?/span>

         八进Ӟ用前~\0来给出,数值本w可以是两位或三位数字,如:\011对应于ASCII字符9Q制表符Q,其效果等价于\t?/span>

        

四、用POSIX字符c?/span>

         POSIX字符cL很多正则表达式实现都支持的一U简写Ş式。Java也支持它Q但JavaScript不支持。POSIX字符如下所C:

[:alnum:]  M一个字母或数字Q等价于[a-zA-Z0-9]

[:alpha:]   M一个字母,{h于[a-zA-Z]

[:blank:]   I格或制表符Q等价于[\t]

[:cntrl:]     ASCII控制字符QASCII 0?1Q再加上ASCII 127Q?/span>

[:digit:]     M一个数字,{h于[0-9]

[:graph:]  M一个可打印字符Q但不包括空?/span>

[:lower:]   M一个小写字母,{h于[a-z]

[:print:]    M一个可打印字符

[:punct:]   既不属于[:alnum:]和[:cntrl:]的Q何一个字W?/span>

[:space:]  M一个空白字W,包括I格Q等价于[^\f\n\r\t\v]

[:upper:]  M一个大写字母,{h于[A-Z]

[:xdigit:]   M一个十六进制数字,{h于[a-fA-F0-9]

 

         POSIX字符和之前见q的元字W不太一P我们来看一个前面利用正则表辑ּ来匹配网中的颜色的例子Q?/span>

文本Q?lt;span style="background-color:#3636FF;height:30px;width:60px;">试</span>

正则表达式:#[[:xdigit:]] [[:xdigit:]] [[:xdigit:]] [[:xdigit:]] [[:xdigit:]] [[:xdigit:]]

l果Q?lt;span style="background-color:?3636FF?height:30px;width:60px;">试</span>

注意Q这里用的模式以[[开头、以]]l束Q这是用POSIX字符cL必须的,POSIX字符必须括在[:?]之间Q外层[和]字符用来定义一个集合,内层的[和]字符是POSIX字符cLw的l成部分?/span>

在java中的POSIX字符表示有所不同Q不是包括在[:?]之间Q而是以\p开_包括在{和}之间Q且大小写有区别Q同时增加了\p{ASCII}Q如下所C:

\p{Alnum}         字母数字字符Q[\p{Alpha}\p{Digit}]

\p{Alpha}          字母字符Q[\p{Lower}\p{Upper}]

\p{ASCII}           所?ASCIIQ[\x00-\x7F]

\p{Blank}           I格或制表符Q[ \t]

\p{Cntrl}            控制字符Q[\x00-\x1F\x7F]

\p{Digit}            十进制数字:[0-9]

\p{Graph}          可见字符Q[\p{Alnum}\p{Punct}]

\p{Lower}          写字母字符Q[a-z]

\p{Print}            可打印字W:[\p{Graph}\x20]

\p{Punct}          标点W号Q?"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

\p{Space}          I白字符Q[ \t\n\x0B\f\r]

\p{Upper}          大写字母字符Q[A-Z]

\p{XDigit}          十六q制数字Q[0-9a-fA-F]




透明的鱼 2012-08-15 17:35 发表评论
]]>
Java关键字final、static使用ȝ http://www.tkk7.com/swzhebei/archive/2012/06/14/380780.html透明的鱼透明的鱼Thu, 14 Jun 2012 05:53:00 GMThttp://www.tkk7.com/swzhebei/archive/2012/06/14/380780.htmlhttp://www.tkk7.com/swzhebei/comments/380780.htmlhttp://www.tkk7.com/swzhebei/archive/2012/06/14/380780.html#Feedback0http://www.tkk7.com/swzhebei/comments/commentRss/380780.htmlhttp://www.tkk7.com/swzhebei/services/trackbacks/380780.html一、final
 ҎE序上下文环境,Java关键字final?#8220;q是无法改变?#8221;或?#8220;l态的”含义Q它可以修饰非抽象类、非抽象cL员方法和变量。你可能Z两种理解而需要阻止改变:设计或效率?
finalcM能被l承Q没有子c,finalcM的方法默认是final的?
finalҎ不能被子cȝҎ覆盖Q但可以被ѝ?
final成员变量表示帔RQ只能被赋gơ,赋值后g再改变?
final不能用于修饰构造方法?
注意Q父cȝprivate成员Ҏ是不能被子类Ҏ覆盖的,因此privatecd的方法默认是finalcd的?

1、finalc?/strong>
 finalcM能被l承Q因此finalcȝ成员Ҏ没有Z被覆盖,默认都是final的。在设计cL候,如果q个cM需要有子类Q类的实现细节不允许改变Qƈ且确信这个类不会载被扩展Q那么就设计为finalcR?

2、finalҎ
如果一个类不允许其子类覆盖某个ҎQ则可以把这个方法声明ؓfinalҎ?
使用finalҎ的原因有二:
W一、把Ҏ锁定Q防止Q何承类修改它的意义和实现?
W二、高效。编译器在遇到调用finalҎ时候会转入内嵌机制Q大大提高执行效率?
例如Q?

public class Test1 {

public static void main(String[] args) {
// TODO 自动生成Ҏ存根
}

public void f1() {
System.out.println("f1");
}
//无法被子c覆盖的Ҏ
public final void f2() {
System.out.println("f2");
}

public void f3() {
System.out.println("f3");
}

private void f4() {
System.out.println("f4");
}
}

public class Test2 extends Test1 {

public void f1(){
System.out.println("Test1父类Ҏf1被覆?");
}

public static void main(String[] args) {
Test2 t=new Test2();
t.f1();
t.f2(); //调用从父cȝ承过来的finalҎ
t.f3(); //调用从父cȝ承过来的Ҏ
//t.f4(); //调用p|Q无法从父类l承获得

}


3、final变量Q常量)
 用final修饰的成员变量表C常量,g旦给定就无法改变Q?
 final修饰的变量有三种Q静态变量、实例变量和局部变量,分别表示三种cd的常量?
 从下面的例子中可以看出,一旦给final变量初值后Q值就不能再改变了?
 另外Qfinal变量定义的时候,可以先声明,而不l初|q中变量也称为finalI白Q无Z么情况,~译器都保I白final在用之前必被初始化。但是,finalI白在final关键字final的用上提供了更大的灉|性,为此Q一个类中的final数据成员可以实C对象而有所不同Q却有保持其恒定不变的特征?/p>

package org.leizhimin;

public class Test3 {
private final String S="final实例变量S";
private final int A=100;
public final int B=90;

public static final int C=80;
private static final int D=70;

public final int E; //finalI白,必须在初始化对象的时候赋初?

public Test3(int x){
E=x;
}

/**
* @param args
*/
public static void main(String[] args) {
Test3 t=new Test3(2);
//t.A=101; //出错,final变量的g旦给定就无法改变
//t.B=91; //出错,final变量的g旦给定就无法改变
//t.C=81; //出错,final变量的g旦给定就无法改变
//t.D=71; //出错,final变量的g旦给定就无法改变

System.out.println(t.A);
System.out.println(t.B);
System.out.println(t.C); //不推荐用对象方式讉K静态字D?
System.out.println(t.D); //不推荐用对象方式讉K静态字D?
System.out.println(Test3.C);
System.out.println(Test3.D);
//System.out.println(Test3.E); //出错,因ؓE为finalI白,依据不同对象值有所不同.
System.out.println(t.E);

Test3 t1=new Test3(3);
System.out.println(t1.E); //finalI白变量E依据对象的不同而不?
}

private void test(){
System.out.println(new Test3(1).A);
System.out.println(Test3.C);
System.out.println(Test3.D);
}

public void test2(){
final int a; //finalI白,在需要的时候才赋?
final int b=4; //局部常?-final用于局部变量的情Ş
final int c; //finalI白,一直没有给赋?
a=3;
//a=4; 出错,已经l赋qg.
//b=2; 出错,已经l赋qg.
}

 
4、final参数
当函数参CؓfinalcdӞ你可以读取用该参数Q但是无法改变该参数的倹{?/p>

public class Test4 {
public static void main(String[] args) {
new Test4().f1(2);
}

public void f1(final int i){
//i++; //i是finalcd?g允许改变?
System.out.print(i);
}

二、static

 static表示“全局”或?#8220;静?#8221;的意思,用来修饰成员变量和成员方法,也可以Ş成静态static代码块,但是Java语言中没有全局变量的概c?

 被static修饰的成员变量和成员Ҏ独立于该cȝM对象。也是_它不依赖cȝ定的实例Q被cȝ所有实例共享。只要这个类被加载,Java虚拟机就能根据类名在q行时数据区的方法区内定扑ֈ他们。因此,static对象可以在它的Q何对象创Z前访问,无需引用M对象?

 用public修饰的static成员变量和成员方法本质是全局变量和全局ҎQ当声明它类的对象市Q不生成static变量的副本,而是cȝ所有实例共享同一个static变量?

 static变量前可以有private修饰Q表C个变量可以在cȝ静态代码块中,或者类的其他静态成员方法中使用Q当然也可以在非静态成员方法中使用--废话Q,但是不能在其他类中通过cd来直接引用,q一点很重要。实际上你需要搞明白Qprivate是访问权限限定,static表示不要实例化就可以使用Q这样就Ҏ理解多了。static前面加上其它讉K权限关键字的效果也以此类推?

 static修饰的成员变量和成员Ҏ习惯上称为静态变量和静态方法,可以直接通过cd来访问,讉K语法为:
cd.静态方法名(参数列表...)
cd.静态变量名

 用static修饰的代码块表示静态代码块Q当Java虚拟机(JVMQ加载类Ӟ׃执行该代码块Q用处非常大Q呵呵)?

1、static变量
 按照是否静态的对类成员变量q行分类可分两种Q一U是被static修饰的变量,叫静态变量或cd量;另一U是没有被static修饰的变量,叫实例变量。两者的区别是:
 对于静态变量在内存中只有一个拷贝(节省内存Q,JVM只ؓ静态分配一ơ内存,在加载类的过E中完成静态变量的内存分配Q可用类名直接访问(方便Q,当然也可以通过对象来访问(但是q是不推荐的Q?
 对于实例变量Q没创徏一个实例,׃为实例变量分配一ơ内存,实例变量可以在内存中有多个拷贝,互不影响Q灵z)?

2、静态方?/strong>
 静态方法可以直接通过cd调用QQ何的实例也都可以调用Q因此静态方法中不能用this和super关键字,不能直接讉K所属类的实例变量和实例Ҏ(是不带static的成员变量和成员成员Ҏ)Q只能访问所属类的静态成员变量和成员Ҏ。因为实例成员与特定的对象关联!q个需要去理解Q想明白其中的道理,不是记忆Q!Q?
 因ؓstaticҎ独立于Q何实例,因此staticҎ必须被实玎ͼ而不能是抽象的abstract?

3、static代码?/strong>
 static代码块也叫静态代码块Q是在类中独立于cL员的static语句块,可以有多个,位置可以随便放,它不在Q何的Ҏ体内QJVM加蝲cL会执行这些静态的代码块,如果static代码块有多个QJVM按照它们在cM出现的先后顺序依ơ执行它们,每个代码块只会被执行一ơ。例如:

public class Test5 {
private static int a;
private int b;

static{
Test5.a=3;
System.out.println(a);
Test5 t=new Test5();
t.f();
t.b=1000;
System.out.println(t.b);
}
static{
Test5.a=4;
System.out.println(a);
}
public static void main(String[] args) {
// TODO 自动生成Ҏ存根
}
static{
Test5.a=5;
System.out.println(a);
}
public void f(){
System.out.println("hhahhahah");
}
 

q行l果Q?
3
hhahhahah
1000
4
5

 利用静态代码块可以对一些static变量q行赋|最后再看一D些例子,都一个static的mainҎQ这样JVM在运行mainҎ的时候可以直接调用而不用创建实例?

4、static和final一块用表示什?/strong>
static final用来修饰成员变量和成员方法,可简单理解ؓ“全局帔R”Q?
对于变量Q表CZ旦给值就不可修改Qƈ且通过cd可以讉K?
对于ҎQ表CZ可覆盖,q且可以通过cd直接讉K?/p>

 



透明的鱼 2012-06-14 13:53 发表评论
]]>
ThreadLocalc(2Q?/title><link>http://www.tkk7.com/swzhebei/archive/2012/05/31/379659.html</link><dc:creator>透明的鱼</dc:creator><author>透明的鱼</author><pubDate>Thu, 31 May 2012 05:16:00 GMT</pubDate><guid>http://www.tkk7.com/swzhebei/archive/2012/05/31/379659.html</guid><wfw:comment>http://www.tkk7.com/swzhebei/comments/379659.html</wfw:comment><comments>http://www.tkk7.com/swzhebei/archive/2012/05/31/379659.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/swzhebei/comments/commentRss/379659.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/swzhebei/services/trackbacks/379659.html</trackback:ping><description><![CDATA[<p><strong>什么是ThreadLocalQ?/strong></p> <p>֐思义它是local variableQ线E局部变量)。它的功用非常简单,是为每一个用该变量的线E都提供一个变量值的副本Q是每一个线E都可以独立地改变自q副本Q而不会和其它U程的副本冲H。从U程的角度看Q就好像每一个线E都完全拥有该变量?/p> <p>使用场景</p> <ol><li><strong>To keep state with a thread (user-id, transaction-id, logging-id)</strong></li><li><strong>To cache objects which you need frequently</strong></li></ol> <p><strong>ThreadLocalc?/strong></p> <p>它主要由四个Ҏl成initialValue()Qget()Qset(T)Qremove()Q其中值得注意的是initialValue()Q该Ҏ是一个protected的方法,昄是ؓ了子c重写而特意实现的。该Ҏq回当前U程在该U程局部变量的初始|q个Ҏ是一个gq调用方法,在一个线E第1ơ调用get()或者set(Object)时才执行Qƈ且仅执行1ơ。ThreadLocal中的实实现直接q回一个nullQ?/p> <p>ThreadLocal的原?/p> <p>ThreadLocal是如何做Cؓ每一个线E维护变量的副本的呢Q其实实现的思\很简单,在ThreadLocalcM有一个MapQ用于存储每一个线E的变量的副本。比如下面的CZ实现Q?/p> <p>public class ThreadLocal<br />{<br /> private Map values = Collections.synchronizedMap(new HashMap());<br /> public Object get()<br /> {<br />  Thread curThread = Thread.currentThread(); <br />  Object o = values.get(curThread); <br />  if (o == null && !values.containsKey(curThread))<br />  {<br />   o = initialValue();<br />   values.put(curThread, o); <br />  }<br />  return o; <br /> }</p> <p> public void set(Object newValue)<br /> {<br />  values.put(Thread.currentThread(), newValue);<br /> }</p> <p> public Object initialValue()<br /> {<br />  return null; <br /> }<br />} </p> <p><strong>ThreadLocal 的?/strong></p> <p>使用Ҏ一Q?/p> <p>Hibernate的文档时看到了关于ThreadLocal理多线E访问的部分。具体代码如?<br /><br />1.  public static final ThreadLocal session = new ThreadLocal(); <br />2.  public static Session currentSession() { <br />3.      Session s = (Session)session.get(); <br />4.      //open a new session,if this session has none <br />5.   if(s == null){ <br />6.      s = sessionFactory.openSession(); <br />7.      session.set(s); <br />8.   } <br />      return s; <br />9. } <br /><br />我们逐行分析 <br />1?初始化一个ThreadLocal对象QThreadLocal有三个成员方?get()、set()、initialvalue()?<br />    如果不初始化initialvalueQ则initialvalueq回null?<br />3。session的getҎ当前U程q回其对应的U程内部变量Q也是我们需要的net.sf.hibernate.SessionQ相当于对应每个数据库连接).多线E情况下׃n数据库链接是不安全的。ThreadLocal保证了每个线E都有自qsQ数据库q接Q?<br />5。如果是该线E初ơ访问,自然QsQ数据库q接Q会是nullQ接着创徏一个SessionQ具体就是行6?<br />6。创Z个数据库q接实例 s <br />7。保存该数据库连接s到ThreadLocal中?<br />8。如果当前线E已l访问过数据库了Q则从session中get()可以获取该U程上次获取q的q接实例?</p> <p>使用Ҏ?/p> <p>当要l线E初始化一个特D值时Q需要自己实现ThreadLocal的子cdƈ重写该方法,通常使用一个内部匿名类对ThreadLocalq行子类化,EasyDBO中创建jdbcq接上下文就是这样做的:</p> <p>public class JDBCContext{<br /> private static Logger logger = Logger.getLogger(JDBCContext.class);<br /> private DataSource ds;<br /> protected Connection connection;<br /> private boolean isValid = true;<br /> private static ThreadLocal jdbcContext;<br /> <br /> private JDBCContext(DataSource ds){<br />  this.ds = ds;<br />  createConnection();  <br /> }<br /> public static JDBCContext getJdbcContext(javax.sql.DataSource ds)<br /> {  <br />  if(jdbcContext==null)jdbcContext=new JDBCContextThreadLocal(ds);<br />  JDBCContext context = (JDBCContext) jdbcContext.get();<br />  if (context == null) {<br />   context = new JDBCContext(ds);<br />  }<br />  return context;<br /> }</p> <p> private static class JDBCContextThreadLocal extends ThreadLocal {<br />  public javax.sql.DataSource ds;<br />  public JDBCContextThreadLocal(javax.sql.DataSource ds)<br />  {<br />   this.ds=ds;<br />  }<br />  protected synchronized Object initialValue() {<br />   return new JDBCContext(ds);<br />  }<br /> }<br />}</p> <p>使用单例模式Q不同的U程调用getJdbcContext()获得自己的jdbcContextQ都是通过JDBCContextThreadLocal 内置子类来获得JDBCContext对象的线E局部变量,q个?/p><img src ="http://www.tkk7.com/swzhebei/aggbug/379659.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/swzhebei/" target="_blank">透明的鱼</a> 2012-05-31 13:16 <a href="http://www.tkk7.com/swzhebei/archive/2012/05/31/379659.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ThreadLocalc(1Q?/title><link>http://www.tkk7.com/swzhebei/archive/2012/05/31/379657.html</link><dc:creator>透明的鱼</dc:creator><author>透明的鱼</author><pubDate>Thu, 31 May 2012 05:15:00 GMT</pubDate><guid>http://www.tkk7.com/swzhebei/archive/2012/05/31/379657.html</guid><wfw:comment>http://www.tkk7.com/swzhebei/comments/379657.html</wfw:comment><comments>http://www.tkk7.com/swzhebei/archive/2012/05/31/379657.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/swzhebei/comments/commentRss/379657.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/swzhebei/services/trackbacks/379657.html</trackback:ping><description><![CDATA[<p><strong><span style="font-size: 18px">1.目的</span></strong></p> <p>ThreadLocal目的是保存一些线E别的全局变量Q比如connectionQ或者事务上下文Q避免这些值需要一直通过函数参数的方式一路传递?span id="more-109"></span></p> <p><span style="font-size: 18px"><strong>2. 常见用法</strong></span><br />举例其中一U常见用法:</p> <div id="hdnbfpj" class="wp_syntax" style="padding-bottom: 15px"> <div id="htnjhhx" class="code"><pre class="java" style="color: #000000; font-family: 'Consolas',monospace,'Courier New'"><span style="font-weight: bold; color: #9400d3">public</span> <span style="font-weight: bold; color: #9400d3">class</span> Test2 <span style="color: #4a708b">{</span> <span style="font-weight: bold; color: #9400d3">public</span> <span style="font-weight: bold; color: #9400d3">static</span> <span style="font-weight: bold; color: #000066">void</span> main<span style="color: #4a708b">(</span><span style="color: #436eee">String</span><span style="color: #4a708b">[</span><span style="color: #4a708b">]</span> args<span style="color: #4a708b">)</span> <span style="font-weight: bold; color: #9400d3">throws</span> <span style="color: #436eee">InterruptedException</span> <span style="color: #4a708b">{</span> testThreadLocal<span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="color: #4a708b">}</span>   <span style="font-weight: bold; color: #9400d3">private</span> <span style="font-weight: bold; color: #9400d3">static</span> <span style="font-weight: bold; color: #000066">void</span> testThreadLocal<span style="color: #4a708b">(</span><span style="color: #4a708b">)</span> <span style="color: #4a708b">{</span> <span style="color: #436eee">Util</span>.<span style="color: #b8860b">setGlobalName</span><span style="color: #4a708b">(</span><span style="color: #247">"zili.dengzl"</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="font-weight: bold; color: #9400d3">new</span> Foo<span style="color: #4a708b">(</span><span style="color: #4a708b">)</span>.<span style="color: #b8860b">printName</span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="color: #4a708b">}</span> <span style="color: #4a708b">}</span>   <span style="font-weight: bold; color: #9400d3">class</span> Foo<span style="color: #4a708b">{</span> <span style="font-weight: bold; color: #9400d3">public</span> <span style="font-weight: bold; color: #000066">void</span> printName<span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">{</span> <span style="color: #436eee">System</span>.<span style="color: #b8860b">out</span>.<span style="color: #b8860b">println</span><span style="color: #4a708b">(</span><span style="color: #247">"globalName="</span><span style="color: #4a708b">+</span><span style="color: #436eee">Util</span>.<span style="color: #b8860b">getGlobalName</span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="color: #4a708b">}</span> <span style="color: #4a708b">}</span>   <span style="font-weight: bold; color: #9400d3">class</span> <span style="color: #436eee">Util</span> <span style="color: #4a708b">{</span> <span style="font-weight: bold; color: #9400d3">private</span> <span style="font-weight: bold; color: #9400d3">static</span> <span style="font-weight: bold; color: #9400d3">final</span> ThreadLocal<span style="color: #4a708b"><</span>String<span style="color: #4a708b">></span> globalName <span style="color: #4a708b">=</span> <span style="font-weight: bold; color: #9400d3">new</span> ThreadLocal<span style="color: #4a708b"><</span>String<span style="color: #4a708b">></span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span>   <span style="font-weight: bold; color: #9400d3">public</span> <span style="font-weight: bold; color: #9400d3">static</span> <span style="color: #436eee">String</span> getGlobalName<span style="color: #4a708b">(</span><span style="color: #4a708b">)</span> <span style="color: #4a708b">{</span> <span style="font-weight: bold; color: #9400d3">return</span> globalName.<span style="color: #b8860b">get</span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="color: #4a708b">}</span>   <span style="font-weight: bold; color: #9400d3">public</span> <span style="font-weight: bold; color: #9400d3">static</span> <span style="font-weight: bold; color: #000066">void</span> setGlobalName<span style="color: #4a708b">(</span><span style="color: #436eee">String</span> name<span style="color: #4a708b">)</span> <span style="color: #4a708b">{</span> globalName.<span style="color: #b8860b">set</span><span style="color: #4a708b">(</span>name<span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="color: #4a708b">}</span> <span style="color: #4a708b">}</span></pre></div></div> <p><strong><span style="font-size: 18px">3.实现分析</span></strong></p> <p>要实C面这L功能Q最单的x是用一个Map<Thread,T>,如下Q?/p> <div id="rrdprrx" class="wp_syntax" style="padding-bottom: 0px"> <div id="vnhtdlt" class="code"><pre class="java" style="color: #000000; font-family: 'Consolas',monospace,'Courier New'"><span style="font-weight: bold; color: #9400d3">class</span> MockThreadLocal<span style="color: #4a708b"><</span>T<span style="color: #4a708b">></span> <span style="color: #4a708b">{</span> <span style="font-weight: bold; color: #9400d3">private</span> Map<span style="color: #4a708b"><</span>Thread, T<span style="color: #4a708b">></span> map <span style="color: #4a708b">=</span> <span style="font-weight: bold; color: #9400d3">new</span> HashMap<span style="color: #4a708b"><</span>Thread, T<span style="color: #4a708b">></span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span>   <span style="font-weight: bold; color: #9400d3">public</span> T get<span style="color: #4a708b">(</span><span style="color: #4a708b">)</span> <span style="color: #4a708b">{</span> <span style="font-weight: bold; color: #9400d3">return</span> <span style="color: #4a708b">(</span>T<span style="color: #4a708b">)</span> map.<span style="color: #b8860b">get</span><span style="color: #4a708b">(</span><span style="color: #436eee">Thread</span>.<span style="color: #b8860b">currentThread</span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="color: #4a708b">}</span>   <span style="font-weight: bold; color: #9400d3">public</span> <span style="font-weight: bold; color: #000066">void</span> set<span style="color: #4a708b">(</span>T value<span style="color: #4a708b">)</span> <span style="color: #4a708b">{</span> map.<span style="color: #b8860b">put</span><span style="color: #4a708b">(</span><span style="color: #436eee">Thread</span>.<span style="color: #b8860b">currentThread</span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span>, value<span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="color: #4a708b">}</span> <span style="color: #4a708b">}</span></pre></div></div> <p>q样也能实现ThreadLocal的效果,但是有一个问题,当对应的U程消失后,map中对应的U程值ƈ不会被回Ӟ从而造成内存泄露?/p> <p>事实上ThreadLocal是这样做的:</p> <p>每个Thread都有一个threadLocalMapQkey是threadLocal对象Qvalue是具体用的倹{ThreadLocal对象的get是先取得当前的ThreadQ然后从q个Thread的threadLcoalMap中取出倹{setcM?/p> <p>下面看下具体代码Q?/p> <div id="dnhlljr" class="wp_syntax" style="padding-bottom: 0px"> <div id="zjtvpfl" class="code"><pre class="java" style="color: #000000; font-family: 'Consolas',monospace,'Courier New'"> <span style="font-weight: bold; color: #008000; font-style: italic">/** * Returns the value in the current thread's copy of this * thread-local variable. If the variable has no value for the * current thread, it is first initialized to the value returned * by an invocation of the {@link #initialValue} method. * * @return the current thread's value of this thread-local */</span> <span style="font-weight: bold; color: #9400d3">public</span> T get<span style="color: #4a708b">(</span><span style="color: #4a708b">)</span> <span style="color: #4a708b">{</span> <span style="color: #436eee">Thread</span> t <span style="color: #4a708b">=</span> <span style="color: #436eee">Thread</span>.<span style="color: #b8860b">currentThread</span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> ThreadLocalMap map <span style="color: #4a708b">=</span> getMap<span style="color: #4a708b">(</span>t<span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="font-weight: bold; color: #9400d3">if</span> <span style="color: #4a708b">(</span>map <span style="color: #4a708b">!=</span> <span style="color: #dc143c">null</span><span style="color: #4a708b">)</span> <span style="color: #4a708b">{</span> ThreadLocalMap.<span style="color: #b8860b">Entry</span> e <span style="color: #4a708b">=</span> map.<span style="color: #b8860b">getEntry</span><span style="color: #4a708b">(</span><span style="font-weight: bold; color: #9400d3">this</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="font-weight: bold; color: #9400d3">if</span> <span style="color: #4a708b">(</span>e <span style="color: #4a708b">!=</span> <span style="color: #dc143c">null</span><span style="color: #4a708b">)</span> <span style="font-weight: bold; color: #9400d3">return</span> <span style="color: #4a708b">(</span>T<span style="color: #4a708b">)</span>e.<span style="color: #b8860b">value</span><span style="color: #4a708b">;</span> <span style="color: #4a708b">}</span> <span style="font-weight: bold; color: #9400d3">return</span> setInitialValue<span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="color: #4a708b">}</span></pre></div></div> <p>注意q里如果取到没有该线E对应的|会调用setInitialValue();Q最l调用initialValue()生成一个|q也是我们很多场景下要overrideq个Ҏ的原因;</p> <p> </p> <p>下面看一下getMap(Thread t)ҎQ?/p> <div id="hjtfrfv" class="wp_syntax" style="padding-bottom: 0px"> <div id="lbtvznt" class="code"><pre class="java" style="color: #000000; font-family: 'Consolas',monospace,'Courier New'"> ThreadLocalMap getMap<span style="color: #4a708b">(</span><span style="color: #436eee">Thread</span> t<span style="color: #4a708b">)</span> <span style="color: #4a708b">{</span> <span style="font-weight: bold; color: #9400d3">return</span> t.<span style="color: #b8860b">threadLocals</span><span style="color: #4a708b">;</span> <span style="color: #4a708b">}</span></pre></div></div> <p>在ThreadcMQ?/p> <div id="tlnhzzx" class="wp_syntax" style="padding-bottom: 0px"> <div id="tjvxrhv" class="code"><pre class="java" style="color: #000000; font-family: 'Consolas',monospace,'Courier New'"> <span style="color: #008b45">/* ThreadLocal values pertaining to this thread. This map is maintained * by the ThreadLocal class. */</span> <span style="color: #436eee">ThreadLocal</span>.<span style="color: #b8860b">ThreadLocalMap</span> threadLocals <span style="color: #4a708b">=</span> <span style="color: #dc143c">null</span><span style="color: #4a708b">;</span></pre></div></div> <p>由此可见Q所有的ThreadLocal的信息,最l是兌到Thread上的Q线E消失后Q对应的Thread对象也被回收Q这时对应的ThreadLocal对象(该线E部?也会被回收?/p> <p>q里Z么是一个ThreadLocalMap呢,因ؓ一个线E可以有多个ThreadLocal变量Q通过map.getEntry(this)取得对应的某个具体的变量?/p> <div id="dnrbnlz" class="wp_syntax" style="padding-bottom: 0px"> <div id="xprdnnl" class="code"><pre class="java" style="color: #000000; font-family: 'Consolas',monospace,'Courier New'"> <span style="font-weight: bold; color: #9400d3">private</span> Entry getEntry<span style="color: #4a708b">(</span><span style="color: #436eee">ThreadLocal</span> key<span style="color: #4a708b">)</span> <span style="color: #4a708b">{</span> <span style="font-weight: bold; color: #000066">int</span> i <span style="color: #4a708b">=</span> key.<span style="color: #b8860b">threadLocalHashCode</span> <span style="color: #4a708b">&</span> <span style="color: #4a708b">(</span>table.<span style="color: #b8860b">length</span> <span style="color: #4a708b">-</span> <span style="color: #a52a2a">1</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> Entry e <span style="color: #4a708b">=</span> table<span style="color: #4a708b">[</span>i<span style="color: #4a708b">]</span><span style="color: #4a708b">;</span> <span style="font-weight: bold; color: #9400d3">if</span> <span style="color: #4a708b">(</span>e <span style="color: #4a708b">!=</span> <span style="color: #dc143c">null</span> <span style="color: #4a708b">&&</span> e.<span style="color: #b8860b">get</span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span> <span style="color: #4a708b">==</span> key<span style="color: #4a708b">)</span> <span style="font-weight: bold; color: #9400d3">return</span> e<span style="color: #4a708b">;</span> <span style="font-weight: bold; color: #9400d3">else</span> <span style="font-weight: bold; color: #9400d3">return</span> getEntryAfterMiss<span style="color: #4a708b">(</span>key, i, e<span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="color: #4a708b">}</span></pre></div></div> <p>最后要注意的一ҎQThreadLocalMap的Entry是一个weakReferenceQ?/p> <div id="dvprdjz" class="wp_syntax" style="padding-bottom: 0px"> <div id="llvhrrp" class="code"><pre class="java" style="color: #000000; font-family: 'Consolas',monospace,'Courier New'"> <span style="font-weight: bold; color: #008000; font-style: italic">/** * The entries in this hash map extend WeakReference, using * its main ref field as the key (which is always a * ThreadLocal object). Note that null keys (i.e. entry.get() * == null) mean that the key is no longer referenced, so the * entry can be expunged from table. Such entries are referred to * as "stale entries" in the code that follows. */</span> <span style="font-weight: bold; color: #9400d3">static</span> <span style="font-weight: bold; color: #9400d3">class</span> Entry <span style="font-weight: bold; color: #9400d3">extends</span> WeakReference<span style="color: #4a708b"><</span>ThreadLocal<span style="color: #4a708b">></span> <span style="color: #4a708b">{</span> <span style="font-weight: bold; color: #008000; font-style: italic">/** The value associated with this ThreadLocal. */</span> <span style="color: #436eee">Object</span> value<span style="color: #4a708b">;</span>   Entry<span style="color: #4a708b">(</span><span style="color: #436eee">ThreadLocal</span> k, <span style="color: #436eee">Object</span> v<span style="color: #4a708b">)</span> <span style="color: #4a708b">{</span> <span style="font-weight: bold; color: #9400d3">super</span><span style="color: #4a708b">(</span>k<span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> value <span style="color: #4a708b">=</span> v<span style="color: #4a708b">;</span> <span style="color: #4a708b">}</span> <span style="color: #4a708b">}</span></pre></div></div> <p>q里主要因ؓThreadLocalMap的key是ThreadLocal对象Q如果某个ThreadLocal对象所有的强引用没有了Q会利用weakref的功能把他回收掉Q然后复用这个entry?/p> <p>考虑一下如果不用weakReference会出C么情况:假设某个对象是这样引用的</p> <div id="rhjlxdt" class="wp_syntax" style="padding-bottom: 0px"> <div id="hjdfhfb" class="code"><pre class="java" style="color: #000000; font-family: 'Consolas',monospace,'Courier New'"><span style="font-weight: bold; color: #9400d3">private</span> <span style="font-weight: bold; color: #9400d3">final</span> ThreadLocal<span style="color: #4a708b"><</span>String<span style="color: #4a708b">></span> globalName <span style="color: #4a708b">=</span> <span style="font-weight: bold; color: #9400d3">new</span> ThreadLocal<span style="color: #4a708b"><</span>String<span style="color: #4a708b">></span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span></pre></div></div> <p>注意没有staticQ然后这个对象被不断的new出来Q然后死掉,每次ThreadLocalmap中都会多Z个entryQ然后这个entry强引用一个ThreadLocal对象QThreadLocalMap本n没有办法确定哪个entry是不用了的,如果恰好q个U程是线E池中的Q会存活很久Q那杯具了?/p> <p>ThreadLocalMap用了weakReferenceQ失d引用的ThreadLocal对象会在下次gc时被回收Q然后ThreadLocalMap本n在get和set的时候会考察key为空的EntryQƈ复用它或者清除,从而避免内存泄霌Ӏ?/p> <p>q样看来QHashMap也有一L问题Q但Z么hashMap不这样呢Q因为hashMap的put是业务代码操作的Q因此如果有长期存活的HashMapQ(比如static的)业务代码putq去有义务去removeQ但ThreadLocal的put操作时ThreadLocalcd的,业务代码不知道,因此也不会去做removeQ而ThreadLocalMap本n不知道引用他的某个entry的key的对象什么时候死掉了Q那么如果不用弱引用Q就不知道这个ThreadLocal对象什么时候需要回收了?/p> <p>附:</p> <p>q里补充一下weakReference的用法供参考(当强引用不存在时Q下ơ垃圑֛收会回收弱引用所引用的对象)Q?/p> <div id="jdfxrzn" class="wp_syntax" style="padding-bottom: 0px"> <div id="pptdndb" class="code"><pre class="java" style="color: #000000; font-family: 'Consolas',monospace,'Courier New'"> <span style="color: #436eee">Object</span> o <span style="color: #4a708b">=</span> <span style="font-weight: bold; color: #9400d3">new</span> <span style="color: #436eee">Object</span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> WeakReference<span style="color: #4a708b"><</span>Object<span style="color: #4a708b">></span> ref <span style="color: #4a708b">=</span> <span style="font-weight: bold; color: #9400d3">new</span> WeakReference<span style="color: #4a708b"><</span>Object<span style="color: #4a708b">></span><span style="color: #4a708b">(</span>o<span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="color: #436eee">System</span>.<span style="color: #b8860b">out</span>.<span style="color: #b8860b">println</span><span style="color: #4a708b">(</span>ref.<span style="color: #b8860b">get</span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> o<span style="color: #4a708b">=</span><span style="color: #dc143c">null</span><span style="color: #4a708b">;</span> <span style="color: #436eee">System</span>.<span style="color: #b8860b">gc</span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="color: #436eee">System</span>.<span style="color: #b8860b">out</span>.<span style="color: #b8860b">println</span><span style="color: #4a708b">(</span>ref.<span style="color: #b8860b">get</span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span></pre></div></div> <p>l果输出Q?/p> <div id="fpzjftj" class="wp_syntax" style="padding-bottom: 0px"> <div id="hbvphfx" class="code"><pre class="java" style="color: #000000; font-family: 'Consolas',monospace,'Courier New'"> java.<span style="color: #b8860b">lang</span>.<span style="color: #436eee">Object</span>@de6ced <span style="color: #dc143c">null</span></pre></div></div> <h3>4. FAQ</h3> <p>4.1 Z么一般的ThreadLocal用法都要加staticQ如下:</p> <div id="hrtfpvd" class="wp_syntax" style="padding-bottom: 0px"> <div id="hzbvxfd" class="code"><pre class="java" style="color: #000000; font-family: 'Consolas',monospace,'Courier New'"><span style="font-weight: bold; color: #9400d3">class</span> Test <span style="color: #4a708b">{</span> <span style="font-weight: bold; color: #9400d3">private</span> <span style="font-weight: bold; color: #9400d3">static</span> <span style="font-weight: bold; color: #9400d3">final</span> ThreadLocal<span style="color: #4a708b"><</span>String<span style="color: #4a708b">></span> globalName <span style="color: #4a708b">=</span> <span style="font-weight: bold; color: #9400d3">new</span> ThreadLocal<span style="color: #4a708b"><</span>String<span style="color: #4a708b">></span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="color: #4a708b">}</span></pre></div></div> <div>answer:事实上,不一定是要staticQ但使用它的对象在业务需要范围类一定要是单例。因为根据前面的分析QThreadLocalMap是以ThreadLocal对象为key的,如果TestcM是staticQ也不是单例的,那么两个Test对象有两个keyQ取出来的数据肯定不?/div> <p> </p> <div id="jdfzjzz" class="wp_syntax" style="padding-bottom: 0px"> <div id="zjbdxvd" class="code"><pre class="java" style="color: #000000; font-family: 'Consolas',monospace,'Courier New'"><span style="font-weight: bold; color: #9400d3">class</span> TestThreadLocal<span style="color: #4a708b">{</span> <span style="font-weight: bold; color: #9400d3">public</span> <span style="font-weight: bold; color: #9400d3">static</span> <span style="font-weight: bold; color: #000066">void</span> main<span style="color: #4a708b">(</span><span style="color: #436eee">String</span><span style="color: #4a708b">[</span><span style="color: #4a708b">]</span> args<span style="color: #4a708b">)</span> <span style="color: #4a708b">{</span> Test t1 <span style="color: #4a708b">=</span> <span style="font-weight: bold; color: #9400d3">new</span> Test<span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> Test t2 <span style="color: #4a708b">=</span> <span style="font-weight: bold; color: #9400d3">new</span> Test<span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span>   t1.<span style="color: #b8860b">pool</span>.<span style="color: #b8860b">set</span><span style="color: #4a708b">(</span><span style="color: #247">"a"</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="color: #436eee">System</span>.<span style="color: #b8860b">out</span>.<span style="color: #b8860b">println</span><span style="color: #4a708b">(</span>t1.<span style="color: #b8860b">pool</span>.<span style="color: #b8860b">get</span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="color: #436eee">System</span>.<span style="color: #b8860b">out</span>.<span style="color: #b8860b">println</span><span style="color: #4a708b">(</span>t2.<span style="color: #b8860b">pool</span>.<span style="color: #b8860b">get</span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="color: #4a708b">}</span> <span style="color: #4a708b">}</span> <span style="font-weight: bold; color: #9400d3">class</span> Test<span style="color: #4a708b">{</span> <span style="font-weight: bold; color: #9400d3">public</span> <span style="color: #436eee">ThreadLocal</span> pool <span style="color: #4a708b">=</span> <span style="font-weight: bold; color: #9400d3">new</span> <span style="color: #436eee">ThreadLocal</span><span style="color: #4a708b">(</span><span style="color: #4a708b">)</span><span style="color: #4a708b">;</span> <span style="color: #4a708b">}</span></pre></div></div> <p>输出会是:aQnull</p> <p>原因无需多解释了。唯一需要啰嗦的一ҎQ就一般情况都是单例,上面那个weakreferenceq是必要的,因ؓ作ؓ框架代码Q不能保证正怋用的情况下一个线E有很多ThreadLocalQ如果不用weakreferenceQ就会有内存泄漏的风险,特别是针对线E池中的U程?/p><!-- END entry --><img src ="http://www.tkk7.com/swzhebei/aggbug/379657.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/swzhebei/" target="_blank">透明的鱼</a> 2012-05-31 13:15 <a href="http://www.tkk7.com/swzhebei/archive/2012/05/31/379657.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>对Spring事务配置的五U方式(转蝲Q?/title><link>http://www.tkk7.com/swzhebei/archive/2012/05/30/379548.html</link><dc:creator>透明的鱼</dc:creator><author>透明的鱼</author><pubDate>Wed, 30 May 2012 06:33:00 GMT</pubDate><guid>http://www.tkk7.com/swzhebei/archive/2012/05/30/379548.html</guid><wfw:comment>http://www.tkk7.com/swzhebei/comments/379548.html</wfw:comment><comments>http://www.tkk7.com/swzhebei/archive/2012/05/30/379548.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/swzhebei/comments/commentRss/379548.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/swzhebei/services/trackbacks/379548.html</trackback:ping><description><![CDATA[     摘要: 转蝲Qhttp://www.tkk7.com/f6k66ve/archive/2012/05/30/379516.htmlSpring配置文g中关于事务配|L׃个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式Q一般变化的只是代理机制q部分?   DataSource、TransactionManagerq两部分只是会根...  <a href='http://www.tkk7.com/swzhebei/archive/2012/05/30/379548.html'>阅读全文</a><img src ="http://www.tkk7.com/swzhebei/aggbug/379548.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/swzhebei/" target="_blank">透明的鱼</a> 2012-05-30 14:33 <a href="http://www.tkk7.com/swzhebei/archive/2012/05/30/379548.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>jdbcq接数据库步骤(转蝲Q?/title><link>http://www.tkk7.com/swzhebei/archive/2012/05/15/378157.html</link><dc:creator>透明的鱼</dc:creator><author>透明的鱼</author><pubDate>Tue, 15 May 2012 01:20:00 GMT</pubDate><guid>http://www.tkk7.com/swzhebei/archive/2012/05/15/378157.html</guid><wfw:comment>http://www.tkk7.com/swzhebei/comments/378157.html</wfw:comment><comments>http://www.tkk7.com/swzhebei/archive/2012/05/15/378157.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/swzhebei/comments/commentRss/378157.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/swzhebei/services/trackbacks/378157.html</trackback:ping><description><![CDATA[<div>Q{载地址Q?a href="http://www.tkk7.com/leeYan/archive/2012/05/08/377603.html">http://www.tkk7.com/leeYan/archive/2012/05/08/377603.html</a>Q?br /> <div><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>JDBCq接数据?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>•创徏一个以JDBCq接数据库的E序Q包?/span><span style="color: rgb(192,0,0)" color="#c00000">7</span><span>个步骤:   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span></span><span style="color: rgb(192,0,0)" color="#c00000">1</span><span>?/span><span style="color: red">加蝲JDBC驱动E序Q?/span><span>   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    在连接数据库之前Q首先要加蝲惌q接的数据库的驱动到JVMQJava虚拟机)Q?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    q通过java.lang.Classcȝ静态方法forName(String  className)实现?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    例如Q?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    </span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">try</span></strong></span><span>{   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    </span><span style="color: rgb(0,130,0)" color="#008200">//加蝲MySql的驱动类<span id="pztvnvl" class="Apple"> </span></span><span>  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    Class.forName(</span><span style="color: rgb(0,0,255)" color="#0000ff">"com.mysql.jdbc.Driver"</span><span>) ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    }</span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">catch</span></strong></span><span>(ClassNotFoundException e){   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    System.out.println(</span><span style="color: rgb(0,0,255)" color="#0000ff">"找不到驱动程序类 Q加载驱动失败!"</span><span>);   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    e.printStackTrace() ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    }   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>   成功加蝲后,会将Drivercȝ实例注册到DriverManagercM?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span></span><span style="color: rgb(192,0,0)" color="#c00000">2</span><span>、提供JDBCq接的URL   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>   •q接URL定义了连接数据库时的协议、子协议、数据源标识?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    •书写形式Q协议:子协议:数据源标?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    协议Q在JDBC中L以jdbc开?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    子协议:是桥q接的驱动程序或是数据库理pȝ名称?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    数据源标识:标记扑ֈ数据库来源的地址与连接端口?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    例如Q(MySql的连接URLQ?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    jdbc:mysql:   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>        </span><span style="color: rgb(0,130,0)" color="#008200">//localhost:3306/test?useUnicode=true&characterEncoding=gbk ;<span id="nxhtvrh" class="Apple"> </span></span><span>  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>   useUnicode=</span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">true</span></strong></span><span>Q表CZ用Unicode字符集。如果characterEncoding讄?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>   gb2312或GBKQ本参数必须讄?/span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">true</span></strong></span><span> 。characterEncoding=gbkQ字W编码方式?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span></span><span style="color: rgb(192,0,0)" color="#c00000">3</span><span>?/span><span style="color: red">创徏数据库的q接</span><span>   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    •要连接数据库Q需要向java.sql.DriverManagerhq获得Connection对象Q?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     该对象就代表一个数据库的连接?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    •使用DriverManager的getConnectin(String url , String username ,    </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    String password )Ҏ传入指定的欲q接的数据库的\径、数据库的用户名?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     密码来获得?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     例如Q?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     </span><span style="color: rgb(0,130,0)" color="#008200">//q接MySql数据库,用户名和密码都是root<span id="jjvfhxv" class="Apple"> </span></span><span>  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     String url = </span><span style="color: rgb(0,0,255)" color="#0000ff">"jdbc:mysql://localhost:3306/test"</span><span> ;    </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     String username = </span><span style="color: rgb(0,0,255)" color="#0000ff">"root"</span><span> ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     String password = </span><span style="color: rgb(0,0,255)" color="#0000ff">"root"</span><span> ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     </span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">try</span></strong></span><span>{   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    Connection con =    </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>             DriverManager.getConnection(url , username , password ) ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     }</span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">catch</span></strong></span><span>(SQLException se){   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    System.out.println(</span><span style="color: rgb(0,0,255)" color="#0000ff">"数据库连接失败!"</span><span>);   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    se.printStackTrace() ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     }   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span></span><span style="color: rgb(192,0,0)" color="#c00000">4</span><span style="color: red">、创Z个Statement </span><span>  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    •要执行SQL语句Q必获得java.sql.Statement实例QStatement实例分ؓ以下</span><span style="color: rgb(192,0,0)" color="#c00000">3</span><span>  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     U类型:   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>      </span><span style="color: rgb(192,0,0)" color="#c00000">1</span><span>、执行静态SQL语句。通常通过Statement实例实现?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>      </span><span style="color: rgb(192,0,0)" color="#c00000">2</span><span>、执行动态SQL语句。通常通过PreparedStatement实例实现?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>      </span><span style="color: rgb(192,0,0)" color="#c00000">3</span><span>、执行数据库存储q程。通常通过CallableStatement实例实现?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    具体的实现方式:   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>        Statement stmt = con.createStatement() ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>       PreparedStatement pstmt = con.prepareStatement(sql) ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>       CallableStatement cstmt =    </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>                            con.prepareCall(</span><span style="color: rgb(0,0,255)" color="#0000ff">"{CALL demoSp(? , ?)}"</span><span>) ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span></span><span style="color: rgb(192,0,0)" color="#c00000">5</span><span>?/span><span style="color: red">执行SQL语句 </span><span>  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    Statement接口提供了三U执行SQL语句的方法:executeQuery 、executeUpdate   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>   和execute   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    </span><span style="color: rgb(192,0,0)" color="#c00000">1</span><span>、ResultSet executeQuery(String sqlString)Q执行查询数据库的SQL语句   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>        Q返回一个结果集QResultSetQ对象?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     </span><span style="color: rgb(192,0,0)" color="#c00000">2</span><span>?/span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">int</span></strong></span><span> executeUpdate(String sqlString)Q用于执行INSERT、UPDATE?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>        DELETE语句以及SQL DDL语句Q如QCREATE TABLE和DROP TABLE{?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     </span><span style="color: rgb(192,0,0)" color="#c00000">3</span><span>、execute(sqlString):用于执行q回多个l果集、多个更新计数或二者组合的   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>        语句?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>   具体实现的代码:   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>          ResultSet rs = stmt.executeQuery(</span><span style="color: rgb(0,0,255)" color="#0000ff">"SELECT * FROM ..."</span><span>) ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    </span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">int</span></strong></span><span> rows = stmt.executeUpdate(</span><span style="color: rgb(0,0,255)" color="#0000ff">"INSERT INTO ..."</span><span>) ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    </span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">boolean</span></strong></span><span> flag = stmt.execute(String sql) ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span></span><span style="color: rgb(192,0,0)" color="#c00000">6</span><span>?/span><span style="color: red">处理l果   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    两种情况Q?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     </span><span style="color: rgb(192,0,0)" color="#c00000">1</span><span>、执行更新返回的是本ơ操作媄响到的记录数?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     </span><span style="color: rgb(192,0,0)" color="#c00000">2</span><span>、执行查询返回的l果是一个ResultSet对象?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    • ResultSet包含W合SQL语句中条件的所有行Qƈ且它通过一套getҎ提供了对q些   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>      行中数据的访问?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    • 使用l果集(ResultSetQ对象的讉KҎ获取数据Q?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     </span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">while</span></strong></span><span>(rs.next()){   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>         String name = rs.getString(</span><span style="color: rgb(0,0,255)" color="#0000ff">"name"</span><span>) ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    String pass = rs.getString(</span><span style="color: rgb(192,0,0)" color="#c00000">1</span><span>) ; </span><span style="color: rgb(0,130,0)" color="#008200">// 此方法比较高?span class="Apple"> </span></span><span>  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     }   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>    Q列是从左到右编LQƈ且从?/span><span style="color: rgb(192,0,0)" color="#c00000">1</span><span>开始)   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span></span><span style="color: rgb(192,0,0)" color="#c00000">7</span><span>?/span><span style="color: red">关闭JDBC对象  </span><span>  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     操作完成以后要把所有用的JDBC对象全都关闭Q以释放JDBC资源Q关闭顺序和?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     明顺序相反:   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     </span><span style="color: rgb(192,0,0)" color="#c00000">1</span><span>、关闭记录集   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     </span><span style="color: rgb(192,0,0)" color="#c00000">2</span><span>、关闭声?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>     </span><span style="color: rgb(192,0,0)" color="#c00000">3</span><span>、关闭连接对?nbsp;  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>          </span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">if</span></strong></span><span>(rs != </span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">null</span></strong></span><span>){   </span><span style="color: rgb(0,130,0)" color="#008200">// 关闭记录?span class="Apple"> </span></span><span>  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>        </span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">try</span></strong></span><span>{   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>            rs.close() ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>        }</span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">catch</span></strong></span><span>(SQLException e){   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>            e.printStackTrace() ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>        }   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>          }   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>          </span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">if</span></strong></span><span>(stmt != </span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">null</span></strong></span><span>){   </span><span style="color: rgb(0,130,0)" color="#008200">// 关闭声明<span id="rhjfznl" class="Apple"> </span></span><span>  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>        </span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">try</span></strong></span><span>{   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>            stmt.close() ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>        }</span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">catch</span></strong></span><span>(SQLException e){   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>            e.printStackTrace() ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>        }   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>          }   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>          </span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">if</span></strong></span><span>(conn != </span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">null</span></strong></span><span>){  </span><span style="color: rgb(0,130,0)" color="#008200">// 关闭q接对象<span id="trblpll" class="Apple"> </span></span><span>  </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>         </span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">try</span></strong></span><span>{   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>            conn.close() ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>         }</span><span><strong><span style="color: rgb(127,0,85)" color="#7f0055">catch</span></strong></span><span>(SQLException e){   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>            e.printStackTrace() ;   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>         }   </span></li><li style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px; list-style-type: none; border-right-width: 0px"><span>          }  <br /></span></li></div></div><img src ="http://www.tkk7.com/swzhebei/aggbug/378157.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/swzhebei/" target="_blank">透明的鱼</a> 2012-05-15 09:20 <a href="http://www.tkk7.com/swzhebei/archive/2012/05/15/378157.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>лǵվܻԴȤ</p> <a href="http://www.tkk7.com/" title="亚洲av成人片在线观看">亚洲av成人片在线观看</a> <div class="friend-links"> </div> </div> </footer> վ֩ģ壺 <a href="http://js-jiarui.com" target="_blank">þѾƷһ</a>| <a href="http://hqshimo.com" target="_blank">һѹۿƵİ</a>| <a href="http://alankell.com" target="_blank">þۺϾƷ</a>| <a href="http://91sebo.com" target="_blank">Ļȫ </a>| <a href="http://aijiu868.com" target="_blank">Ƶۿ</a>| <a href="http://714747.com" target="_blank">þ޾Ʒgv</a>| <a href="http://yy6653.com" target="_blank">պ˳ۺձ</a>| <a href="http://kencery.com" target="_blank">ƷŮҹˬˬˬ</a>| <a href="http://cztbm.com" target="_blank">һaɫëƬ</a>| <a href="http://hengzekang.com" target="_blank">޾Ʒ456߲</a>| <a href="http://yutuzb.com" target="_blank">avƷɫ߿ </a>| <a href="http://miyatb.com" target="_blank">777ˬѿһ</a>| <a href="http://144446.com" target="_blank">ɫվWWWĻ</a>| <a href="http://5gi555.com" target="_blank">ŷxƵ</a>| <a href="http://dqzlxgg.com" target="_blank">þ޹vwww </a>| <a href="http://6wss.com" target="_blank">޹˾þۺһ</a>| <a href="http://222xx8.com" target="_blank">þþþѿ</a>| <a href="http://56v6.com" target="_blank">ĻmvߵӰ</a>| <a href="http://0359puju.com" target="_blank">aëƬƵۿ</a>| <a href="http://okgou58.com" target="_blank">һ</a>| <a href="http://codecampo.com" target="_blank">ҹAVƬ</a>| <a href="http://v1s3u5.com" target="_blank">ҪWWWѿƵ</a>| <a href="http://tccqdy.com" target="_blank">ѾѾӰԺ߹ۿ</a>| <a href="http://b2b-chinese.com" target="_blank">ޱAAAר</a>| <a href="http://ywgj50225.com" target="_blank">þþþAVվ</a>| <a href="http://airou08.com" target="_blank">69ʽƵ</a>| <a href="http://ldcatv.com" target="_blank">һƵվ</a>| <a href="http://yytengsheng.com" target="_blank">avƬ߹ۿ</a>| <a href="http://6969xxoo.com" target="_blank">þۺͼۺͼ</a>| <a href="http://qvod-player.com" target="_blank">һѹۿ </a>| <a href="http://c9133.com" target="_blank">þۺ³³</a>| <a href="http://952268.com" target="_blank">ŷ޾þav</a>| <a href="http://zzo8.com" target="_blank">һҴ</a>| <a href="http://chaoxung.com" target="_blank">91ƷѲ߹ۿ</a>| <a href="http://74rr.com" target="_blank">ٸԼA</a>| <a href="http://whyandroid.com" target="_blank">Ůžžվֻ</a>| <a href="http://339258.com" target="_blank">պһһ</a>| <a href="http://7x966.com" target="_blank">޳ѵӰ</a>| <a href="http://zcrencai.com" target="_blank">Ƶ</a>| <a href="http://wenfaka.com" target="_blank">˳77777ɫ߲</a>| <a href="http://gg596gg.com" target="_blank">޾Ʒ·һ߹ۿ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>