JDBC Driver
• JDBC Driver is a software component that enables
java application to interact with the database.
• There are 4 types of JDBC drivers:
1. JDBC-ODBC bridge driver (Type-1)
2. Native-API driver (partially java driver) (Type-2)
3. Network Protocol driver (fully java driver) (Type-
3)
4. Thin driver (fully java driver) (Type-4)
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);}
}
}

Java data baseJava data baseJava data baseJava data base.pptx

  • 1.
    JDBC Driver • JDBCDriver is a software component that enables java application to interact with the database. • There are 4 types of JDBC drivers: 1. JDBC-ODBC bridge driver (Type-1) 2. Native-API driver (partially java driver) (Type-2) 3. Network Protocol driver (fully java driver) (Type- 3) 4. Thin driver (fully java driver) (Type-4)
  • 2.
    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
  • 3.
    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");
  • 4.
    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");
  • 5.
    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();
  • 6.
    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)); }
  • 7.
    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();
  • 8.
    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.
  • 9.
    • Let's firstcreate a table in oracle database. • create table emp(id number(10),name varchar 2(40),age number(3));
  • 10.
    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");
  • 11.
    //step3 create thestatement 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);} } }