Steps To Design JDBC Applications
Steps To Design JDBC Applications
1
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
2
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
implementation classes are provided by the Database Vendors as part of their Database
Softwares.
-->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
-->If we want to use Type1 Driver provided by Sun MicroSystems in our JDBC applications then it
is not required to set classpath environment variable because Type1 Driver was provided by Sun
-->If we want to use Type1 Driver in our JDBC applications then before loading we have to
3
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
-->To configure Microsoft product odbc Driver we have to use the following path.
To setting DSN:-
Start
Control Panel
Administrative Tools
user DSN
Click on OK
-->To load and register Driver in our Jdbc applications we have to use the following method from class
‘Class’
Eg:Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
-->When JVM encounter the above instruction JVM will pickup the parameter that is JDBCOdbcDriver
Class name and JVM will search for its .class file in the current location,if it is not available then JVM will
-->If JVM identify JDBCODBCDriver.class file in Java pre-defined library(rt.jar) then JVM will load
-->At the time of loading JdbcOdbcDriver class byte code to the memory JVM will execute a static block,
As part of this JVM wiil execute a method call likeDriverManager.registerDriver(--); by the execution
4
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
-->In case of Type1 Driver if we use either Jdbc 4.0 version or Jdk 6.0 version then it is optional
To perform loading and register the driver step because JVM will perform Drvier registration
automatically at the time of establishing the connection between Java application and Database.
NOTE:To prepare Jdbc applications Java API has provided the required pre-defined library in the form
of java.sql package so that we have to import this package in our Java file.
Import java.sql.*;
-->java.sql package includes the following pre-defined library to design Jdbc applications.
1. Driver (I)
2. DriverManager (C)
3. Connection (I)
4. Statement (I)
5. PreparedStatement (I)
6. ResultSet (I)
7. ResultSetMetaData (I)
8. DatabaseMetaData (I)
9. Savepoint(i)
5
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
-->To establish the connection between Java application and Database we have to use the following
Method from DriverManager class.
Ex:Connection con=DriverManager.getConnection(“jdbc:odbc:dsnName”,”system”,”durga”);
-->When JVM encounter the above instruction JVM will access getConnection method,as part
of the getConnection method JVM will access connect() method to establish virtual socket connection
Between Java application and database as per the url which we provided.
-->If we use Type1 Driver then we have to use the following Driver class name and URL
d-class : sun.jdbc.odbc.JdbcOdbcDriver
url :jdbc:odbc:dsnName
-->In general all the Jdbc Drivers should have an url with the following format.
main-protocol: sub-protocol
-->where main-protocol name should be Jdbc for each and every Driver but the sub protocol name
should be varied from Driver to Driver.
6
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
Q) In Jdbc applications getConnecction() method will establish the connection between Java application
and Database and return connection object but connection is an interface how it is possible to
Ans:In general in Java technology we are unable to create objects for the interfaces directly,if we want
to accommodate interface data in the form of objects then we have to take either an implementation
-->If we take implementation class as an alternative then it may allow its own data a part from the data
Declared in interface and implementation class object should have its own identity instead of interface
identity.
-->If we want to create an object with only interface identity and to allow only interface data we have to
-->In jdbc applications getConnection() method will return connection object by returning anonymous
NOTE: To create connection object taking an implementation class or anonymous inner class is
7
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
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
-->To write and execute SQL Queries we have to use same predefined library from Statement prepared
-->To use the above required predefined library we have to prepare either Statement or
-->In jdbc applications when we have a requirement to execute the same SQL Query in the next
Sequence where to improve the performance of JDBC application we will use prepared Statement.
8
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
-->In jdbc applications when we have a requirement to access stored procedures and functions available
-->To prepare Statement object we have to use the following method from Connection.
Where createStatement() method will return Statement object by creating Statement interfaces
Anonymous inner class object.
9
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
1.executeQuery()
2.executeUpdate()
3.execute()
Ans:Where executeQuery() method can be used to execute “selection group SQL Queries” in order to
fetch(retrieve) data from Database.
-->when JVM encounter executeQuery() method with selection group SQL query then JVM will pickup
Selection group SQL Query,send to JdbcOdbcDriver,it will send to connection.Now connection will
carrythat SQL Query to the database engine through Odbc Driver.
-->At database database engine will execute the selection group SQL Query by performing Query
-->By the execution of selection group SQL Query database engine will fetch the data from database and
-->As Java technology is pure object oriented,Java application will store the fetched data in the form of
-->As per the predefined implementation of executeQuery method JVM will return the generated
-->where executeUpdate() method can be used to execute updation group SQLQueries in order to
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.
10
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
-->As per the predefined implementation of executeUpdate() method JVM will return row count value
-->In Jdbc applications execute() method can be used to execute both selection group and updation
-->when JVM encounter selection group SQL Query with execute() method then JVM will send selection
Group SQL Query to database engine,where database engine will execute it and send back the fetched
-->In Java application ResultSet object will be created with the fetched data but as per the predefined
-->when JVM encounter updation group SQL Query as parameter to execute() method then JVM
Will send it to the database engine,where database engine will perform updations on database and
return row Count value to Java application.But as per the predefined implementation of execute()
method JVM will return “false” as Boolean value from execute() method
Ex:
boolean b1=st.execute (“select * from emp1”);
11
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
Ex:con.close();
JdbcApp1: The following example demonstrate how to create a table on Database through a JDBC
applicationby taking table name as Dynamic input.
//import section
import java.sql.*;
import java.io.*;
class JdbcApp1{
public static void main(String args[]) throws Exception{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//prepare Statement
Statement st=con.createStatement();
//create BufferedReader
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String tname=br.readLine();
//prepare SQLQuery
st.executeUpdate(sql);
12
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
con.close();
}}
JdbcApp2: The following example demonstrates how to insert no.of records on database table by
taking records data as dynamic input.
import java.io.*;
import java.sql.*;
public class JdbcApp2 {
public static void main(String[] args)throws Exception {
Class.forName("oracle.jdbc.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","du
rga");
Statement st=con.createStatement();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
System.out.print("Employee Number :");
int eno=Integer.parseInt(br.readLine());
System.out.print("Employee Name :");
String ename=br.readLine();
System.out.print("Employee Salary :");
float esal=Float.parseFloat(br.readLine());
System.out.print("Employee Address :");
String eaddr=br.readLine();
st.executeUpdate("insert into emp1
values("+eno+",'"+ename+"',"+esal+",'"+eaddr+"')");
System.out.println("Employee Inserted Successfully");
System.out.print("Onemore Employee[Yes/No]? :");
String option=br.readLine();
if(option.equals("No"))
{
break;
}
}
con.close();
}}
13
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
In Jdbc applications if we want to used Type1 driver provided by sun micro systems then we have to
usethe following Driver class and URL
driver.class:sun.jdbc.odbc.JdbcOdbcDriver
url:jdbc:odbc:dsnName
-->Similarly if we want to use Type4 Driver provided by oracle we have to use the following Driver
class and URL
driver_class:oracle.jdbc.driver.OracleDriver
url:jdbc:oracle:thin:@localhost:1521:xe
-->Oracle Driver is a class provided by oracle software in the form of Ojdbc14.jar file
C:\oracleexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc.jar
-->If we want to use Type4 Driver provided by oracle in our Jdbc applications we have to set
classpath environment variable to the location where we have ojdbc14.jar
D:\jdbc4>set
classpath=%classpath%;C:\oracleexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar
JdbcApp3: The following example Demonstrate how to perform updations on Database table
through Jdbc Application
import java.io.*;
import java.sql.*;
public class JdbcApp3 {
public static void main(String[] args)throws Exception {
//Class.forName("oracle.jdbc.OracleDriver");
Connection con=DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:xe","system","durga");
Statement st=con.createStatement();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Bonus Amount :");
int bonus_Amt=Integer.parseInt(br.readLine());
System.out.print("Salary Range :");
float sal_Range=Float.parseFloat(br.readLine());
int rowCount=st.executeUpdate
("update emp1 set esal=esal+"+bonus_Amt+" where esal<"+sal_Range);
System.out.println("Employees Updated :"+rowCount);
con.close();
}}
14
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
JdbcApp4: The following example demonstrates how to delete no.of records from database
table through a Jdbc application
import java.io.*;
import java.sql.*;
public class JdbcApp4 {
public static void main(String[] args)throws Exception {
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
Connection con=DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:xe","system","durga");
Statement st=con.createStatement();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Salary Range :");
float sal_Range=Float.parseFloat(br.readLine());
int rowCount=st.executeUpdate("delete from emp1 where esal<"+sal_Range);
System.out.println("Records Deleted :"+rowCount);
con.close();
}
}
15
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
-->In jdbc application we will use executeUpdate() method to execute the Updation group SQL
queries like create,insert,update,delete,drop,alter and so on.
-->If we execute the SQL Queries like insert,update and delete then really some no.of record will
be updated on database table then that number will be return as rowCount value from
executeUpdate().
-->If we execute the SQL Queries like create,alter,drop with executeUpdate() method then records
manipulation is not available on database,in this context the return value from executeUpdate()
method is completely depending on the type of Driver which we used in JDBC application.
-->In the above context if we use type1 Driver provided by SunMicroSystems the executeUpdate()
method will return "-1" as rowCount value.
-->For the above requirement if we use Type4 Driver provided by oracle then executeUpdate()
method will return "0" as rowCount value
JdbcApp5:
import java.sql.*;
public class JdbcApp5 {
public static void main(String[] args)throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:nag","system","durga");
Statement st=con.createStatement();
int rowCount1=st.executeUpdate("create table emp1(eno number)");
System.out.println(rowCount1);
int rowCount2=st.executeUpdate("drop table emp1");
System.out.println(rowCount2);
con.close();
}
}
JdbcApp6:
import java.sql.*;
public class JdbcApp6 {
public static void main(String[] args)throws Exception {
Class.forName("oracle.jdbc.OracleDriver");
Connection con=DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:xe","system","durga");
Statement st=con.createStatement();
int rowCount1=st.executeUpdate("create table emp1(eno number)");
System.out.println(rowCount1);
int rowCount2=st.executeUpdate("drop table emp1");
16
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com
JAVA Means DURGASOFT
System.out.println(rowCount2);
con.close();
}
}
17
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com