Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Tuesday, 18 June 2013

JDBC in Java - Create JDBC connection in Java

Welcome to very first part of  'JDBC Java Tutorial Series', in this particular blog we will come across 'How to create a JDBC connection in Java' and What are 'Class.foreName() and DriverManager classes in JDBC. In today's discussion we will create a jdbc connection to MySql database using 'com.mysql.jdbc.Driver'.


Project structure

Lets take a look at overall project structure for today's discussion. We have added a 'my-sql-connector,jar' in lib folder to make the connection happen. Please make sure the specified jar file is added to the classpath. The actual code of creating jdbc connection resides under 'CreateConnection.java' class file.



Database

This is the sample database that we have used to create a connection with, execute the code below in your MySql query editor.
-- Dumping database structure for checkjdbc
CREATE DATABASE IF NOT EXISTS `checkjdbc` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `checkjdbc`;




Creating JDBC connection in Java

To create a jdbc connection in java we need to add an appropriate jar file and actual work is done by loading the driver class and calling a connection string to obtainb connection from that, see the code below :

src\com\beingjavaguys\jdbc\CreateConnection.java

This is a simple java class where we have loaded the driver class using Class.foreName() method and that obtained a connection with the help of DriverManager Class. A brief description of these two is given in the rest part of the tutorial.
package com.beingjavaguys.jdbc;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;

public class CreateConnection {

 // defining driver name, mysql-jdbc driver in this case
 String driverName = "com.mysql.jdbc.Driver";
 Connection con = null;

 // database url string with hostname and port on which db is running
 String url = "jdbc:mysql://localhost:3306/";

 public Connection getConnection(String dbName, String username,
   String password) {

  // creating connection url
  String connectionUrl = url + dbName;

  try {
   // registers the specified driver class into memory
   Class.forName(driverName);
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }

  try {
   // returns a connection objcet by selecting an appropriate driver
   // for specified connection URL
   con = DriverManager
     .getConnection(connectionUrl, username, password);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return con;
 }
}



What Class.foreName() does exactly ?

Class.foreName() loads the class that is being passed to it as an string argument. In our case Class.foreName("com.mysql.jdbc.Driver") loads the specified driver class into JVM, so that the class can be used while creating a connection.



What if we have registered more than one driver class ?

In case we have loaded more than one driver class into jvm, this would not create any issue while getting connection. The connection would use only one appropriate class in all of loaded classes. In case there are more than one appropriate driver class for the connection than, very first appropriate class in the order of they are being loaded is used in getting connection and all others will be ignored simply.



What java.sql.DriverManager class does exactly ?

DriverManager is a simple class that manages a set of JDBC driver classes. When getConnection() is called DriverManager attempts to locate a suitable driver for connection from all loaded driver classes.



Implementation class code

This is a simple Java class containing the execution code here so that the actual implementation can be explained.
package com.beingjavaguys.impl;

import com.beingjavaguys.jdbc.CreateConnection;

public class Implementation {
 public static void main(String args[]) {

  // create connection in JDBC
  CreateConnection createConnection = new CreateConnection();
  if ((createConnection.getConnection("checkJDBC", "root", "root") != null)) {
   System.out.println("Connection Created Successfully !");
  }

 }
}



In this particular blog we came across how to create a jdbc connection in java and a brief about what is Class.foreName() and DriverManager class. In upcoming blogs we will see more about JDBC including JDBC Create Table & JDBC Inset Query & JDBC Delete and JDBC Update Implementation in Java.







Thanks for reading !
Being Java Guys Team

JDBC in Java Create Connection Example Project" from "SkyDrive"





0 comments:

Post a Comment

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft