<?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>Alex's Blog &#187; 数据库</title>
	<atom:link href="http://www.hialex.net/tags/%e6%95%b0%e6%8d%ae%e5%ba%93/feed" rel="self" type="application/rss+xml" />
	<link>http://www.hialex.net</link>
	<description>记录生活与工作中的点点滴滴</description>
	<lastBuildDate>Sat, 09 Jan 2010 12:37:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JDBC连接Mysql数据库</title>
		<link>http://www.hialex.net/2008/02/jdbc-link-to-mysql.html</link>
		<comments>http://www.hialex.net/2008/02/jdbc-link-to-mysql.html#comments</comments>
		<pubDate>Tue, 19 Feb 2008 12:44:36 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[工作学习]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[jsp]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://www.hialex.net/2008/02/jdbc-link-to-mysql.html</guid>
		<description><![CDATA[看了JDBC连接技术视频，特别想试着连连看，但是视频中老师是连接的Oracle数据库，而我的机器上并没有装Oracle，所以想连别的数据库，比如说Mysql。
按照视频中老师讲解的方法，JDBC连接任何数据库搞定两个步骤即可，一是找到欲连接的数据库的JDBC驱动，二是写好数据库连接地址，所以我照着这两点尝试着连Mysql：
第一步：找到Mysql的JDBC连接驱动（下载地址）。
第二步：将下载下来的文件解压，将其中的mysql-connector-java-5.1.5-bin.jar拷贝到%TOMCAT_HOME%\lib目录下。
第三步：在要连接数据库的JSP页面中填写参数，具体如下：

数据库驱动程序为：&#8221;com.mysql.jdbc.Driver&#8221;
数据库连接地址为：&#8221;jdbc:mysql://localhost/[数据库名]&#8220;

完成以上步骤，JDBC连接Mysql数据库的核心步骤基本就搞定了，剩下的就是JDBC的一些固定步骤了。
我在网上搜索jdbc连接mysql数据库的方法时发现了我在视频中学到的方法并不是和网上的方法完全一致，有点小的差别，但是我用以上的方法是能成功连接的。
附：JDBC连接Mysql数据库代码

&#60;%
//定义数据库操作的常量及对象
//数据库驱动程序
final    String    DBDRIVER            =    &#34;com.mysql.jdbc.Driver&#34;    ;
//数据库连接地址
final    String    DBURL                =   [...]]]></description>
			<content:encoded><![CDATA[<p>看了JDBC连接技术视频，特别想试着连连看，但是视频中老师是连接的Oracle数据库，而我的机器上并没有装Oracle，所以想连别的数据库，比如说Mysql。</p>
<p>按照视频中老师讲解的方法，JDBC连接任何数据库搞定两个步骤即可，一是找到欲连接的数据库的JDBC驱动，二是写好数据库连接地址，所以我照着这两点尝试着连Mysql：</p>
<p>第一步：找到Mysql的JDBC连接驱动（<a href="http://dev.mysql.com/downloads/connector/j/5.0.html" target="_blank">下载地址</a>）。</p>
<p>第二步：将下载下来的文件解压，将其中的mysql-connector-java-5.1.5-bin.jar拷贝到%TOMCAT_HOME%\lib目录下。</p>
<p>第三步：在要连接数据库的JSP页面中填写参数，具体如下：</p>
<ul>
<li>数据库驱动程序为：&#8221;com.mysql.jdbc.Driver&#8221;</li>
<li>数据库连接地址为：&#8221;jdbc:mysql://localhost/[数据库名]&#8220;</li>
</ul>
<p>完成以上步骤，JDBC连接Mysql数据库的核心步骤基本就搞定了，剩下的就是JDBC的一些固定步骤了。</p>
<p>我在网上搜索jdbc连接mysql数据库的方法时发现了我在视频中学到的方法并不是和网上的方法完全一致，有点小的差别，但是我用以上的方法是能成功连接的。</p>
<p>附：JDBC连接Mysql数据库代码</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>
<span style="color: #666666; font-style: italic;">//定义数据库操作的常量及对象</span>
<span style="color: #666666; font-style: italic;">//数据库驱动程序</span>
<span style="color: #000000; font-weight: bold;">final</span>    <span style="color: #003399;">String</span>    DBDRIVER            <span style="color: #339933;">=</span>    <span style="color: #0000ff;">&quot;com.mysql.jdbc.Driver&quot;</span>    <span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//数据库连接地址</span>
<span style="color: #000000; font-weight: bold;">final</span>    <span style="color: #003399;">String</span>    DBURL                <span style="color: #339933;">=</span>    <span style="color: #0000ff;">&quot;jdbc:mysql://localhost/jsp&quot;</span>    <span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//数据库用户名</span>
<span style="color: #000000; font-weight: bold;">final</span>    <span style="color: #003399;">String</span>    DBUSER            <span style="color: #339933;">=</span>    <span style="color: #0000ff;">&quot;root&quot;</span>    <span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//数据库密码</span>
<span style="color: #000000; font-weight: bold;">final</span>    <span style="color: #003399;">String</span>    DBPASSWORD        <span style="color: #339933;">=</span>    <span style="color: #0000ff;">&quot;root&quot;</span>    <span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//声明一个数据库连接对象</span>
<span style="color: #003399;">Connection</span>        conn                <span style="color: #339933;">=</span>    <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//声明一个数据库操作对象</span>
<span style="color: #003399;">Statement</span>    stmt                    <span style="color: #339933;">=</span>    <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//声明一个结果集对象</span>
<span style="color: #003399;">ResultSet</span>    rs                        <span style="color: #339933;">=</span>    <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span>        sql                        <span style="color: #339933;">=</span>    <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #339933;">%&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;%</span>
<span style="color: #666666; font-style: italic;">//进行数据库操作</span>
<span style="color: #000000; font-weight: bold;">try</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//加载驱动程序</span>
<span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span>DBDRIVER<span style="color: #009900;">&#41;</span>     <span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//连接数据库</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>DBURL,DBUSER,DBPASSWORD<span style="color: #009900;">&#41;</span>    <span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//实例化数据库操作对象</span>
stmt    <span style="color: #339933;">=</span>    conn.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>    <span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//编写SQL语句</span>
sql        <span style="color: #339933;">=</span>    <span style="color: #0000ff;">&quot; SELECT name FROM person WHERE id='&quot;</span><span style="color: #339933;">+</span>name<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;' AND password='&quot;</span><span style="color: #339933;">+</span>password<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//查询记录</span>
rs    <span style="color: #339933;">=</span>    stmt.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span>sql<span style="color: #009900;">&#41;</span>    <span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>rs.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//如果有记录，执行此段代码</span>
flag     <span style="color: #339933;">=</span>    <span style="color: #000066; font-weight: bold;">true</span>    <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">//依次关闭</span>
rs.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>    <span style="color: #339933;">;</span>
stmt.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>    <span style="color: #339933;">;</span>
conn.<span style="color: #006633;">close</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: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">%&gt;</span></pre></div></div>

<p><a href="http://17fav.com/?url=http%3A%2F%2Fwww.hialex.net%2F2008%2F02%2Fjdbc-link-to-mysql.html&title=JDBC%E8%BF%9E%E6%8E%A5Mysql%E6%95%B0%E6%8D%AE%E5%BA%93" title="用 17fav 收藏和分享本文"><img src="http://17fav.com/i/bookmark.gif" alt="17fav 收藏本文" /></a></p><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>2008-03-01 -- <a href="http://www.hialex.net/2008/03/usage-of-jdbc.html" title="(九)JDBC操作技术">(九)JDBC操作技术</a> (0)</li><li>2009-03-08 -- <a href="http://www.hialex.net/2009/03/mysql-notes-3.html" title="MySQL读书笔记(3)">MySQL读书笔记(3)</a> (0)</li><li>2009-03-03 -- <a href="http://www.hialex.net/2009/03/mysql-notes-2.html" title="MySQL读书笔记(2)">MySQL读书笔记(2)</a> (0)</li><li>2009-03-02 -- <a href="http://www.hialex.net/2009/03/mysql-notes-1.html" title="MySQL读书笔记(1)">MySQL读书笔记(1)</a> (0)</li><li>2008-04-02 -- <a href="http://www.hialex.net/2008/04/servlet-note.html" title="Servlet程序设计">Servlet程序设计</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.hialex.net/2008/02/jdbc-link-to-mysql.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
