SlideShare uma empresa Scribd logo
1 de 48
JDBC API
JDBC API
• this is a part of the JDBC Specification that provides an abstraction
(interface) to program java applications accessing the JDBC service
• we know that the JDBC Service is implemented into the JDBC Driver
• the JDBC API is described into two parts:
1. JDBC CORE API
2. JDBC Extension API
JDBC CORE API: THIS part of the API supports accessing the extended
services such as:
• Connection pool
• Accessing distributed transaction
Understanding JDBC API
• WE want to use JDBC API in our java application to access the
tabular datastore such as DB Server.
Why should I use Database server?
• Because to manage the persistence data, why persist the data because to persist our business data,
to do this we need CRUD sql operations.
• To connect to the database server, we should interact with jdbc drivers, to do CRUD operations we
should follow SQL (STRUCTURED QUERY LANGUAGE) because database server only understand one
language called sql .
• We understand the java application would mainly want to interact with database server for CRUD
(create, read, update and delete) operations.
• We also know that the SQL is used to describe the CRUD operations to database server, that means
the Core (central part) of using the JDBC API is to submit the SQL statement to the JDBC Driver;
requesting it to execute with DATABASE SERVER and carry the result back.
• Note:
• I have developed one java application, I want to store the data into the database server, to store or
to interact with database server I have to use only one language that is SQL statement; but
database server is located somewhere else, so I need one vehicle( bus i.e jdbc driver) to carry my
sql statements, so we are using jdbc driver to carry our data with sql statements, so travel over the
different networks we should follow the network API CALL interface. Next I have submitted my SQL
( data) to JDBC drivers, jdbc driver safely takes my (sql) data to the database server.
• Here jdbc driver acts as a courier boy.
The Basic steps of working with JDBC
API
• We know that the java application wants to work
with JDBC to access the tabular datastore
• We also know that the application wants to
access the tabular datastore managing the
persistence data.
• Thus the basic requirement in this process will be
to perform the CRUD operations
• The following are the basic steps involved to
implement for accessing the datastore
performing CRUD operations:
•
• Here our intention is to execute the statement
i.e. Step 3: but to perform step 3 we should
have step 2, to do the step 2 we should have
make the establish the connection to database
server
Step1: obtaining the connection to
DB:
• STEP1 : is just like a connecting to the mail
box, first we have to use login, password to
connect to our Mail inbox
• STEP1 : is just like a connecting to the mail box, first we
have to use login, password to connect to our Mail inbox
• We know that any database server demands to establish a
session/connectivity before accessing any of its services.
• Thus even working with JDBC , we start with establishing
the DB Session.
• Creating a Database session is costlier job it includes:
• Authentication
• Allocating the resources for the session.
• Note: the DB Session can not be shared between the
clients/threads.
Q: How establish/obtain connection to
DB with respect to java/JDBC?
• We have multiple approaches to do this
• Lets start with the basic (primary) approach i.e
using driver object direction.
APPROACH 1:
• Obtaining the connection to db using Driver
object directly: in this approach we use the
connect() method of the Driver object for
getting the connection to the server.
Q: What is Driver object?
Ans: it is a part of JDBC Driver responsible for establishing a session to the database server.
Q: What is java.sql.Driver?
Ans:
It is an interface part of JBC API designed to represent the Driver object. (to the java application)
The following method of Driver object is used to establish the session to the database server:
Connection connect(String jdbcUrl, Properties props) throws SQLException
{
-----;
}
This method is declared in java.sql.Driver interface, implemented by the Driver object. Thus connect() is
Non-static method.
That means to invoke the connect() method we need the following three (3) pre-requisit:
1. Driver Object
2. jdbcUrl
3. Properties
Thus we can implement this step in following two steps:
Step 1: create a Driver object instance
Step 2: invoke the connect() method.
Step 1: create a Driver object instance
• We know that the Driver class (i.e: implementation of java.sql.Driver
interface)is provided by the 3rd party vendor.
• Thus the Driver class Name varies from one JDBC Driver to other:
• Example:
• For sun JDBC-ODBC Bridge Driver
• sun.jdbc.odbc.JdbcOdbcDriver
• For Oracle OCI & thin Driver
• oracle.jdbc.driver.OracleDriver
• (or)
• oracle.jdbc.driver.OracleDri ver
• For MySQL Connector driver
• com.mysql.jdbc.Driver
• Pre-requisite: (I.E we must know the following before learning the
step1)
Q: How many ways we can instantiate a java object (with respect to JRE)?
Ans:
We have only 4 ways to do this:
new keyword
newInstance() method of java.lang.Class object
clone()
Deserialization
And (2) are used for initial state , defined into the class constructor.
•
new keyword:
Use new keyword when you know the class name while developing time .
newInstance():
• Supports instantiating the object whose class name is known dynamically at run
time.
• (3.) clone():
• To create a new object with a copy of state of existing object
• (4.) deserialization:
• To create a object with the state from serialized bytes.
• From this pre-requisite discussion we understand the Driver class instance can be
created using any of the following options:
• Using new keyword
• newInstance() method of java.lang.Class
• using new keyword;
• we may want to write the code as shown below:
• java.sql.Driver d=new Oracle.jdbc.driver.OracleDriver();
• the above statement (for creating Driver class object) is simple but makes our
program tightly bounded to OracleDriver.
• Migrating to some other driver such as MYSQL Driver needs changes in the java
program.
• Note:
• Tightly bounded: In case any changes in a program and main logic may be destroyed is called tightly bounded.
• Loosely coupled : In case without changing the program then it is called loosely coupled.
• Example program:
• //Message.java
• public interface Message
• {
• String getMessage();
• }
• //Oraclemsg.java
• public class Oraclemsg implements Message
• {
• public String getMessage()
• {
• return "I'm from Oraclemsg";
• }
• }
• //MYSQLmsg.java
• public class MYSQLmsg implements Message
• {
• public String getMessage()
• {
• return "I'm from MYSQLmsg";
• }
• }
• //Test.java with using newInstance() method of java.lang.Class
• public class Test
• {
• public static void main(String args[]) throws Exception
• {
• String classname=args[0];
• Class c=Class.forName(classname);
• Object o=c.newInstance();
• Message msg=(Message)o;
• System.out.println(msg.getMessage());
• }
• }
• /*
• output:
• D:webtechnologiesjdbcprognewInstance>javac *.java
•
• D:webtechnologiesjdbcprognewInstance>java Test Oraclemsg
• I'm from Oraclemsg
•
• D:webtechnologiesjdbcprognewInstance>java Test MYSQLmsg
• I'm from MYSQLmsg
•
• D:webtechnologiesjdbcprognewInstance>
• */
•
• /* (or) we can write the main() by using new operator
• //Test.java with using new operator
• public class Test
• {
• public static void main(String args[])
• {
• Message msg;
• msg=new OracleMsg();// i can't call MYSQLmsg class method, and i got confused which class
object want to
• //create the object so here new operator is not recommended, if you use new operator it leads to
write
• //hardcode
• System.out.println(msg.getMessage());
• //if u want to create MYSQLmsg class method again we need to create the object something like
below
• msg=new MYSQLmsg();
• System.out.println(msg.getMessage());
• }
• }
• */
• Q: How work with the Class.newInstance() method to create a object?
• Ans:
• We want to implement the following two statements to do this:
• //Statement 1:
• Class c=Class.forName(className);
• // Statement 2:
• Object o=c.newInstance();
• Q: what happens when at statement 1 (i.e Class.forName())?
• Ans:
•
• The following operations are performed inside the forName() method:
• Find is the .class with the given name is loaded into the JVM
• If Yes: return the Class object representing the given class loaded.
• : Load the class into the JVM:
• If Failed throw the respective exception
• If successful return the Class object representing Class loaded.
Q: What is the role of the Class object
in the JVM?
Fig: newInstanceJVM.JPG
Fig: newInstanceJVM.JPG
Fig: newInstanceJVM.JPG
Fig: newInstanceJVM.JPG
Fig: newInstanceJVM.JPG
Fig: newInstanceJVM.JPG
Fig: newInstanceJVM.JPG
Fig: newInstanceJVM.JPG
Q: What is the role of the Class object
in the JVM?
• Fig: newInstanceJVM.JPG
• We know that for every type loaded into the
JVM a java.lang.Class instance will be created.
• This object is responsible to represent the
type definition loaded into the JVM to the java
application. So that the java Application can
dynamically introspect the types.
Q: What happens when we invoke
the newInstance()?
• Ans:
• The newInstance() method of Class object creates
a new instance of the class this object is
representing
• This uses a no-argument class constructor to
instance the object.
• From the above discussion we prefer to
instantiate the Driver object using the
newInstance() method of Class object, as it
provides a convenience to dynamically shift
between the implementations.
Note:
• In case of private constructor:
• //Abc.java
• public class Abc
• {
• private Abc()
• {
• System.out.println("private Constructor()");
• }
• public static Abc getInstance()
• {
• //some logic
•
• return(new Abc());
• }
• }
• //Test.java
• public class Test
• {
• public static void main(String args[])
• {
• //creating the object in case of private constructor
• Abc a=Abc.getInstance();
• }
• }
• /*
• Output:
• D:materialjava(E DRIVE)javaconstructorprivate>javac *.java
•
• D:materialjava(E DRIVE)javaconstructorprivate>java Test
• private Constructor()
• */
• But in case we are creating the object inside the class (Ex: Abc.java then no problem directly we can create by
using new operator: like below:
• //Abc.java
• public class Abc
• {
• private Abc()
• {
• System.out.println("private Constructor()");
• }
• public static void main(String args[])
• {
• Abc a=new Abc();
• }
• }
• /*
•
• D:materialjava(E DRIVE)javaconstructorprivateinside>javac *.java
•
• D:materialjava(E DRIVE)javaconstructorprivateinside>java Abc
• private Constructor()
• */
•
List 1.1: Sample code for Step 1.1: creating a Driver class instance
•
String
driverClassName=”oracle.jdbc.driver.OracleDr
iver=”oracle.jdbc.driver.OracleDriver”;
Class c=Class.forname(driverClassName);
Driver d=(Driver)c.newInstance();
•
Step 1.2: Invoking the connect() method:
•
• We know that the connect() method is non-static thus
we need a Driver object reference which we got in the
step 1.1
• However still we also need to prepare the inputs to the
connect(0 method so thtat we can invoke it.
• The following are the two inputs for connect() method:
1. jdbcUrl(String)
2. dbProperties(java.util.Properties)
jdbcUrl(String)
• as the name described this is a simple string used
by JDBC to locate the
• database server that we it to connect.
• That means we are informing to the Driver object
that where the Database server is located.
• Why should we inform Driver object only?
Because this Driver object only knows how to
connect to the Database server. Url contains the
address (location) of the Database server.
• The JDBC URL describes the location of the
resource (DataBase Server) to JDBC.
jdbcUrl(String)
• From the URL Format we understand locating the
database server includes the information (inputs) that
varies from one DB to other and also one JDBC Driver
to other.
• Example:
•
• In case of Type-1 Driver:
• i.e: JdbcOdbcDriver we use the following URL format:
• jdbc:odbc:<ODBC DSN>
• Fig: jdbcurl2.JPG
In case of Type 1 driver: jdbcurl2.JPG
In case of JDBC Type-2 Driver:
Jdbc:oracle:oci:@<TNS_SERVICE_NAME>
Fig: jdbcurl3.JPG
Note:
• If I want to connect to Oracle DB Server, I have to write
the information about Oracle Driver in Driver object
and I have to inform to the OCN Native Driver which
port should I connect( ex: 1521) because ports only
connects to the Instance (like DB Names) probably
ports may connects more than one instance (ex: port
1521 may connect Inventory and finance instances
(DB).
• Once I have declared port name (TNS Service), I should
inform to which Data Base I have to connect
In case of JDBC Type-4 Driver:
2. dbProperties: second (2nd) argument (parameter) of
connect() method
• Apart from providing the URL for locating the
database server we also require to supply
necessary parameters such as username and
password to the database server for
authentication and establishing the session.
• The parameter require for establishing the
session to DB differ from one database to other.
• The java.util.Properties is an implementation of
Map which is suitable to describe the multiple
data elements each of them with a unique name.
•
Map
Key Value
User System
Password manager
• Thus the 2
nd
argument of connect() method is
Properties

Mais conteúdo relacionado

Mais procurados

Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Pooja Talreja
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types pptkamal kotecha
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivitybackdoor
 
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...Pallepati Vasavi
 
Java- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionJava- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionMazenetsolution
 
Java.sql package
Java.sql packageJava.sql package
Java.sql packagemyrajendra
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servletsNuha Noor
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)Maher Abdo
 
Interface callable statement
Interface callable statementInterface callable statement
Interface callable statementmyrajendra
 
JDBC Java Database Connectivity
JDBC Java Database ConnectivityJDBC Java Database Connectivity
JDBC Java Database ConnectivityRanjan Kumar
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivityVaishali Modi
 

Mais procurados (20)

JDBC
JDBCJDBC
JDBC
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
 
Jdbc complete
Jdbc completeJdbc complete
Jdbc complete
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
 
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
 
Chap3 3 12
Chap3 3 12Chap3 3 12
Chap3 3 12
 
Java- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionJava- JDBC- Mazenet Solution
Java- JDBC- Mazenet Solution
 
Jdbc
JdbcJdbc
Jdbc
 
Java.sql package
Java.sql packageJava.sql package
Java.sql package
 
Overview Of JDBC
Overview Of JDBCOverview Of JDBC
Overview Of JDBC
 
Core jdbc basics
Core jdbc basicsCore jdbc basics
Core jdbc basics
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
 
Jdbc_ravi_2016
Jdbc_ravi_2016Jdbc_ravi_2016
Jdbc_ravi_2016
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)
 
Interface callable statement
Interface callable statementInterface callable statement
Interface callable statement
 
JDBC Java Database Connectivity
JDBC Java Database ConnectivityJDBC Java Database Connectivity
JDBC Java Database Connectivity
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 

Destaque (10)

Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)
 
