Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training

Accessing Database from servlets through JDBC!

Accessing Database from servlets through JDBC! Accessing Access Database From Servlet T his article shows you how to access database from servlets. Here I am assuming that you are using win95/98/2000 and running Java Web Server. For the sake of


JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout

Accessing Access Database From Servlet


This article shows you how to access database from servlets. Here I am assuming that you are using win95/98/2000 and running Java Web Server. For the sake of simplicity I have used Microsoft Access Database as backend and Sun's JDBC-ODBC bridge to connect to access data source. First of all download the source code and database used in this tutorial and now follow the following the steps.

  1. Unzip the downloaded file into your favorite directory.
  2. Create an ODBC data source "emaildb" by selecting "emaildb.mdb" database from unzipped folder.
  3. Compile you file, move emaildb.class file to Java Web Servers servlets directory and register the servlet. Now open your browser and run the servlet.

Your browser should display the data from database.

Now we will examine how the code  works.

import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class emaildb extends HttpServlet{
   Connection theConnection;
   private ServletConfig config;

public void init(ServletConfig config)
  throws ServletException{

public void service (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

   HttpSession session = req.getSession(true);


   PrintWriter out = res.getWriter();

   out.println("<HTML><HEAD><TITLE>Emai List.</TITLE>");


   out.println("<BODY bgColor=blanchedalmond text=#008000 topMargin=0>");

   out.println("<P align=center><FONT face=Helvetica><FONT color=fuchsia style=\"BACKGROUND-COLOR: white\"><BIG><BIG>List of E-mail addresses.</BIG></BIG></FONT></P>");

  out.println("<P align=center>");

out.println("<TABLE align=center border=1 cellPadding=1 cellSpacing=1 width=\"75%\">");







   //Loading Sun's JDBC ODBC Driver 

   //Connect to emaildb Data source
   theConnection = DriverManager.getConnection("jdbc:odbc:emaildb", "admin", "");

   Statement theStatement=theConnection.createStatement();

   ResultSet theResult=theStatement.executeQuery("select * from emaillists"); //Select all records from emaillists table.

  //Fetch all the records and print in table



   out.println("<TD>" + theResult.getString(1) + "</TD>");

   out.println("<TD>" + theResult.getString(2) + "</TD>");

   String s=theResult.getString(3);

   out.println("<TD><a href=" + s + ">" + s + "</a></TD>");



  theResult.close();//Close the result set

  theStatement.close();//Close statement

  theConnection.close(); //Close database Connection

  }catch(Exception e){

   out.println(e.getMessage());//Print trapped error.





  public void destroy(){




Here we have used Sun's JDBC-ODBC  Bridge, but we can easily replace the driver in order to connect the servlet to another database. For example, we can use the following code to connect our program to MySQL database.


  theConnection = DriverManager.getConnection("jdbc:mysql://", "mysql","mysql");

JDBC driver for MySQL is available at and is free. Here I have assumed that your mysql server is running at port 3306 of computer, whose IP address is


Share on Google+Share on Google+

Accessing Database from servlets through JDBC!

Posted on: January 30, 2005 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Accessing Database from servlets through JDBC!   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
November 3, 2011
hey just nice.

fantastic solving many problems because of the info that i got from the site.thank you.
November 23, 2011

February 16, 2012
JDBC Connectivity

Database connectivity through default driver is not working. It showing error: Data source name not fount andno default driver
April 26, 2012

what step is tis? Java Web Servers servlets directory and register the servlet
August 12, 2012
This is very useful

This is very useful so thank you very much..
August 20, 2012
information about java

is there any way to view all packages,classes in them and methods in each of them(not in command prompt)..also in
September 20, 2012
Yes ! I found the solution on again

Thank You !
August 9, 2013

please refer docs
maswood ansari
March 14, 2013
jdbc connectivity over servlet using MS-ACCESS

programming running without servlet successfully but when i am using servlet then it is giving following error message [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified