Core Java Tutorials


Java Hello World

Java Compilation

JDK New Features

JVM Architecture

Java OOPs Concepts

Java Package


Exception Handling


Immutable Class

File Handling




Concurrent Package


Collection Internal



Reflection In Java


Class Loader

Java Inner classes

Garbage Collector


References In Java

Heap Dump & Thread Dump

/java/db-connectivityPrevious             Next

JDBC Connectivity

To start using JDBC (Java database connectivity) first you would need a database system, and then a JDBC (Java database connectivity) driver for your database,During this article JDBC (Java database connectivity) will be explained with MySQL and Oracle. firstly i would go for oracle than i will go for MySQl

Step to connect with database

there are some following steps for connecting with Oracle database

Import JDBC Packages

The Import statements tell the Java compiler where to find the classes you reference in your code and are placed at the very beginning of your source code. To use the standard JDBC package, which allows you to select, insert, update, and delete data in SQL tables, add the following imports to your source code,basically two package are there which we need to import

import java.sql.*;

Register JDBC Driver

You must register the driver in your program before you use it. Registering the driver is the process by which the Oracle driver's or MySQL driver's class file is loaded into the memory, so it can be utilized as an implementation of the JDBC interfaces. You need to do this registration only once in your program. We can register a driver by "forName()"" method

How to register a Driver with "Class.forName()"

forName() method is a method of Class class. It is used to dynamically load the driver class. It is used to register the driver class. The whole string of the driver is passed as an argument in the method. The basic idea behind using Class.forName() is to load a JDBC driver implementation When you let Java load the driver class with the forName call, the driver will register itself so that it can be used.
The following example uses Class.forName( ) to register the Oracle driver.

try {
catch(ClassNotFoundException ex) {

Create Connection with database

Now we need to create a connection with database. we can create connection with database using getConnection() method which is a static method present in DriverManager class, The most commonly used form of getConnection() requires you to pass a database URL, a username, and a password, Look at the following example

try {
   Connection con=DriverManager.getConnection(
catch(SQLException ex) {

Create Statement PreparedStatement object

we can pass our sql query to the database in two ways

From Statement Object
From PreparedStatement Object

both object are use for send sql query to the database.Basiclly Statement object is use for DDL Operation and PreparedStatement object is use for DML Operation. see the following example

try {
   Connection con=DriverManager.getConnection(
   Statement stmt=con.createStatement();
   PreparedStatement prst=con.prepareStatement();
catch(SQLException ex) {

Executing query

we can execute our sql query using 3 method

execute() :-The execute method executes an SQL statement and indicates the form of the first result,To execute a SQL statement with the execute method, call it by passing it a valid SQL statement as a String object, or as a string literal
executeUpdae() :-Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.
executeQuery() :-the method normally used with a SELECT statement, executeQuery( ), returns a ResultSet object. The executeQuery( ) method effectively combines the execute( ) and getResultSet( ) methods into one call:

Closing Connection

It is always better to close the database/resource objects after usage.

/java/db-connectivityPrevious             Next