Jdbc api
Jdbc apiJdbc api
Jdbc api
 
Cultures of india
Cultures of indiaCultures of india
Cultures of india
 
India
IndiaIndia
India
 
Jdbc Ppt
Jdbc PptJdbc Ppt
Jdbc Ppt
 
Indian culture
Indian cultureIndian culture
Indian culture
 
INCREDIBLE INDIA
INCREDIBLE INDIAINCREDIBLE INDIA
INCREDIBLE INDIA
 
CULTURES OF INDIA
CULTURES OF INDIACULTURES OF INDIA
CULTURES OF INDIA
 
India Presentation
India PresentationIndia Presentation
India Presentation
 
Indian culture
Indian cultureIndian culture
Indian culture
 

Semelhante a 3 jdbc api (20)

Jdbc connectivity
Jdbc connectivityJdbc connectivity
Jdbc connectivity
 
4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt
 
JDBC
JDBCJDBC
JDBC
 
JDBC java for learning java for learn.ppt
JDBC java for learning java for learn.pptJDBC java for learning java for learn.ppt
JDBC java for learning java for learn.ppt
 
Unit 5.pdf
Unit 5.pdfUnit 5.pdf
Unit 5.pdf
 
Jdbc presentation
Jdbc presentationJdbc presentation
Jdbc presentation
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Assignment#10
Assignment#10Assignment#10
Assignment#10
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
PROGRAMMING IN JAVA -unit 5 -part I
PROGRAMMING IN JAVA -unit 5 -part IPROGRAMMING IN JAVA -unit 5 -part I
PROGRAMMING IN JAVA -unit 5 -part I
 
