JDBC Components

JDBC stands for Java Database Connectivity. For connectivity with the database we uses JDBC.

Ads

Tutorials   
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
Ads

JDBC Components

  

JDBC stands for Java Database Connectivity. For connectivity with the database we uses JDBC. It establish connection to access the database. This provides a set of classes in the java.sql package for Java applications to communicate with databases. Mostly databases used a language called SQL. SQL stands for Structured Query Language. JDBC gives you the opportunity to communicate with standard database. JDBC includes four components:

1. The JDBC API

The JDBC API gives access of programming data from the Java. To use this, applications can execute SQL statements and retrieve results and updation to the database. The JDBC API is part of the Java platform, it includes the Java Standard Edition.

2. JDBC Driver Manager

The JDBC DriverManager is the class in JDBC API. The objects of this class can connect Java applications to a JDBC driver. DriverManager is the very important part of the JDBC architecture.

3. JDBC Test Suite

The JDBC driver test suite helps JDBC drivers to run your program. They are not exhaustive,they do exercise with important features in the JDBC API.

4. JDBC-ODBC Bridge

The Java Software bridge provides JDBC access via ODBC drivers.You have to load ODBC binary code for client machines for using this driver. This driver is very important for application server code has to be in Java in a three-tier architecture.

The program below describes how to run the JDBC program with MySql. JDBCExample.java is the program to understand how to establish the connection with database. The Url, Connection, Driver, Statement, Resultset etc. and how to process the java code with database is mentioned here.

JDBCExample.java

import java.sql.*;
public
class JDBCExample {
 
public static void main(String a[])
  
{
 
Connection conn = null;
  String url =
"jdbc:mysql://localhost:3306/";
  String dbName =
"vineej";
  String driver =
"com.mysql.jdbc.Driver";
  String userName =
"vineej";
  String password =
"no";
 
try {
 
Class.forName(driver).newInstance();
  Connection con =DriverManager.getConnection(url+dbName,userName,password);
  Statement st = con.createStatement();
  ResultSet rs = st.executeQuery("SELECT * from Employee");
  
System.out.println("Results");
  
while( rs.next() ) {
   
String data = rs.getString(1);
    System.out.println( data );
    }
  st.close();
  }
 
catch( Exception e ) {
 
System.out.println(e.getMessage());
  e.printStackTrace();
  }
  }
}
 

Description of program
This program making the connection between MySQL database and java with the help of many types of APIs interfaces.First connected it and then execute a query.It shows the result, like in above example it displays the all employees in the existing table Employee.

Description of code

1. Connection
An interface in java.sql package that provides connection with the database like- MySQL and java files. The SQL statements are executed within the context of the Connection interface.

2. Class.forName(String driver)
forName() is static method of the "Class" class . This loads the driver class and returns Class instance. It takes string type value as an argument and matches the class with string.

3. DriverManager
This class of java.sql package controls the JDBC drivers. Each driver has to be registered with this class.

4. getConnection(String url, String userName, String password)
This static method of DriverManager class makes a connection with database url. It takes these given arguments of string type.

url - Database url where stored or created your database
userName - User name of MySQL
password -Password of MySQL

5. con.close()
This method of Connection interface is used for closing the connection. All the resources will be free occupied by the database.

6. printStackTrace()
The method is used to show error messages. If the connection is not connected then it throws the exception and print the message.

Java Database Connectivity Steps-

Some steps are given below of the JDBC

1. First import the java.sql package

import java.sql.*;

The star '*' indicates that all of the classes in the java.sql and java.io packages are to be imported.

2. Loading a database driver

In this step, the driver class loaded by calling Class.forName() with the Driver class. Class is a class in java.lang package.

 Class.forName("com.mysql.jdbc.Driver");

3. Creating a jdbc Connection

The objects defined by DriverManager class to establish the applications with the JDBC driver. This class manages the JDBC drivers which is installed on the system. getConnection() is the method by which it can connect. It uses the username, password, and a jdbc url to make the connection with database.

try{
   Connection con=DriverManager.getConnection(url,"userName","password");
}
catch( SQLException x ){
   System.out.println( "e.printStackTrace" );
}

4. Creating a jdbc Statement object
When an connection is established then we can interact with the database. Connection interface defines methods for interacting with the database. It used to instantiate a Statement by using the createStatement() method.

Statement st = con.createStatement();

5. Executing a statement with the Statement object

This interface defines methods which is used to communicate with database. This class has three methods to execute the statements- executeQuery(), executeUpdate(), and execute(). For SELECT statements, executeQuery() method will be used. For create or modify tables, the executeUpdate() method is used. executeQuery() method returns the result of the query in the form of ResultSet object and executeUpdate() method returns the number of rows affected from execution of the query.

ResultSet rs = st.executeQuery("SELECT * from Employee");

6. Getting ResultSet object

Executing the executeQuery() method returns the result in the form of ResultSet object. We can now operate on this object to extract the rows values returned from the execution of the query. Its next() method sets the pointer to the first row and getX() methods can be used to get different types of data from the result set.

while( rs.next() ) {
   String data = rs.getString(1);
   System.out.println( data );
}


Download Source Code:

Advertisements

Ads
Share on Google+Share on Google+

JDBC Components

Posted on: June 27, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: JDBC Components   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
prabhakr
February 3, 2014
source code for registration

its very useful for students

Ads

 

Ads