JDBC
JDBC
1|VIKAS-JDBC
Limitation of File System:-
1. It is database dependent because this is provided by local
operating system.
2. We can’t store huge amount of data.
3. There is no security mechanism is available for store data.
4. There is no mechanism available to prevent duplicate data.
5. There is no support of query language.
Limitation of database:-
JDBC:-
2|VIKAS-JDBC
JDBC Features:-
1. JDBC is a standard API, with the help of JDBC API we can
communicate with any database without rewrite our application.
Therefore, java JDBC API is database independent.
2. JDBC API developed in java and JDBC API applicable for all
platform. So JDBC concept is platform independent API.
3. By using JDBC API we can perform all types of structure query
operation.
JDBC version:-
Evaluation of JDBC:-
• If we want communicate with database by using C/C++ application,
then database specific libraries must be available in our
application directory.
C/C++ Application
(ORACLE LIBRARY) ORACLE
3|VIKAS-JDBC
So, problem with this approach is, if we migrate one database to
another database then we have to write entire application once
again by using new database specific library.
C/C++ Application
(MYSQL LIBRARY) MYSQL
ORACLE
ODBC Driver ORACLE
MYSQL
C/C++/JAVA ODBC Driver MYSQL
Application
SYBASE
ODBC Driver SYBASE
4|VIKAS-JDBC
• Overcome these problems sun-micro-system introduce JDBC concept
in 19th feb 1997.
• JDBC concept application for any platform and it is platform
independent technology.
• JDBC driver are implemented in java, if we used JDBC driver with
java application then internal conversion is not required and
performance of the application will not go down.
ORACLE
JDBC Driver ORACLE
MYSQL
JAVA JDBC Driver
Application MYSQL
SYBASE
JDBC Driver SYBASE
Note:- ODBC Concept is applicable for any Database and for any
Language, but only for Windows Platform.
ODBC JDBC
1).ODBC full name Open Database 1).JDBC there is no official full
Connectivity. name but we called Java Database
Connectivity.
2).ODBC is database independent 2).JDBC is database independent
and platform dependent applicable and platform independent
only for window machine . applicable for all machine.
3).We can use ODBC for any 3).We can use JDBC only for java
language like C, C++, Java etc language.
5|VIKAS-JDBC
JDBC Architecture:-
JAVA APPLICATION
JDBC API
DriverManager
DriverManager:-
• DriverManager is a class present in java.sql package.
• DriverManager is very important component of JDBC architecture.
• DriverManager is responsible to manage all types database driver
available in our System.
• DriverManager is responsible to register and unregister database
driver.
DriverManager.registerDriver(driver);
DriverManager.unregisterDriver(driver);
6|VIKAS-JDBC
• DriverManager is responsible to establish connection between
java application and database with the help of database specific
driver.
Connection con=DriverManager.getConnection(String url, String
uname, String upwd);
Database Driver:-
• Driver is a software which available in from of jar file, which
is part of database.
• It is collection of implementation classes of various interface
present in JDBC API.
• Database driver is very important component of JDBC
Architecture, without driver software we can’t touch database.
• Database driver work as translator/bridge between java
application and database, driver convert java specific call to
database specific call and database specific call to java
specific call.
JDBC API:-
• JDBC API is collection of classes and interface, by using this
classes and interface in our java application we can communicate
with database.
• Database vendor use JDBC API to developed Driver Software.
• JDBC API contains two packages.
1. java.sql package.
2. javax.sql package.
1. DriverManager
2. Date
3. Time
4. TimeStamp
5. Types
6. SQLException
7|VIKAS-JDBC
Interface present in java.sql package:-
1. Driver
2. Connection
3. Statement
4. PreparedStatement
5. CallableStatement
6. ResultSet
7. ResultSetMetaData
8. DataBaseMetaData
8|VIKAS-JDBC
• Suppose java application communicate with ORACLE database with
the help of oracle driver. So, oracle driver software is
collection of implementing classes of JDBC API interface.
• Every software identifies by some special class name, similarly
driver software is also identified by some special class name.
• Driver is a implementation class of Driver interface present in
java.sql package.
Example:-
Type-1 driver or JDBC-ODBC bridge driver:-This driver class name is
sun.jdbc.odbc.JdbcOdbcDriver
sun.jdbc.odbc:-package name.
JdbcOdbcDriver:-Driver class name.
Driver interface:-
Driver interface act as requirement specification present in java.sql
package and driver software are responsible to provide implementation
of this interface.
Driver class:-
Driver class in a implementation class which provide the implements
of Driver interface present in JDBC API of java.sql package, Driver
class is present in Driver software.
Driver Software:-
Driver Software is very important component of JDBC architecture,
without Driver Software we can’t communicate with Database.
9|VIKAS-JDBC
Driver software is present in form jar file like:-ojdbc14.jar,
ojbc6.jar, mysql-connector.jar and it is the part of database.
1. Type-1 Driver
2. Type-2 Driver
3. Type-3 Driver
4. Type-4 Driver
MACHINE-1 MACHINE-2
10 | V I K A S - J D B C
• This driver is not directly communicating with database, this
driver communicates with database with the help of Microsoft
ODBC driver, so it is database independent.
MACHINE-1 MACHINE-2
• Type-2 driver is also known as Native-Driver or Native-API-
Partly-java driver.
• Type-2 driver is same as Type-1 driver just only one change in
place ODBC driver we used DATABASE SPECIFIC NATIVE LIBRARY.
• Database Native Library is set of function written in non-
java(like C/C++).
• Type-2 driver internally take the support of Database Specific
native library to communicate with database.
• Type-2 driver convert java call to database specific native
library call which is directly understandable to Database.
11 | V I K A S - J D B C
Type-3 Driver(Middleware Driver or Network Protocol Driver or All Java
Net Protocol Driver.):-
JAVA NETWORK
APPLICATIO MIDDLEWARE
PROTOCOL DRIVER SERVER DATABASE
N
JAVA
THIN DRIVER
APPLICATION DATABASE
MACHINE-1 MACHINE-2
12 | V I K A S - J D B C
•Type-4 driver is directly communicating with database without
help of any other component like ODBC or Database Specific native
library or Middleware server, therefore this driver is known as
thin driver.
• Type-4 driver is also known as Pure-Java-Driver because this
driver is developed 100% in java.
• Type-4 driver is also known as All-Java-Native-Protocol-Driver
because this driver uses database specific native protocol to
communicate with database.
Advantage of Type-4 driver:-
• Type-4 driver is light-weight.
• It is platform independent.
• Type-4 driver is more secure because it is using database vendor
specific native protocol.
13 | V I K A S - J D B C
• If a Driver software is completely developed in java technology
such types of Driver software are known as pure java driver.
• If a Driver software is developed in java and other technology
like C,C++ then such type of Driver are known as Partial java
Driver.
Type-2 driver:-If above two driver is not available then we used type-
2 driver.
14 | V I K A S - J D B C
• JDBC API contains set of interface and database vendor is
responsible to provide implementation of these interface in the
form of Driver software. So, Driver software should be available
in our application for this we have to place driver software
corresponding jar file in class path.
• Oracle Type-4 driver jar file name is ojdbc6.jar and we place
this jar file in class path and this jar file is present in
following directory.
D:\app\vikas123\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar
• Once we place Driver jar file in class path now, we can load and
register driver by using forName() method of Class class.
Example:-
Class.forName(“oracle.jdbc.OracleDriver”);
When we load Driver class then loaded Driver class static block
will be executed automatically.
15 | V I K A S - J D B C
DriverManager.registerDriver(driver);
• From JDBC 4.0 version(JAVA 1.6v) onward Driver call will load
automatically from the class path and we have not need to load
Driver class explicitly.
Example:-
Connection con=
DriverManager.getConnection(jdbcurl,username,upwd);
1. Driver name----------------------------thin
2. Machine on which database is running---@localhost
16 | V I K A S - J D B C
3. Port number of database----------------1521
4. System ID------------------------------XE
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:or
cl","SYSTEM","vikas123");
System.out.println(con.getClass().getName());
In this program we try to get con variable class name then we result:-
oracle.jdbc.driver.T4CConnection/OracleConnection
Result:- oracle.jdbc.driver.T4CConnection/OracleConnection
This is hardcoded and it applicable only for oracle type-4 driver we
can’t change the driver URL name, if we want try to change the driver
name then we write entire program once again. Overcome this problem
we used Connection interface in place of implementation class.
17 | V I K A S - J D B C
1. Select Query Operation.
2. Non-Select Query Operation.
Once we create/write Sql query now we can execute sql query by using
following method of Statement interface.
1. executeQuery()
2. executeUpdate()
3. execute()
Example:-
int count=st.executeUpdate(“update emp set esal=esal+50000 where
esal<100000”);
18 | V I K A S - J D B C
public boolean execute()
This method returns boolean value if sql query is select then return
true value and if Sql query non-select then return value is false.
1. getXXX(String column_name)
2. getXXX(int column_index)
19 | V I K A S - J D B C
st.close():-After close statement then not allowed sending further
query to database.
package com.vikas.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TestJDBC
{
public static void main(String[] args)throws SQLException,
ClassNotFoundException
{
//Step-1 load and register driver.
Class.forName("oracle.jdbc.OracleDriver");
//Step-2 Establish the connection between Java Application and
Database.
Connection
connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:
1521:orcl","SYSTEM","vikas123");
//Step-3 Prepare Statement object.
Statement statement=connection.createStatement();
//Step-4 Write SQL query
String sqlquery="create table emp(eid number(5),ename
varchar2(10),esal float(10),eaddr varchar2(10))";
//Step-5 Execute SQL query
statement.executeUpdate(sqlquery);
System.out.println("Table Created Successfully");
//close connection
statement.close();
connection.close();
}
}
20 | V I K A S - J D B C
Install Database Software based on our requirement like oracle
mysql...etc.
For above JDBC program execution we install oracle11g database.
21 | V I K A S - J D B C
jdbc:oracle:oci:@orcl(Oracle Version 9 )
Main-protocol:-jdbc
Sub-protocol:-oracle
Driver-Connection-Details:- oci:@orcl
oci is database specific native library name.
orcl is System ID
orcl:-orcl System ID is unique ID for every database, system id
is varies from database to database or database version to
version.
Example:2
TestJDBC.java
package com.vikas.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TestJDBC {
public static void main(String[] args)throws SQLException,
ClassNotFoundException
{
//Step-1 load and register driver.
Class.forName("oracle.jdbc.OracleDriver");
//Step-2 Establish the connection between Java Application and
Database.
Connection
connection=DriverManager.getConnection("jdbc:oracle:oci:@orcl","SYST
EM","vikas123");
//Step-3 Prepare Statement object.
Statement statement=connection.createStatement();
//Step-4 Write SQL query
String sqlquery="create table emp(eid number(5),ename
varchar2(10),esal float(10),eaddr varchar2(10))";
//Step-5 Execute SQL query
22 | V I K A S - J D B C
statement.executeUpdate(sqlquery);
System.out.println("Table Created Successfully");
//close connection
statement.close();
connection.close();
}
}
Create a table in Oracle Database by using type-3 Driver:-
Driver jar file:-Same driver jar file we will used that jar file we
had uses in type-2 driver.
Jar file is jdbc14.jar, jdbc6.jar, jdbc7.jar.
JDBC URL:-“jdbc:oracle:thin:@localhost:1521:orcl”.
Example:-3
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TestJDBC {
public static void main(String[] args)throws SQLException,
ClassNotFoundException
{
//Step-1 load and register driver.
Class.forName("oracle.jdbc.OracleDriver");
//Step-2 Establish the connection between Java Application and
Database.
23 | V I K A S - J D B C
Connection
connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:
1521:orcl","SYSTEM","vikas123");
//Step-3 Prepare Statement object.
Statement statement=connection.createStatement();
//Step-4 Write SQL query
String sqlquery="create table emp(eid number(5),ename
varchar2(10),esal float(10),eaddr varchar2(10))";
//Step-5 Execute SQL query
statement.executeUpdate(sqlquery);
System.out.println("Table Created Successfully");
//close connection
statement.close();
connection.close();
}
}
24 | V I K A S - J D B C
1) Right click on src of Created Java Project.
2) Select "New" button.
3) Select "Class".
4) Provide package Name.
5) Provide Class Name.
6) Select main method if required.
7) Run JDBC by clicking on Run icon.
25 | V I K A S - J D B C
{
Class.forName("oracle.jdbc.OracleDriver");
Connection
connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:
1521:orcl","SYSTEM","vikas123");
Statement statement=connection.createStatement();
ResultSet resultSet=statement.executeQuery("select * from emp");
System.out.println("EID\tENAME\tESAL\tEADDR");
while(resultSet.next())
{
System.out.print(resultSet.getInt(1)+"\t"+resultSet.getString(2)+"\t
"+resultSet.getFloat(3)+"\t"+resultSet.getString(4));
System.out.println();
}
statement.close();
connection.close();
}
}
Then we can’t expect what the exact result, it varies from driver to
driver, if driver is type-4, we get empty ResultSet object and perform
update operation in respective database table, when we try to retrieve
result from empty ResultSet object then we will get exception.
package com.vikas.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestJDBC
{
public static void main(String[] args)throws SQLException,
ClassNotFoundException
26 | V I K A S - J D B C
{
Class.forName("oracle.jdbc.OracleDriver");
Connection
connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:
1521:orcl","SYSTEM","vikas123");
Statement statement=connection.createStatement();
ResultSet resultSet=statement.executeQuery("insert into emp
values(5678,'fghr',567000,'gulf')");
System.out.println("EID\tENAME\tESAL\tEADDR");
while(resultSet.next())
{
System.out.print(resultSet.getInt(1)+"\t"+resultSet.getString(2)+"\t
"+resultSet.getFloat(3)+"\t"+resultSet.getString(4));
System.out.println();
}
statement.close();
connection.close();
}
}
Then we can’t expect what the exact result, it varies from driver to
driver if driver is type-4, then we will get result number of rows
selected.
package com.vikas.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TestJDBC {
public static void main(String[] args)throws SQLException,
ClassNotFoundException
{
Class.forName("oracle.jdbc.OracleDriver");
27 | V I K A S - J D B C
Connection
connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:
1521:orcl","SYSTEM","vikas123");
Statement statement=connection.createStatement();
int rowcount=statement.executeUpdate("select * from emp");
System.out.println(rowcount);
statement.close();
connection.close();
}
}
28 | V I K A S - J D B C
package com.vikas.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TestJDBC {
public static void main(String[] args)throws SQLException,
ClassNotFoundException
{
Class.forName("oracle.jdbc.OracleDriver");
Connection
connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:
1521:orcl","SYSTEM","vikas123");
Statement statement=connection.createStatement();
int rowcount=statement.executeUpdate("drop table emp1");
if(rowcount==0)
{
System.out.println("Table deleted successfully");
}
statement.close();
connection.close();
}
}
29 | V I K A S - J D B C
while(true)
{
System.out.println("Enter Employee ID");
int eid=sc.nextInt();
System.out.println("Enter Employee Name");
String ename=sc.next();
System.out.println("Enter Employee Salary");
float esal=sc.nextFloat();
System.out.println("Enter Employee Address");
String eaddr=sc.next();
statement.executeUpdate("insert into emp
values("+eid+",'"+ename+"','"+esal+"','"+eaddr+"')");
System.out.println("Record Inserted Successfully");
System.out.println("Do you want insert more record[YES/NO] ");
String option=sc.next();
if(option.equalsIgnoreCase("no"))
{
break;
}
}
sc.close();
statement.close();
connection.close();
}
}
package com.vikas.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class TestJDBC {
public static void main(String[] args)throws SQLException,
ClassNotFoundException
{
Class.forName("oracle.jdbc.OracleDriver");
30 | V I K A S - J D B C
Connection
connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:
1521:orcl","SYSTEM","vikas123");
Statement statement=connection.createStatement();
int updatecount=statement.executeUpdate("update emp set
esal=esal+20000 where esal>60000");
System.out.println("Number of Row Updated::"+updatecount);
statement.close();
connection.close();
}
}
31 | V I K A S - J D B C
}
sc.close();
statement.close();
connection.close();
}
}
32 | V I K A S - J D B C
{
Class.forName("oracle.jdbc.OracleDriver");
Connection
connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:
1521:orcl","SYSTEM","vikas123");
Statement statement=connection.createStatement();
Scanner sc=new Scanner(System.in);
System.out.println("Enter Salary Range");
float salrange=sc.nextFloat();
String sqlquery="delete from emp where esal>'"+salrange+"'";
int updatecount=statement.executeUpdate(sqlquery);
System.out.println("Number of Row Updated::"+updatecount);
sc.close();
statement.close();
connection.close();
}
}
Case:-1
ResultSet rs=statement.executeQuery(“select * from emp”);
33 | V I K A S - J D B C
Case:-2
ResultSet rs=statement.executeQuery(“select ename, esal, eaddr, eid
from emp”);
while(rs.next())
{
System.out.print(rs.getString(1)+” ”+rs.getFloat(2)+”
”+rs.getString(3)+” ”+rs.getInt(4));
}
Case:-3
ResultSet rs=statement.executeQuery(“select ename, eid from emp”);
• Column order of database table is EID,ENAME,ESAL,EADDR.
• Number of columns of database table is 4.
• Column order of ResultSet is ENAME, EID.
• Number of columns of ResultSet is 2.
• So, we retrieve record from database table based column order of
ResultSet as following
while(rs.next())
{
System.out.print(rs.getString(1)+” ”+rs.getInt(2));
}
package com.vikas.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestJDBC {
34 | V I K A S - J D B C
public static void main(String[] args)throws SQLException,
ClassNotFoundException
{
Class.forName("oracle.jdbc.OracleDriver");
Connection
connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:
1521:orcl","SYSTEM","vikas123");
Statement statement=connection.createStatement();
String sqlquery="select ename,esal from emp";
System.out.println("ENAME\tESAL");
ResultSet rs=statement.executeQuery(sqlquery);
while(rs.next())
{
System.out.print(rs.getString(1)+"\t"+rs.getFloat(2));
System.out.println();
}
statement.close();
connection.close();
}
}
package com.vikas.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class TestJDBC {
public static void main(String[] args)throws SQLException,
ClassNotFoundException
{
Class.forName("oracle.jdbc.OracleDriver");
Connection
connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:
1521:orcl","SYSTEM","vikas123");
Statement statement=connection.createStatement();
Scanner sc=new Scanner(System.in);
System.out.println("Enter Initial character of Employee");
35 | V I K A S - J D B C
String intial_char=sc.next()+"%";
String sqlquery="select * from emp where ename like
'"+intial_char+"'";
boolean flag=false;
System.out.println("EID\tENAME\tESAL\tEADDR");
ResultSet rs=statement.executeQuery(sqlquery);
while(rs.next())
{
flag=true;
System.out.print(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getFloat(
3)+"\t"+rs.getString(4));
System.out.println();
}
if(flag==false)
{
System.out.println("Record is not available");
}
sc.close();
statement.close();
connection.close();
}
}
package com.vikas.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestJDBC {
public static void main(String[] args)throws SQLException,
ClassNotFoundException
{
Class.forName("oracle.jdbc.OracleDriver");
Connection
connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:
1521:orcl","SYSTEM","vikas123");
Statement statement=connection.createStatement();
String sqlquery="select * from emp order by esal ASC";
36 | V I K A S - J D B C
//for descending 'DESC' we can use.
System.out.println("EID\tENAME\tESAL\tEADDR");
ResultSet rs=statement.executeQuery(sqlquery);
while(rs.next())
{
System.out.print(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getFloat(
3)+"\t"+rs.getString(4));
System.out.println();
}
statement.close();
connection.close();
}
}
Aggregate Function:-In oracle database multiple aggregate function
are available by using this aggregate function we get summary of
result like.
count(*):-This aggregate function return number of records available
in oracle database table.
37 | V I K A S - J D B C
//----------ueses count(*) aggregate function-------------
String sqlquery1="select count(*) from emp";
ResultSet resultSet1=statement.executeQuery(sqlquery1);
if(resultSet1.next())
{
System.out.print("Total Number Of Record Aailable=
"+resultSet1.getInt(1));
System.out.println();
}
//-------------ueses max() aggregate function---------------
String sqlquery2="select * from emp where esal in(select max(esal)
from emp)";
ResultSet resultSet2=statement.executeQuery(sqlquery2);
if(resultSet2.next())
{
System.out.println("Highest Salary of Employee Information");
System.out.println("EID\tENAME\tESAL\tEADDR");
System.out.print(resultSet2.getInt(1)+"\t"+resultSet2.getString(2)+"
\t"+resultSet2.getFloat(3)+"\t"+resultSet2.getString(4));
System.out.println();
}
//-------------ueses min() aggregate function---------------
String sqlquery3="select * from emp where esal in(select min(esal)
from emp)";
ResultSet resultSet3=statement.executeQuery(sqlquery3);
if(resultSet3.next())
{
System.out.println("Lowest Salary of Employee Information");
System.out.println("EID\tENAME\tESAL\tEADDR");
System.out.print(resultSet3.getInt(1)+"\t"+resultSet3.getString(2)+"
\t"+resultSet3.getFloat(3)+"\t"+resultSet3.getString(4));
}
statement.close();
connection.close();
}
}
38 | V I K A S - J D B C