Sunteți pe pagina 1din 3

JDBC(JAVA DATABASE CONNECTIVITY):-

LOAD AND REGISTER DRIVER :-


➔ In general Driver is an interface present in java.sql package provided by Sun
Microsystems and whose implementation classes are provided by the Database
Vendors.
➔ To load and Register the Driver first we have to make available Driver implementation to
JDBC application.For this we have to set classpath environment variable to the location
Where we have Driver implementation class or we have to keep corresponding jar file in
current working directory
➔ To load and register Driver in our Jdbc applications we have to use the following method
from class “Class” .
Public static Class forName(String class_Name)
EX: Class.forName(“oracle.jdbc.OracleDriver”);
➔ When JVM encounter the above instruction JVM will pickup the parameter that is
OracleDriver Class name and JVM will search for its .class file in the current working
location,if it is not available then JVM will search for it in class path.
➔ If JVM identify OracleDriver.class file in Java pre-defined library or from the jar present
in current working directory or from the class path then JVM will load OracleDriver class
byte code to the memory.
➔ At the time of loading OracleDriver class byte code to the memory JVM will execute a
static block, As part of this , JVM will execute a method call like
DriverManager.registerDriver(--); by the execution of registerDriver() method only
JDBCOdbcDrvier will be available to our Jdbc applications.
➔ Sample code for loading and registering driver with drivermanager service.
Import oracle.jdbc.OracleDriver;
class A {
----------
-----------
static {
-----------
-----------
OracleDriver type4Driver = new OracleDriver();
Try{
DriverManager driverManager = DriverManager.register(type4Driver);
}}catch(Exception e){
System.out.println(e);
}
}
ESTABLISH THE CONNECTION BETWEEN JAVA APPLICATION AND DATABASE :-
➔ To establish the connection between Java application and Database we have to use the
following Method from DriverManager class. Public static Connection
getConnection(String url,String db_user_name,String db_password)
➔ Connection
con=DriverManager.getConnection(“"jdbc:oracle:thin:@localhost:1521:xe"”,”system”,”
admin”);
➔ When JVM encounter the above instruction JVM will access getConnection method,as
part of the getConnection method JVM will establish virtual connection between Java
application and database as per the url which we provided.
➔ As we know interface reference can hold the implementation class object similarly
Connection interface reference holds the object of a java class that implements
java.sql.Connection interface .
➔ In the above connection statement “con” is reference variable of java.sql.Connection
interface pointing to object of a class that implements java.sql.Connection interface .So
“con” becomes object of that class .
➔ DriverManager.getConnection(url,username,password) method call returns object of a
java class that implements java.sql.Connection interface.
➔ Please find below pseudo code for connection test.
Import java.sql.*;
Class ConnTest {
Public statis void main (String args…) {
// load and register the driver
Class.forName("oracle.jdbc.OracleDriver");
// get connection object
Connection connection = DriverManager.getConnection(url, userName, password);
If(connection == null) {
System.out.println(“Connection not Created”);
}
Else{
System.out.println(“Connection Created”)
}
}
}
STATEMENT PREPARED STATEMENT COLLABLE STATEMENT :-
➔ As part of the Jdbc applications after establish the connection between Java application
and Database we have to prepare SQL Queries,we have to transfer SQL Queries to the
databseEngine and we have to make Database Engine to execute SQL Queries.
➔ To write and execute SQL Queries we have to use Statement prepared
Statement and callableStatement from predefined library .
➔ To use the above required predefined library we have to prepare either Statement or
preparedStatement or CallableStatement objects.

** NOTE **
According to DBMS sql queries are :-
i)DDL (create , alter …)
ii)DML (insert , update , delete)
iii)DRL(select)
iv)TCL(commit , rollback , ….)
➔ According to jdbc persistence logic there are two types of sql statements .
o Select sql query (DRL)
o Non-Select sql query (DDL,DML,TCL …..)
➔ Select sql query execution returns single record or bunch of record that are selected
from database table .
➔ Non-select sql query returns a numeric value representing no of records effected
because of query esecution.
➔ JDBC api related predefined library has given following methods for execution of select
and non select sql queries .
o executeUpdate(--) :-
-> Our jdbc application will use executeUpdate() to send and execute non-
select sql query database software.
-> when JVM encounter updation group SQL Query with execteUpdate()
method the JVM will pickup That Sql Query and send to Database through
Database connection.At Database side Database engine Will execute
it,perform updation from Database,identify rowCount value (number of
records got updated) and return to Java application.
->As per the predefined implementation of executeUpdate() method JVM
will return row count value From executeUpdate() method.
-> Public int executeUpdate(String sql_Query) throws SQLException
EX: int rowCount=st.executeUpdate(“update emp1 set esal=esal+500 where
esal<1000”);

o executeQuery(---) :-
o Our jdbc application will use executeQuery() to send and execute select sql
query in database software.
o when JVM encounter executeQuery() method with selection group SQL
query then JVM will pickup Selection group SQL Query,send to
JdbcOdbcDriver, connection will carry that SQL Query to the database
engine through Odbc Driver.
o At database database engine will execute the selection group SQL Query by
performing Query Tokenization,Query parsing,Query optimization and
Query Execution.
o This method will return jdbc resultSet object having set of records selected
from data base software.
o Public ResultSet executeQuery(String sql_Query) throws SQLException
o EX: ResultSet rs=st.executeQuery(“select * from emp1”);

S-ar putea să vă placă și