<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>机械境-信永软件工程师培训学校 &#187; jdbc</title>
	<atom:link href="http://code.oseschool.com/index.php/archives/tag/jdbc/feed" rel="self" type="application/rss+xml" />
	<link>http://code.oseschool.com</link>
	<description>世界：男人、女人、程序员</description>
	<lastBuildDate>Sun, 07 Mar 2010 02:43:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JDBC连接数据库篇</title>
		<link>http://code.oseschool.com/index.php/archives/44</link>
		<comments>http://code.oseschool.com/index.php/archives/44#comments</comments>
		<pubDate>Mon, 24 Aug 2009 13:59:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[jdbc]]></category>

		<guid isPermaLink="false">http://code.oseschool.com/?p=44</guid>
		<description><![CDATA[Java Data Base Connectivity,JDBC——简单来说就是java连接数据库并执行SQL的API，当然不管什么数据库都一样。我们来看看链接代码：

/** @author Sinyee Simrina */
public class DBConnectDemo &#123;
&#160;
	private Connection conn;
&#160;
	/**
	 * Function : just a jdbc connection DEMO
	 *
	 * Product describe: just a jdbc connection DEMO
	 *
	 * on Aug 21, 2009 BY Simrina
	 */
	public void connectDB&#40;&#41; &#123;
&#160;
		//connncet messages
		String user = &#34;root&#34;;
		String password = &#34;root&#34;;
&#160;
		//url for different DB  问题一：连接名是怎么构成的
		String url [...]]]></description>
			<content:encoded><![CDATA[<p>Java Data Base Connectivity,JDBC——简单来说就是java连接数据库并执行SQL的API，当然不管什么数据库都一样。我们来看看链接代码：</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/** @author Sinyee Simrina */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DBConnectDemo <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Connection</span> conn<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Function : just a jdbc connection DEMO
	 *
	 * Product describe: just a jdbc connection DEMO
	 *
	 * on Aug 21, 2009 BY Simrina
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> connectDB<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//connncet messages</span>
		<span style="color: #003399;">String</span> user <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">String</span> password <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//url for different DB  问题一：连接名是怎么构成的</span>
		<span style="color: #003399;">String</span> url <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;jdbc:mysql://localhost:3306/testDB&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//driver for mySQL问题二：加载驱动的字符串到底什么意思？</span>
			<span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.mysql.jdbc.Driver&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			
&nbsp;
			<span style="color: #666666; font-style: italic;">//问题三：为什么我没有用常用Class.forName(&quot;XXX&quot;).newInstance();</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
				conn <span style="color: #339933;">=</span> <span style="color: #003399;">DriverManager</span>.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span>url, user, password<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// TODO to do sth after connnect DB 后续问题：连接了数据之后怎么用</span>
&nbsp;
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">SQLException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
				e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">ClassNotFoundException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>问题一：连接名是怎么构成的</p>
<p>问题二：加载驱动的字符串到底什么意思？</p>
<p>问题三：为什么我没有用常用Class.forName(&#8221;XXX&#8221;).newInstance();</p>
<p>后续问题：连接了数据之后怎么用</p>
<p>搞清楚前三个问题，JDBC以后闭着眼睛也可以写出来了</p>
<p>JDBC 连接方式+数据库类别+对应数据库的连接url</p>
<p>所以除了mySQL:</p>
<p>我们看看其他数据库的连接讯息。。</p>
<p>//url = &#8220;jdbc:oracle:thin:@localhost:1521:testDB&#8221;;</p>
<p>//url = &#8220;jdbc:sqlserver://localhost:1433;DatabaseName=testDB&#8221;</p>
<p>Class.forName(&#8221;com.mysql.jdbc.Driver&#8221;); 其实很简单，看看图</p>
<p><img class="alignnone size-full wp-image-48" title="jdbc配置" src="http://code.oseschool.com/wp-content/uploads/2009/08/jdbc.png" alt="jdbc配置" width="221" height="378" /></p>
<p>看懂了吧，所谓加载驱动就是找到对应的驱动class而已，不需要死记硬背。所以不要再问为什么sqlServer2000和2005的驱动字符串不一样了，微软刚好改变了Driver的存放的包的包名而已——下次发现ClassNotFoundException时候，直接看看你的classpath下有没有这个驱动——或者是驱动的包名类名是不是正确。</p>
<p>我们看看oracle/sqlserver的，现在知道为什么了吧</p>
<p>//Driver for ORACLE</p>
<p>//Class.forName(&#8221;oracle.jdbc.OracleDriver&#8221;);</p>
<p>//Driver for sqlSeriver2000</p>
<p>//Class.forName(&#8221;com.microsoft.jdbc.sqlserver.SQLServerDriver&#8221;);</p>
<p>//Driver for sqlSeriver2005</p>
<p>//Class.forName(&#8221;com.microsoft.sqlserver.jdbc.SQLServerDriver&#8221;);</p>
<p>为什么没有用常用Class.forName(&#8221;XXX&#8221;).newInstance();</p>
<p>newInstance方法的作用，是获取该驱动的一个实例，连接JDBC驱动只要加载就足够了，DriverManager就可以获取到连接</p>
<p>前三步做完了，不管哪一个数据库都可以获取到连接，不管是orcale还是sqlServer,DB2或者Sybase，当然写的时候不要忘记抛错。</p>
]]></content:encoded>
			<wfw:commentRss>http://code.oseschool.com/index.php/archives/44/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

