<?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>SolitaryGeek &#187; tomcat</title>
	<atom:link href="http://solitarygeek.com/tag/tomcat/feed" rel="self" type="application/rss+xml" />
	<link>http://solitarygeek.com</link>
	<description>James Selvakumar&#039;s Blog</description>
	<lastBuildDate>Tue, 21 Dec 2010 04:35:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Connecting to a database from a java web application</title>
		<link>http://solitarygeek.com/java/connecting-to-a-database-from-a-java-web-application</link>
		<comments>http://solitarygeek.com/java/connecting-to-a-database-from-a-java-web-application#comments</comments>
		<pubDate>Wed, 12 Mar 2008 17:14:02 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[tomcat]]></category>

		<guid isPermaLink="false">http://jamesselvakumar.wordpress.com/?p=44</guid>
		<description><![CDATA[<p>In these days of numerous java frameworks, we often forget or don&#8217;t care about some simple things. Though this post might not be very interesting to most of you, it might help some of those to whom this might be the information they are looking for. So bear with me.</p> <p> Pre-requisites:</p> <p>- Latest version of Tomcat (currently 6.0.16)</p> <p>- A database (In my case, it&#8217;s mysql 5.0)</p> <p>- Appropriate jdbc &#8220;driver&#8221; jar file for your database. (In my case, it&#8217;s mysql jdbc driverr)</p> <p>Step 1:</p> <p>- Copy the &#8220;jdbc driver&#8221; jar file to the TOMCAT_HOME/lib directory. (Usually, C:\Program Files\Apache <span style="color:#777"> . . . &#8594; Read More: <a href="http://solitarygeek.com/java/connecting-to-a-database-from-a-java-web-application">Connecting to a database from a java web application</a></span>]]></description>
			<content:encoded><![CDATA[<p>In these days of numerous java frameworks, we often forget or don&#8217;t care about some simple things. Though this post might not be very interesting to most of you, it might help some of those to whom this might be the information they are looking for. So bear with me.</p>
<p><strong> Pre-requisites:</strong></p>
<p>- Latest version of Tomcat (currently 6.0.16)</p>
<p>- A database <img src='http://solitarygeek.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  (In my case, it&#8217;s mysql 5.0)</p>
<p>- Appropriate jdbc &#8220;driver&#8221; jar file for your database. (In my case, it&#8217;s mysql jdbc driverr)</p>
<p><span id="more-181"></span><strong>Step 1:</strong></p>
<p>- Copy the &#8220;jdbc driver&#8221; jar file to the TOMCAT_HOME/lib directory.  (Usually, C:\Program Files\Apache Software Foundation\Tomcat 6.0.x\lib)</p>

<p><strong>Step 2:</strong></p>
<p>- Open the &#8220;context.xml&#8221; file of your web application. It can be found under META-INF directory in your web app.</p>
<p>- Your context.xml file might look something like this, initially:</p>
<p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243;?&gt;<br />
&lt;Context path=&#8221;/CrickBoss-JSF&#8221;/&gt;<br />
<strong>Step 3:</strong></p>
<p>- Update your context.xml to register your &#8220;jdbc driver&#8221;, like this:</p>
<p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243;?&gt;<br />
&lt;Context path=&#8221;/CrickBoss-JSF&#8221;&gt;<br />
&lt;Resource name=&#8221;jdbc/CrickBossDB&#8221; auth=&#8221;Container&#8221;<br />
type=&#8221;javax.sql.DataSource&#8221; username=&#8221;root&#8221; password=&#8221;mypassword&#8221;<br />
driverClassName=&#8221;com.mysql.jdbc.Driver&#8221;<br />
url=&#8221;jdbc:mysql://127.0.0.1:3306/CrickBossDB&#8221;<br />
maxActive=&#8221;8&#8243; maxIdle=&#8221;4&#8243;/&gt;<br />
&lt;/Context&gt;</p>
<p>Things to note here:</p>
<p>- Give appropriate resource name for your datasource. In my case, it&#8217;s <strong>&#8220;jdbc/CrickBossDB&#8221; .</strong></p>
<p>- Use appropriate username and password with respect to your database.</p>
<p>- Provide appropriate driver name in the driverClassName attribute. In my case, it&#8217;s <strong>&#8220;com.mysql.jdbc.Driver&#8221;</strong></p>
<p>- Enter your database url correctly. In my case, it&#8217;s <strong>&#8220;jdbc:mysql://127.0.0.1:3306/CrickBossDB&#8221;</strong></p>
<p>(Note: Refer your database driver&#8217;s documentation to find out the jdbc url. Microsoft SQL Server, for example, expects the string &#8220;databaseName&#8221; in the jdbc url.)</p>
<p>Don&#8217;t worry about other settings. You need not change it at this point of time.</p>
<p><strong>Step 4:</strong></p>
<p>- Register the jdbc datasource in &#8220;web.xml&#8221;. (This file is located under &#8220;WEB-INF&#8221; directory.), by adding the following code:</p>
<p>&lt;resource-ref&gt;<br />
&lt;res-ref-name&gt;jdbc/CrickBossDB&lt;/res-ref-name&gt;<br />
&lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;<br />
&lt;res-auth&gt;Container&lt;/res-auth&gt;<br />
&lt;/resource-ref&gt;</p>
<p><em>Note:</em></p>
<p>Give your datasource name in &lt;res-ref-name&gt; as defined in your context.xml. In my case, it&#8217;s <strong>&#8220;jdbc/CrickBossDB&#8221;</strong></p>
<p><strong>Step 5:</strong></p>
<p>We are almost there. It&#8217;s now time to use all the configurations we have made.</p>
<p>Now lookup the jdbc datasource we have defined from your servlet, like this:</p>
<p><em>InitialContext context = new InitialContext();<br />
DataSource dataSource = (DataSource) context.lookup(&#8220;java:/comp/env/jdbc/CrickBossDB&#8221;);</em></p>
<p>Once you get the datasource, creating a connection is a piece of cake.</p>
<p><em>Connection connection = dataSource.getConnection();</em></p>
<p>That&#8217;s it. Now it&#8217;s up to you to use this connection and execute some queries.</p>
]]></content:encoded>
			<wfw:commentRss>http://solitarygeek.com/java/connecting-to-a-database-from-a-java-web-application/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

