Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Sourcecode Searchengine Connecting to the Database Using JDBC and Pure Java driver

 
 

Share on Google+Share on Google+

Connecting to the Database Using JDBC and Pure Java driver

Advertisement
Connecting to the Database Using JDBC and Pure Java driver Connecting to the Database JDBC Driver In our search engine we are using MySQL database server and MM.MySQL Driver for connecting our application to the database. MM.MySQL Driver is

Connecting to the Database

JDBC Driver

In our search engine we are using MySQL database server and MM.MySQL Driver for connecting our application to the database. MM.MySQL Driver is available at http://www.worldserver.com/mm/mysql. As stated earlier MM.MySQL Driver is 100% pure Java Driver for MySQL and is distributed under the GNUGLP. Developers can use this driver to make connections to MySQL server from both Java Applications and Applets. MM.MySQL Driver provide all the JDBC features. It automatically re-connect if connection is failed. It also provide Unicode support and support for varied character encoding.

Connecting to the Database

All the classes and interfaces of JDBC API are defined in java.sql package, so it is necessary to import java.sql.* package in our application.

In this search engine our application communicate with database using JDBC and for making connection we are using MM.MySQL JDBC Driver.

First of all we obtain a Connection object that represents the network connection to the database. Driver Manager plays very important role of establishing connection. Class.forName("org.git.mm.mysql.Driver").newInstance() tells the Driver Manager to use MM.MySQL JDBC Driver. To obtain a connection to the Database getConnection() method of DriverManager class is used, which provides database connectivity to our search engine.

 

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

  public class search extends HttpServlet{

  //Connection object

  Connection theConnection;

  private String dbURL =   "jdbc:mysql://10.0.0.1/javadevelopers?user=username&password=password";

  //private String servletURL="/servlet";

  private String servletURL="http://www.webappcabaret.com/javadevelopers";

  //private String image="/images/open.gif";
  private String image="http://www.roseindia.net/images/open.gif";

   public void init() throws ServletException{
   makeConnection();
  }

 

  public void makeConnection(){

  //System.out.println("Opening db connection");

  try{

  Class.forName("org.gjt.mm.mysql.Driver").newInstance();
  theConnection = DriverManager.getConnection(dbURL);
  }
  catch (Exception e)
  {

   // System.out.println(e.getMessage());

   }

  // System.out.println("Connected to the database");

  }

  public void destory(){
  try{
  theConnection.close();
  }catch(Exception e){}

  }

The close() method of Connection object closes the database connection.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 30, 2005

Related Tutorials

Discuss: Connecting to the Database Using JDBC and Pure Java driver  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com