Jdbc Connction Steps with Examples


Example to connect to oracle database:

Connecting to java application to oracle database.you have to follow 5 steps :
1.load the driver class,
2.create the connection object,
3.create the statement object
4.execute query Connect
5.Connection close

oracle database connectivity steps:

Driver class:The oracle database driver class is oracle.jdbc.driver.OracleDriver.

Connection URl:The oracle database connection URL is jdbc:oracle:thin:@localhost:1521:xe.
here jdbc is API,oracle is the database, thin is the driver,local host is server on which oracle is running,or we can use IP address,1521 is port number and
and XE is the Oracle service name.

User name:The oracle database default userName is system.

Password:Password is given by the user at the time of installing the oracle database. In this example, we are going to use system123 as the password.

import java.sql.*;
class ConnecttoOracle{
public static void main(String args[]){
try{
// load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");

//create  the connection object
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");

// create the statement object
Statement stmt=con.createStatement();

// execute query
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));

// close the connection object
con.close();

}
catch(Exception e){
System.out.println(e);}

}
}


Example to connect to Mysql databaase:



Connecting to java application to mysql database.you have to follow 5 steps :
1.load the driver class,
2.create the connection object,
3.create the statement object
4.execute queryConnect
5.Connection close

Mysql database connectivity steps:

Driver class:The mysql database driver class is com.mysql.jdbc.Driver.

Connection URl:The mysql database connection URL is jdbc:mysql://localhost:3306/test.
here jdbc is API,mysql is database,local host is server on which mysql is running,or we can use IP address,3306 is port number and test is database name.
we can use any database name here.

Username:The mysql database default userName is root.

Password:Password is given by the user at the time of installing the mysql database. In this example, we are going to use root123 as the password.

import java.sql.*;
class ConnecttoMysql{
public static void main(String args[]){
try{
class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/database name","root","root123");

//here root is username and root123 is password

Statement stmt=con.createStatement();

ResultSet rs=stmt.executeQuery("select * from emp");

while(rs.next())
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));

con.close();

}
catch(Exception e){
System.out.println(e);}

}
}
 

JDBC

JDBC:

  • The JDBC ( Java Database Connectivity) API defines interfaces and classes for writing database applications in Java by making database connections. 
  • Using JDBC you can connect to the database and execute any type of statements related relational database. 
  • JDBC is a Java API for executing SQL statements and supports basic SQL functionality. 

JDBC Architecture:

http://instanceofjavaforus.blogspot.in/

  • Java application calls the JDBC API. JDBC loads a driver which connects to the database.

Connectivity Steps to JDBC:

5 Steps to connect java Application with the database using JDBC.They are:
  • Register the driver class
  • Creating Connection
  • Creating Statement
  • Execting Queries
  • Closing Connection

Register the driver class:

  • The forName() of class is used to register the driver class.Using this class is used load the class dynamically.
try {

Class.forName(com.mysql.jdbc.Driver”); //Or any other driver

}

catch(Exception x){

System.out.println( “Unable to load the driver class!” );

}

Creating the Connection:


  • DriverManager is the backbone of JDBC architecture. DriverManager class manages the JDBC drivers that are installed on the system. 
  • Its getConnection() method of Driver Manager class is used to establish a connection to a database. It uses a username, password, and a jdbc url to establish a connection to 
  • the database and returns a connection object.

try{
 Connection dbConnection=DriverManager.getConnection(url,”loginName”,”Password”)
}
catch( SQLException x ){
System.out.println( “Couldn’t get connection!” );
}

Creating Statement:


  • Once a connection is established we can interact with the database. Connection interface defines methods for interacting with the database via the established connection. Statement 
  • A statement object is used to send and execute SQL statements to a database.
       statement = dbConnection.createStatement();

Three kinds of Statements:

  • Statement
  • Prepared Statement
  • Callable Statement

Statement:

  • Execute simple sql queries without parameters.
         Statement createStatement().

Prepared Statement: 

  • Execute precompiled sql queries with or without parameters.
        PreparedStatement prepareStatement(String sql).

Callable Statement: 

  • Execute a call to a database stored procedure.
       CallableStatement prepareCall(String sql)

Executing Queries:


  • Statement interface defines methods that are used to interact with database and execution of SQL statements.
  • The Statement class has three methods:
  • executeQuery()
  • executeUpdate()
  • execute(). 

executeQuery():


  • For a SELECT statement, the method to use is executeQuery . 

executeUpdate():

  • For statements that create or modify tables, the method to use is executeUpdate. 
  • statements and are executed with the method executeUpdate. 

execute():

  • execute() executes an SQL statement that is written as String object.

Connection Close():

  • This method is used to close the connection.
          con.close();

Syntax:

public void close() throws SQLException.


Select Menu