Home > Java > Connecting to a database from a java web application

Connecting to a database from a java web application

In these days of numerous java frameworks, we often forget or don’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.

Pre-requisites:

- Latest version of Tomcat (currently 6.0.16)

- A database :-) (In my case, it’s mysql 5.0)

- Appropriate jdbc “driver” jar file for your database. (In my case, it’s mysql jdbc driverr)

Step 1:

- Copy the “jdbc driver” jar file to the TOMCAT_HOME/lib directory. (Usually, C:\Program Files\Apache Software Foundation\Tomcat 6.0.x\lib)

Step 2:

- Open the “context.xml” file of your web application. It can be found under META-INF directory in your web app.

- Your context.xml file might look something like this, initially:

<?xml version=”1.0″ encoding=”UTF-8″?>
<Context path=”/CrickBoss-JSF”/>
Step 3:

- Update your context.xml to register your “jdbc driver”, like this:

<?xml version=”1.0″ encoding=”UTF-8″?>
<Context path=”/CrickBoss-JSF”>
<Resource name=”jdbc/CrickBossDB” auth=”Container”
type=”javax.sql.DataSource” username=”root” password=”mypassword”
driverClassName=”com.mysql.jdbc.Driver”
url=”jdbc:mysql://127.0.0.1:3306/CrickBossDB”
maxActive=”8″ maxIdle=”4″/>
</Context>

Things to note here:

- Give appropriate resource name for your datasource. In my case, it’s “jdbc/CrickBossDB” .

- Use appropriate username and password with respect to your database.

- Provide appropriate driver name in the driverClassName attribute. In my case, it’s “com.mysql.jdbc.Driver”

- Enter your database url correctly. In my case, it’s “jdbc:mysql://127.0.0.1:3306/CrickBossDB”

(Note: Refer your database driver’s documentation to find out the jdbc url. Microsoft SQL Server, for example, expects the string “databaseName” in the jdbc url.)

Don’t worry about other settings. You need not change it at this point of time.

Step 4:

- Register the jdbc datasource in “web.xml”. (This file is located under “WEB-INF” directory.), by adding the following code:

<resource-ref>
<res-ref-name>jdbc/CrickBossDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Note:

Give your datasource name in <res-ref-name> as defined in your context.xml. In my case, it’s “jdbc/CrickBossDB”

Step 5:

We are almost there. It’s now time to use all the configurations we have made.

Now lookup the jdbc datasource we have defined from your servlet, like this:

InitialContext context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup(”java:/comp/env/jdbc/CrickBossDB”);

Once you get the datasource, creating a connection is a piece of cake.

Connection connection = dataSource.getConnection();

That’s it. Now it’s up to you to use this connection and execute some queries.

 

Related posts:

  1. Developing A Simple Java Application With Spring
  2. Developing A Simple Pluggable Java Application
  3. Must have tools for a Java Developer
  4. Five different uses of Java Applets

Categories: Java Tags: ,
  1. March 20th, 2008 at 14:18 | #1

    Dude! You Rawk!

  2. Jaya
    May 13th, 2008 at 20:51 | #2

    Thanks ,, it was helpful for me

  3. July 2nd, 2008 at 03:58 | #3

    dear i want to connect java to mysql
    when i run programm i found folloing error during running

    Exception in thread “main” java.lang.NoClassDefFoundError: org/aspectj/lang/Sign
    ature
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at TestMysql.main(TestMysql.java:9)

    please solv this.

  4. Ngoni Munyikwa
    July 20th, 2008 at 21:09 | #4

    You hammered right to the marrow !!!. have been looking for this exmaple for a long time !!!

    To help others with a JSP script, use the one below and make necessary slight modification on line DataSource dataSource = (DataSource) context.lookup(”java:/comp/env/jdbc/test”);

    Test.jsp

    Read from mySQL Database

    Following records are selected from the ‘jakartaproject’ table. 

    Sr.
    No.

    Project
    Url
    Address

    Description
    of the project

    .

    <a href=”"> 

    .

    <a href=”"> 

  5. Ngoni Munyikwa
    July 20th, 2008 at 21:10 | #5

    Oh, the server was jealousy there !!!!, it ran the script!!!, wish i could help

  6. Samuel Oyet
    October 22nd, 2008 at 16:52 | #6

    This is exact the kind of help I have been looking for. It took me weeks to discover it right here!! Long live the author!

  7. Rajul Konkar
    March 10th, 2009 at 07:23 | #7

    It worked for me, thou
    Is there another way ,Where I can put the database connection part inside web.xml?

  8. Mahesh
    March 21st, 2009 at 08:29 | #8

    Hi it good, can u let know how to connect an desktop application to a web application,
    I mean i have a desktop application which used by a school, i need to connect that to a web portal , which technology we shuold go ahead with..

  1. No trackbacks yet.