?? j-jdbc-3-2.html
字號:
}
// -->
</script><script src="../i/emailfriend.js" language="javascript"></script><a href="#navskip"><img border="0" alt="Skip to main content" src="../i/c.gif"></a>
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td class="tbgc" width="160"><a href="/developerWorks/cgi-bin/click.cgi?url=http://www.ibm.com/cn"><img height="47" width="160" alt="IBM" border="0" src="../i/ibm-logo.gif"></a></td><td class="tbg" width="195"><font color="#99ccff" size="2"><b>China</b></font><a href="#main"><img alt="Skip to main content" height="1" width="195" border="0" src="../i/c.gif"></a></td><td valign="top" align="right" class="tbgc" width="100%">
<table cellspacing="0" cellpadding="0" border="0">
<form name="form1" id="form1" action="http://202.95.2.149/cgi-bin/dWsearch.pl" method="get">
<tr>
<td colspan="6"><img alt="" height="4" width="390" border="0" src="../i/c.gif"></td>
</tr>
<tr valign="middle">
<td class="dwsearch" height="8"><select class="dwsearch" name="selScope"><option value="dW">dW 中國所有內(nèi)容</option><option value="dW">................. </option><option value="dW">dW 中國技術(shù)專區(qū):</option><option value="comptZ"> 組件技術(shù)</option><option value="javaZ"> Java 技術(shù)</option><option value="linuxZ"> Linux</option><option value="xmlZ"> XML</option><option value="linuxZ">dW 中國特別專題:</option><option value="securityZ"> 安全</option><option value="unicodeZ"> Unicode</option><option value="all">IBM 全部內(nèi)容</option></select></td><td class="small"><img alt="" border="0" height="1" width="5" src="../i/c.gif"></td><td><input maxlength="100" size="15" height="15" name="UserRestriction" class="dwsearch" type="text"></td><td><img alt="" height="1" width="5" border="0" src="../i/c.gif"></td><td width="64"><input alt="搜索" name="Search" value="Search" border="0" height="23" width="64" src="../i/search.gif" type="image"></td><td valign="top"><img alt="" height="1" width="34" border="0" src="../i/c.gif"></td>
</tr>
<tr valign="top">
<td class="small" colspan="6"><a target="_blank" style="color: #ffffff;" href="http://202.95.2.149/help.htm">高級搜索</a><img alt="" height="1" width="1" border="0" src="../i/c.gif"></td>
</tr>
<tr>
<td colspan="6"><img alt="" height="4" width="390" border="0" src="../i/c.gif"></td>
</tr>
</form>
</table>
</td>
</tr>
<tr>
<td class="hbg" height="21" width="160"></td><td class="bbg" valign="top" height="21" colspan="2"> <a href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/" class="mainlink">IBM 主頁</a><span class="divider"> | </span><a href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/products/index.shtml" class="mainlink">產(chǎn)品與服務(wù)</a><span class="divider"> | </span><a href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/support/index.shtml" class="mainlink">支持與下載</a><span class="divider"> | </span><a href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/account/index.shtml" class="mainlink">個性化服務(wù)</a></td>
</tr>
</table>
<TABLE width="100%" cellspacing="0" cellpadding="0" border="0">
<TR>
<TD bgcolor="#ffffff" height="1" width="150"><IMG src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#cc6633" height="3" width="150"><IMG src="../i/c.gif" width="150" height="3"></TD>
</TR>
<TR>
<TD bgcolor="#333333" height="1" width="150"><IMG src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#000000" height="1" width="150"><IMG src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#ffffff" height="1" width="150"><IMG src="../i/c.gif" width="150" height="2"></TD>
</TR>
</TABLE>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><font face="Verdana, sans-serif" size="-1"><a href="http://www.ibm.com/">IBM</a> : <a href="/developerWorks/index.shtml">developerWorks 中國網(wǎng)站</a> : <a href="/developerWorks/java/index.shtml">Java</a> : <a href="/developerWorks/cnedu.nsf/java-onlinecourse-bytitle">教學(xué) - 在線教程</a></font>
<br>
<img alt="用 JDBC 管理數(shù)據(jù)庫連接" src="imagemaster/masthead.jpg"></td><td width="*" valign="bottom" align="right"><a border="0" href="j-jdbc.zip"><img alt="下載 ZIP 文件" border="0" src="../i/icon-zip.gif"></a><a target="_blank" href="../tutorial_eng/index.html" border="0"><img alt="英文原文" border="0" src="../i/icon-source.gif"></a>
</td>
</tr>
<tr>
<td height="2" colspan="2"><img height="2" width="1" src="../i/c.gif"></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜單" border="0" src="../i/main.gif" name="topmain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index3.html"><img alt="章節(jié)菜單" border="0" src="../i/section.gif" name="topsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-jdbc-8-3.html"><img alt="給出此教程的反饋意見" border="0" src="../i/feedback.gif" name="topfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topprevious'); iOver('bottomprevious'); self.status=previousblurb; return true;" onMouseOut="iOut('topprevious'); iOut('bottomprevious'); self.status=''; return true;" href="j-jdbc-3-1.html"><img alt="上頁" border="0" src="../i/previous.gif" name="topprevious"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-jdbc-3-3.html"><img alt="下頁" border="0" src="../i/next.gif" name="topnext"></a></TD>
</TR>
</TABLE>
<table bgcolor="ffffff" cellspacing="0" cellpadding="2" border="0" height="400" width="100%">
<tr valign="bottom">
<a name="navskip"></a><td height="25" colspan="4"><img alt="3.JDBC 驅(qū)動程序基礎(chǔ)" src="imagemaster/titlebar3.jpg" border="0" height="25" width="562"></td>
</tr>
<tr>
<td bgcolor="ffffff" width="15"> </td><td bgcolor="ffffff" width="12"> </td><td valign="top" align="left" bgcolor="ffffff" width="*">
<p>
<br>
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="90%"><font size="4" face="Verdana, Arial, Helvetica"><b>注冊 JDBC 驅(qū)動程序</b></font></td><td width="200" align="right"><font size="1" face="Verdana, Arial, Helvetica"><nobr> 第 2 頁(共9 頁)</nobr></font></td>
</tr>
</table>
<br>
<br>
</p>
<font size="2" face="Verdana, Arial, Helvetica">
<p>
在 Java 應(yīng)用程序和數(shù)據(jù)庫之間創(chuàng)建連接的過程的第一步是用“Java 虛擬機(jī)”(Java virtual machine(JVM))注冊 JDBC 驅(qū)動程序,Java 應(yīng)用程序就是在此 JVM 中運(yùn)行的。在傳統(tǒng)的連接機(jī)制(相對于后面中討論的 <code>DataSource</code> 連接機(jī)制)中,連接和所有數(shù)據(jù)庫通訊是由 <code>DriverManager</code> 對象控制的。要建立一個連接,必須用 <code>DriverManager</code> 對象注冊針對目標(biāo)數(shù)據(jù)庫的適當(dāng)?shù)?JDBC 驅(qū)動程序。
</p>
<p>
JDBC 規(guī)范(更多細(xì)節(jié)請參閱 <i>JDBC API 教程和參考</i><a href="j-jdbc-8-2.html">參考資料</a>),規(guī)定了 JDBC 驅(qū)動程序在裝入 JVM 時應(yīng)該自動用 <code>DriverManager</code> 對象注冊自身。例如,下面的代碼段使用一個靜態(tài)的初始化程序來首先創(chuàng)建一個 <code>persistentjava</code> JDBC 驅(qū)動程序的實(shí)例,然后用 <code>DriverManager</code> 將其注冊。</p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
static {
java.sql.DriverManager.registerDriver(new com.persistentjava.JdbcDriver()) ;
}
</code>
</pre>
<p>
注冊驅(qū)動程序就是將驅(qū)動程序類裝入 JVM 的工作,它可以用幾種不同的方法完成。一種方法就是使用類調(diào)用器(ClassLoader)<code>Class.forName(com.persistentjava.JdbcDriver);</code>。另一種方法不太常見,是使用 <code>jdbc.drivers</code> 系統(tǒng)屬性。這種方法可以有三種不同的方式:</p>
<ul>
<li>從命令行:
<br>
<code>java -Djdb.drivers=com.persistentjava.JdbcDriver Connect</code>
<br>
<br>
</li>
<li>在 java 應(yīng)用程序中:<br>
<code>System.setProperty("jdbc.drivers", "com.persistentjava.JdbcDriver") ;</code>
<br>
<br>
</li>
<li>通過在系統(tǒng)屬性文件中設(shè)置 <code>jdbc.drivers</code> 屬性,這通常和系統(tǒng)有關(guān)。</li>
</ul>
<p>通過用冒號隔離驅(qū)動程序,多個驅(qū)動程序可以使用上面提到的系統(tǒng)屬性方法注冊。使用系統(tǒng)屬性方法的一個好處是驅(qū)動程序很容易在 JVM 中調(diào)入和調(diào)出,而不用修改任何代碼(或至少是修改最少的代碼)。如果注冊了多個驅(qū)動程序,它們的優(yōu)先順序是:1)JVM 初始化時通過 <code>jdbc.drivers</code> 屬性注冊的 JDBC 驅(qū)動程序,2)動態(tài)裝入的 JDBC 驅(qū)動程序。因?yàn)?<code>jdbc.drivers</code> 屬性只在 <code>DriverManager()</code> 方法的第一次調(diào)用時檢查一次,確保所有驅(qū)動程序在建立數(shù)據(jù)庫連接之前被正確注冊是很重要的。
</p>
<p>
然而,不是所有的 JVM 都平等創(chuàng)建,一些 JVM 并不符合 JVM 規(guī)范。結(jié)果是,靜態(tài)初始化程序并不如預(yù)期的那樣運(yùn)行。這導(dǎo)致了注冊 JDBC 驅(qū)動程序的多種方法,包括:</p>
<ul>
<li>
<code>Class.forName("com.persistentjava.JdbcDriver").newInstance() ; </code>
</li>
<li>
<code>DriverManager.registerDriver(new com.persistentjava.JdbcDriver()) ; </code>
</li>
</ul>
<p>這些替代方案應(yīng)該在所有 JVM 中都運(yùn)行良好,所以您應(yīng)該放心地在眾多可能出現(xiàn)的 JVM 中使用它們工作。最后一個問題是 <code>Class.forname()</code> 可能拋出一個 <code>ClassNotFoundException</code>,所以您需要在合適的異常處理程序中包裝注冊代碼。
</p>
<br>
</font></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜單" border="0" src="../i/main.gif" name="bottommain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index3.html"><img alt="章節(jié)菜單" border="0" src="../i/section.gif" name="bottomsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-jdbc-8-3.html"><img alt="給出此教程的反饋意見" border="0" src="../i/feedback.gif" name="bottomfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topprevious'); iOver('bottomprevious'); self.status=previousblurb; return true;" onMouseOut="iOut('topprevious'); iOut('bottomprevious'); self.status=''; return true;" href="j-jdbc-3-1.html"><img alt="上頁" border="0" src="../i/previous.gif" name="bottomprevious"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-jdbc-3-3.html"><img alt="下頁" border="0" src="../i/next.gif" name="bottomnext"></a></TD>
</TR>
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
</TABLE>
<TABLE width="100%" cellpadding="0" cellspacing="0" border="0">
<TR>
<TD width="100%">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><img alt="" height="1" width="1" src="../i/c.gif"></td>
</tr>
<tr valign="top">
<td class="bbg" height="21"> <a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/index.shtml">關(guān)于 IBM</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/privacy/index.shtml">隱私條約</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/legal/index.shtml">法律條款</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/contact/index.shtml">聯(lián)系 IBM</a></td>
</tr>
</table>
</TD>
</TR>
</TABLE>
</body>
</html>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -