?? j-jdbc-3-3.html
字號:
function iOver(image)
{
if (browser=="N3")document[image].src=eval(image + "over.src");
}
// -->
</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 中國所有內容</option><option value="dW">................. </option><option value="dW">dW 中國技術專區:</option><option value="comptZ"> 組件技術</option><option value="javaZ"> Java 技術</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 全部內容</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">產品與服務</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">個性化服務</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 中國網站</a> : <a href="/developerWorks/java/index.shtml">Java</a> : <a href="/developerWorks/cnedu.nsf/java-onlinecourse-bytitle">教學 - 在線教程</a></font>
<br>
<img alt="用 JDBC 管理數據庫連接" 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="章節菜單" 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-2.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-4.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 驅動程序基礎" 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 驅動程序 URL</b></font></td><td width="200" align="right"><font size="1" face="Verdana, Arial, Helvetica"><nobr> 第 3 頁(共9 頁)</nobr></font></td>
</tr>
</table>
<br>
<br>
</p>
<font size="2" face="Verdana, Arial, Helvetica">
<p>
一旦 JDBC 驅動程序用 <code>DriverManager</code> 注冊了,就可以用于建立到數據庫的連接。但是很多不同的驅動程序都可能被實際注冊過,<code>DriverManager</code> 怎樣選擇正確的驅動程序呢?(請記住,單個的 JVM 可能支持多個并發應用程序,它們可能用不同的驅動程序連接到不同的數據庫。)方法非常簡單:每個 JDBC 驅動程序使用一個專門的 JDBC URL(它與 Web 地址有相同的格式)作為自我標識的一種方法。URL 的格式是直觀的,看起來還可能很熟悉:<code>jdbc:<i>sub-protocol</i>:<i>database locator</i></code>。<i>子協議(sub-protocol)</i>與 JDBC 驅動程序有關,可以是 <i>odbc</i>、<i>oracle</i>、<i>db2</i> 等等,根據實際的 JDBC 驅動程序廠商而不同。<i>數據庫定位器(database locator)</i>是與驅動程序有關的指示器,用于唯一指定應用程序要和哪個數據庫進行交互。根據驅動程序的類型,該定位器可能包括主機名、端口和數據庫系統名。
</p>
<p>
當提供了具體的 URL 之后,<code>DriverManager</code> 在已注冊的驅動程序集合中循環,直到其中一個驅動程序識別出指定的 URL。如果沒有發現適合的驅動程序,就拋出一個 <code>SQLException</code>。
下面的清單說明了幾個實際 JDBC URL 的具體示例:</p>
<ul>
<li>
<code>jdbc:odbc:jdbc</code>
</li>
<li>
<code>jdbc:oracle:thin:@persistentjava.com:1521:jdbc";</code>
</li>
<li>
<code>jdbc:db2:jdbc</code>
</li>
</ul>
<p>很多驅動程序(包括 JDBC-ODBC 橋驅動程序)接受在 URL 末尾的附加參數,如用戶名和密碼。
</p>
<p>
給定具體的 JDBC URL,獲取數據庫連接的方法就是在 <code>DriverManager</code> 對象上調用 <code>getConnection()</code>。這種方法有幾種形式:</p>
<ul>
<li>
<code>DriverManager.getConnection(url) ; </code>
</li>
<li>
<code>DriverManager.getConnection(url, username, password) ; </code>
</li>
<li>
<code>DriverManager.getConnection(url, dbproperties) ; </code>
</li>
</ul>
<p>在這里,<code>url</code> 是一個 <code>String</code> 對象,也就是 JDBC URL;用戶名和密碼是 <code>String</code> 對象,它們是 JDBC 應用程序要用來連接到數據源的用戶名和密碼;而 <code>dbproperties</code> 是一個包括所有參數(可能包括用戶名和密碼)的 Java <code>properties</code> 對象,JDBC 驅動程序需要這些參數作成功的連接。
</p>
<p>
現在我們掌握了驅動程序的基礎知識,就可以更詳細地檢查個別的驅動程序類型了。
</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="章節菜單" 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-2.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-4.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">關于 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">聯系 IBM</a></td>
</tr>
</table>
</TD>
</TR>
</TABLE>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -