Java Database Connectivity
(JDBC)
Components of JDBC
JDBC Architecture
Why use JDBC?
• Before JDBC, ODBC API was used to connect
and execute query to the database. But ODBC
API uses ODBC driver that is written in C
language which is platform dependent and
unsecured.
• That is why Sun Microsystem has defined its
own API (JDBC API) that uses JDBC driver
written in Java language.
What is API?
• API (Application programming interface) is a
document that contains description of all the
features of a product or software.
• It represents classes and interfaces that
software programs can follow to communicate
with each other. An API can be created for
applications, libraries, operating systems, etc
Types of JDBC Driver
• JDBC Driver is a software component that enables
java application to interact with the database. There
are 4 types of drivers
5 Steps to connect to the database in java
• There are 5 steps to connect any java application
with the database in java using JDBC. They are as
follows:
1. Register the driver class
2. Creating connection
3. Creating statement
4. Executing queries
5. Closing connection
1) Register the driver class
• The forName() method of Class class is used to
register the driver class. This method is used to
dynamically load the driver class.
Syntax of forName() method
public static void forName(String className)
throws ClassNotFoundException
Example to register the OracleDriver class
Class.forName("oracle.jdbc.driver.OracleDriver");
2) Create the connection object
The getConnection() method of DriverManager class is used
to establish connection with the database.
Syntax of getConnection() method
1) public static Connection getConnection(String url)throws SQLExceptio
n
2) public static Connection getConnection(String url,String name,String p
assword) throws SQLException
Example to establish connection with the Oracle database
Connection con=DriverManager.getConnection( "jdbc:oracle:
thin:@localhost:1521:xe","system","password");
3) Create the Statement object
The createStatement() method of Connection
interface is used to create statement. The object
of statement is responsible to execute queries with
the database.
Syntax of createStatement() method
• public Statement createStatement()throws SQLExce
ption
• Example to create the statement object
• Statement stmt=con.createStatement();
4) Execute the query
The executeQuery() method of Statement interface is used to
execute queries to the database. This method returns the
object of ResultSet that can be used to get all the records of a
table.
Syntax of executeQuery() method
public ResultSet executeQuery(String sql)throws SQLException
Example to execute query
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
5) Close the connection object
• By closing connection object statement and
ResultSet will be closed automatically.
• The close() method of Connection interface is
used to close the connection.
Syntax of close() method
public void close()throws SQLException
• Example to close connection
con.close();
Example to connect to the Oracle database
• For connecting java application with the oracle database, you need to
follow 5 steps to perform database connectivity.
• In this example we are using Oracle10g as the database. So we need to
know following information for the oracle database:
• Driver class: The driver class for the oracle database is
oracle.jdbc.driver.OracleDriver.
• Connection URL: The connection URL for the oracle10G database is
jdbc:oracle:thin:@localhost:1521:xe
where jdbc is the API, oracle is the database, thin is the driver, localhost is
the server name on which oracle is running, we may also use IP address,
1521 is the port number and XE is the Oracle service name. You may get
all these informations from the tnsnames.ora file.
• Username: The default username for the oracle database is system.
• Password: Password is given by the user at the time of installing the oracle
database.
• Let's first create a table in oracle database.
• create table emp(id number(10),name varchar
2(40),age number(3));
Example to Connect Java Application with Oracle database
import java.sql.*;
class OracleCon{
public static void main(String args[]){
try{
//step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
//step2 create the connection object
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
//step3 create the statement object
Statement stmt=con.createStatement();
//step4 execute query
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
//step5 close the connection object
con.close();
}catch(Exception e){ System.out.println(e);}
}
}

jdbc concepts in java jdbc programming- programming

  • 1.
  • 4.
  • 5.
  • 7.
    Why use JDBC? •Before JDBC, ODBC API was used to connect and execute query to the database. But ODBC API uses ODBC driver that is written in C language which is platform dependent and unsecured. • That is why Sun Microsystem has defined its own API (JDBC API) that uses JDBC driver written in Java language.
  • 8.
    What is API? •API (Application programming interface) is a document that contains description of all the features of a product or software. • It represents classes and interfaces that software programs can follow to communicate with each other. An API can be created for applications, libraries, operating systems, etc
  • 9.
    Types of JDBCDriver • JDBC Driver is a software component that enables java application to interact with the database. There are 4 types of drivers
  • 10.
    5 Steps toconnect to the database in java • There are 5 steps to connect any java application with the database in java using JDBC. They are as follows: 1. Register the driver class 2. Creating connection 3. Creating statement 4. Executing queries 5. Closing connection
  • 11.
    1) Register thedriver class • The forName() method of Class class is used to register the driver class. This method is used to dynamically load the driver class. Syntax of forName() method public static void forName(String className) throws ClassNotFoundException Example to register the OracleDriver class Class.forName("oracle.jdbc.driver.OracleDriver");
  • 12.
    2) Create theconnection object The getConnection() method of DriverManager class is used to establish connection with the database. Syntax of getConnection() method 1) public static Connection getConnection(String url)throws SQLExceptio n 2) public static Connection getConnection(String url,String name,String p assword) throws SQLException Example to establish connection with the Oracle database Connection con=DriverManager.getConnection( "jdbc:oracle: thin:@localhost:1521:xe","system","password");
  • 13.
    3) Create theStatement object The createStatement() method of Connection interface is used to create statement. The object of statement is responsible to execute queries with the database. Syntax of createStatement() method • public Statement createStatement()throws SQLExce ption • Example to create the statement object • Statement stmt=con.createStatement();
  • 14.
    4) Execute thequery The executeQuery() method of Statement interface is used to execute queries to the database. This method returns the object of ResultSet that can be used to get all the records of a table. Syntax of executeQuery() method public ResultSet executeQuery(String sql)throws SQLException Example to execute query ResultSet rs=stmt.executeQuery("select * from emp"); while(rs.next()) { System.out.println(rs.getInt(1)+" "+rs.getString(2)); }
  • 15.
    5) Close theconnection object • By closing connection object statement and ResultSet will be closed automatically. • The close() method of Connection interface is used to close the connection. Syntax of close() method public void close()throws SQLException • Example to close connection con.close();
  • 16.
    Example to connectto the Oracle database • For connecting java application with the oracle database, you need to follow 5 steps to perform database connectivity. • In this example we are using Oracle10g as the database. So we need to know following information for the oracle database: • Driver class: The driver class for the oracle database is oracle.jdbc.driver.OracleDriver. • Connection URL: The connection URL for the oracle10G database is jdbc:oracle:thin:@localhost:1521:xe where jdbc is the API, oracle is the database, thin is the driver, localhost is the server name on which oracle is running, we may also use IP address, 1521 is the port number and XE is the Oracle service name. You may get all these informations from the tnsnames.ora file. • Username: The default username for the oracle database is system. • Password: Password is given by the user at the time of installing the oracle database.
  • 17.
    • Let's firstcreate a table in oracle database. • create table emp(id number(10),name varchar 2(40),age number(3));
  • 18.
    Example to ConnectJava Application with Oracle database import java.sql.*; class OracleCon{ public static void main(String args[]){ try{ //step1 load the driver class Class.forName("oracle.jdbc.driver.OracleDriver"); //step2 create the connection object Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","system","oracle"); //step3 create the statement object Statement stmt=con.createStatement(); //step4 execute query ResultSet rs=stmt.executeQuery("select * from emp"); while(rs.next()) System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)); //step5 close the connection object con.close(); }catch(Exception e){ System.out.println(e);} } }