java.pptx
java.pptxjava.pptx
java.pptx
 
jdbc
jdbcjdbc
jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
jdbc
jdbcjdbc
jdbc
 
Jdbc new
Jdbc newJdbc new
Jdbc new
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 

Mais de myrajendra

Mais de myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 
Properties
PropertiesProperties
Properties
 
Interface result set
Interface result setInterface result set
Interface result set
 
Interface database metadata
Interface database metadataInterface database metadata
Interface database metadata
 
Interface connection
Interface connectionInterface connection
Interface connection
 
Indexing
IndexingIndexing
Indexing
 
Get excelsheet
Get excelsheetGet excelsheet
Get excelsheet
 

Último

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Último (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

3 jdbc api

  • 2. JDBC API • this is a part of the JDBC Specification that provides an abstraction (interface) to program java applications accessing the JDBC service • we know that the JDBC Service is implemented into the JDBC Driver • the JDBC API is described into two parts: 1. JDBC CORE API 2. JDBC Extension API JDBC CORE API: THIS part of the API supports accessing the extended services such as: • Connection pool • Accessing distributed transaction Understanding JDBC API • WE want to use JDBC API in our java application to access the tabular datastore such as DB Server.
  • 3.
  • 4. Why should I use Database server? • Because to manage the persistence data, why persist the data because to persist our business data, to do this we need CRUD sql operations. • To connect to the database server, we should interact with jdbc drivers, to do CRUD operations we should follow SQL (STRUCTURED QUERY LANGUAGE) because database server only understand one language called sql . • We understand the java application would mainly want to interact with database server for CRUD (create, read, update and delete) operations. • We also know that the SQL is used to describe the CRUD operations to database server, that means the Core (central part) of using the JDBC API is to submit the SQL statement to the JDBC Driver; requesting it to execute with DATABASE SERVER and carry the result back. • Note: • I have developed one java application, I want to store the data into the database server, to store or to interact with database server I have to use only one language that is SQL statement; but database server is located somewhere else, so I need one vehicle( bus i.e jdbc driver) to carry my sql statements, so we are using jdbc driver to carry our data with sql statements, so travel over the different networks we should follow the network API CALL interface. Next I have submitted my SQL ( data) to JDBC drivers, jdbc driver safely takes my (sql) data to the database server. • Here jdbc driver acts as a courier boy.
  • 5. The Basic steps of working with JDBC API • We know that the java application wants to work with JDBC to access the tabular datastore • We also know that the application wants to access the tabular datastore managing the persistence data. • Thus the basic requirement in this process will be to perform the CRUD operations • The following are the basic steps involved to implement for accessing the datastore performing CRUD operations: •
  • 6.
  • 7. • Here our intention is to execute the statement i.e. Step 3: but to perform step 3 we should have step 2, to do the step 2 we should have make the establish the connection to database server
  • 8. Step1: obtaining the connection to DB: • STEP1 : is just like a connecting to the mail box, first we have to use login, password to connect to our Mail inbox
  • 9.
  • 10.
  • 11. • STEP1 : is just like a connecting to the mail box, first we have to use login, password to connect to our Mail inbox • We know that any database server demands to establish a session/connectivity before accessing any of its services. • Thus even working with JDBC , we start with establishing the DB Session. • Creating a Database session is costlier job it includes: • Authentication • Allocating the resources for the session. • Note: the DB Session can not be shared between the clients/threads.
  • 12. Q: How establish/obtain connection to DB with respect to java/JDBC?
  • 13. • We have multiple approaches to do this • Lets start with the basic (primary) approach i.e using driver object direction.
  • 14. APPROACH 1: • Obtaining the connection to db using Driver object directly: in this approach we use the connect() method of the Driver object for getting the connection to the server.
  • 15. Q: What is Driver object? Ans: it is a part of JDBC Driver responsible for establishing a session to the database server. Q: What is java.sql.Driver? Ans: It is an interface part of JBC API designed to represent the Driver object. (to the java application) The following method of Driver object is used to establish the session to the database server: Connection connect(String jdbcUrl, Properties props) throws SQLException { -----; } This method is declared in java.sql.Driver interface, implemented by the Driver object. Thus connect() is Non-static method. That means to invoke the connect() method we need the following three (3) pre-requisit: 1. Driver Object 2. jdbcUrl 3. Properties Thus we can implement this step in following two steps: Step 1: create a Driver object instance Step 2: invoke the connect() method.
  • 16. Step 1: create a Driver object instance • We know that the Driver class (i.e: implementation of java.sql.Driver interface)is provided by the 3rd party vendor. • Thus the Driver class Name varies from one JDBC Driver to other: • Example: • For sun JDBC-ODBC Bridge Driver • sun.jdbc.odbc.JdbcOdbcDriver • For Oracle OCI & thin Driver • oracle.jdbc.driver.OracleDriver • (or) • oracle.jdbc.driver.OracleDri ver • For MySQL Connector driver • com.mysql.jdbc.Driver • Pre-requisite: (I.E we must know the following before learning the step1)
  • 17. Q: How many ways we can instantiate a java object (with respect to JRE)? Ans: We have only 4 ways to do this: new keyword newInstance() method of java.lang.Class object clone() Deserialization And (2) are used for initial state , defined into the class constructor. • new keyword: Use new keyword when you know the class name while developing time . newInstance():
  • 18. • Supports instantiating the object whose class name is known dynamically at run time. • (3.) clone(): • To create a new object with a copy of state of existing object • (4.) deserialization: • To create a object with the state from serialized bytes. • From this pre-requisite discussion we understand the Driver class instance can be created using any of the following options: • Using new keyword • newInstance() method of java.lang.Class • using new keyword; • we may want to write the code as shown below: • java.sql.Driver d=new Oracle.jdbc.driver.OracleDriver(); • the above statement (for creating Driver class object) is simple but makes our program tightly bounded to OracleDriver. • Migrating to some other driver such as MYSQL Driver needs changes in the java program.
  • 19. • Note: • Tightly bounded: In case any changes in a program and main logic may be destroyed is called tightly bounded. • Loosely coupled : In case without changing the program then it is called loosely coupled. • Example program: • //Message.java • public interface Message • { • String getMessage(); • } • //Oraclemsg.java • public class Oraclemsg implements Message • { • public String getMessage() • { • return "I'm from Oraclemsg"; • } • } • //MYSQLmsg.java • public class MYSQLmsg implements Message • { • public String getMessage() • { • return "I'm from MYSQLmsg"; • } • }
  • 20. • //Test.java with using newInstance() method of java.lang.Class • public class Test • { • public static void main(String args[]) throws Exception • { • String classname=args[0]; • Class c=Class.forName(classname); • Object o=c.newInstance(); • Message msg=(Message)o; • System.out.println(msg.getMessage()); • } • } • /*
  • 21. • output: • D:webtechnologiesjdbcprognewInstance>javac *.java • • D:webtechnologiesjdbcprognewInstance>java Test Oraclemsg • I'm from Oraclemsg • • D:webtechnologiesjdbcprognewInstance>java Test MYSQLmsg • I'm from MYSQLmsg • • D:webtechnologiesjdbcprognewInstance> • */ • • /* (or) we can write the main() by using new operator
  • 22. • //Test.java with using new operator • public class Test • { • public static void main(String args[]) • { • Message msg; • msg=new OracleMsg();// i can't call MYSQLmsg class method, and i got confused which class object want to • //create the object so here new operator is not recommended, if you use new operator it leads to write • //hardcode • System.out.println(msg.getMessage()); • //if u want to create MYSQLmsg class method again we need to create the object something like below • msg=new MYSQLmsg(); • System.out.println(msg.getMessage()); • } • } • */
  • 23. • Q: How work with the Class.newInstance() method to create a object? • Ans: • We want to implement the following two statements to do this: • //Statement 1: • Class c=Class.forName(className); • // Statement 2: • Object o=c.newInstance(); • Q: what happens when at statement 1 (i.e Class.forName())? • Ans: • • The following operations are performed inside the forName() method: • Find is the .class with the given name is loaded into the JVM • If Yes: return the Class object representing the given class loaded. • : Load the class into the JVM: • If Failed throw the respective exception • If successful return the Class object representing Class loaded.
  • 24. Q: What is the role of the Class object in the JVM?
  • 33. Q: What is the role of the Class object in the JVM? • Fig: newInstanceJVM.JPG • We know that for every type loaded into the JVM a java.lang.Class instance will be created. • This object is responsible to represent the type definition loaded into the JVM to the java application. So that the java Application can dynamically introspect the types.
  • 34. Q: What happens when we invoke the newInstance()? • Ans: • The newInstance() method of Class object creates a new instance of the class this object is representing • This uses a no-argument class constructor to instance the object. • From the above discussion we prefer to instantiate the Driver object using the newInstance() method of Class object, as it provides a convenience to dynamically shift between the implementations.
  • 35. Note: • In case of private constructor: • //Abc.java • public class Abc • { • private Abc() • { • System.out.println("private Constructor()"); • } • public static Abc getInstance() • { • //some logic • • return(new Abc()); • } • } • //Test.java • public class Test • { • public static void main(String args[]) • { • //creating the object in case of private constructor • Abc a=Abc.getInstance(); • } • } • /* • Output: • D:materialjava(E DRIVE)javaconstructorprivate>javac *.java • • D:materialjava(E DRIVE)javaconstructorprivate>java Test • private Constructor() • */
  • 36. • But in case we are creating the object inside the class (Ex: Abc.java then no problem directly we can create by using new operator: like below: • //Abc.java • public class Abc • { • private Abc() • { • System.out.println("private Constructor()"); • } • public static void main(String args[]) • { • Abc a=new Abc(); • } • } • /* • • D:materialjava(E DRIVE)javaconstructorprivateinside>javac *.java • • D:materialjava(E DRIVE)javaconstructorprivateinside>java Abc • private Constructor() • */ •
  • 37. List 1.1: Sample code for Step 1.1: creating a Driver class instance • String driverClassName=”oracle.jdbc.driver.OracleDr iver=”oracle.jdbc.driver.OracleDriver”; Class c=Class.forname(driverClassName); Driver d=(Driver)c.newInstance(); •
  • 38. Step 1.2: Invoking the connect() method: • • We know that the connect() method is non-static thus we need a Driver object reference which we got in the step 1.1 • However still we also need to prepare the inputs to the connect(0 method so thtat we can invoke it. • The following are the two inputs for connect() method: 1. jdbcUrl(String) 2. dbProperties(java.util.Properties)
  • 39. jdbcUrl(String) • as the name described this is a simple string used by JDBC to locate the • database server that we it to connect. • That means we are informing to the Driver object that where the Database server is located. • Why should we inform Driver object only? Because this Driver object only knows how to connect to the Database server. Url contains the address (location) of the Database server. • The JDBC URL describes the location of the resource (DataBase Server) to JDBC.
  • 41. • From the URL Format we understand locating the database server includes the information (inputs) that varies from one DB to other and also one JDBC Driver to other. • Example: • • In case of Type-1 Driver: • i.e: JdbcOdbcDriver we use the following URL format: • jdbc:odbc:<ODBC DSN> • Fig: jdbcurl2.JPG
  • 42. In case of Type 1 driver: jdbcurl2.JPG
  • 43. In case of JDBC Type-2 Driver: Jdbc:oracle:oci:@<TNS_SERVICE_NAME> Fig: jdbcurl3.JPG
  • 44. Note: • If I want to connect to Oracle DB Server, I have to write the information about Oracle Driver in Driver object and I have to inform to the OCN Native Driver which port should I connect( ex: 1521) because ports only connects to the Instance (like DB Names) probably ports may connects more than one instance (ex: port 1521 may connect Inventory and finance instances (DB). • Once I have declared port name (TNS Service), I should inform to which Data Base I have to connect
  • 45. In case of JDBC Type-4 Driver:
  • 46. 2. dbProperties: second (2nd) argument (parameter) of connect() method • Apart from providing the URL for locating the database server we also require to supply necessary parameters such as username and password to the database server for authentication and establishing the session. • The parameter require for establishing the session to DB differ from one database to other. • The java.util.Properties is an implementation of Map which is suitable to describe the multiple data elements each of them with a unique name. •
  • 48. • Thus the 2 nd argument of connect() method is Properties

Notas do Editor

  1. Fig: newInstanceJVM.JPG