SlideShare a Scribd company logo
1 of 45
JDBC –
Java DataBase Connectivity

Biz on Cloud System
What is JDBC?








“JDBC an API(Application Programming Interface) to
write programs to connect to a database, retrieve the
data from the database and utilize the data in a java
program.”
What is ODBC? It is an Open DataBase Connectivity
API Specification given by X open company containing
set of rules and guidelines to develop ODBC Drivers.
JDBC Specification is given in Java language to interact
with Database software's.
ODBC specification is given in C language to interact
with “VB, VB.Net, D2K,
2
General Architecture





What design pattern is
implied in this
architecture?
What does it buy for us?
Why is this architecture
also multi-tiered?

3
4
JDBC Driver Types
 There
1.
2.
3.
4.

are 4 JDBC Driver Types
Type-1 JDBC Driver
Type-2 JDBC Driver
Type-3 JDBC Driver
Type-4 JDBC Driver

5
Type-1: JDBC-ODBC Bridge
Driver

6
Type-1
 JDBC

driver type-1 is designed to interact
with ODBC Drivers. Each ODBC Driver uses
the database specific vendor DB Library to
communicate with DB S/W
 In a Type 1 driver, a JDBC bridge is used to
access ODBC drivers installed on each client
machine. Using ODBC requires configuring
on your system a Data Source Name (DSN)
that represents the target database.
7
TYPE-1
 When

Java first came out, this was a useful
driver because most databases only
supported ODBC access but now this type of
driver is recommended only for experimental
use or when no other alternative is available.
 The type 1 driver is not considered a
deployment-level driver and is typically used
for development and testing purposes only.
8
Advantages of Type-1
 1.

It is a built in JDBC driver of J2SDK s/w.
So there is no need of arranging JDBC Driver
separately.
 2. since ODBC drivers and vendor DB
Libraries are available almost for all the DB
s/w’s , we can use JDBC TYPE 1 driver to
interact with all the DB S/ws

9
Disadvantages of type-1
 Since

multiple converssions are involved,
performance of jdbc is very poor
 Due to performance problem, this driver is
not suitable for large scale applications

10
Type 2: JDBC-Native API:
 In

a Type 2 driver, JDBC API calls are
converted into native C/C++ API calls which
are unique to the database. These drivers
typically provided by the database vendors
and used in the same manner as the JDBCODBC Bridge, the vendor-specific driver must
be installed on each client machine.

11
 If

we change the Database we have to
change the native API as it is specific to a
database and they are mostly obsolete now
but you may realize some speed increase
with a Type 2 driver, because it eliminates
ODBC's overhead.

12
13
Type 3: JDBC-Net pure Java:
 In

a Type 3 driver, a three-tier approach is
used to accessing databases. The JDBC
clients use standard network sockets to
communicate with an middleware application
server. The socket information is then
translated by the middleware application
server into the call format required by the
DBMS, and forwarded to the database
server.
14
Type-3: JDBC-Net pure java
 This

kind of driver is extremely flexible, since
it requires no code installed on the client and
a single driver can actually provide access to
multiple databases.

15
16
Type-3: JDBC-Net pure java
 You

can think of the application server as a
JDBC "proxy," meaning that it makes calls for
the client application. As a result, you need
some knowledge of the application server's
configuration in order to effectively use this
driver type.

17
Basic steps to use
a database in Java
 1.Establish

a connection
 2.Create JDBC Statements
 3.Execute SQL Statements
 4.GET ResultSet
 5.Close connections

18
Type 4: 100% pure Java:
 In

a Type 4 driver, a pure Java-based driver
that communicates directly with vendor's
database through socket connection. This is
the highest performance driver available for
the database and is usually provided by the
vendor itself.
 This kind of driver is extremely flexible, you
don't need to install special software on the
client or server. Further, these drivers can be
downloaded dynamically.

19
20
 You

can think of the application server as a
JDBC "proxy," meaning that it makes calls for
the client application. As a result, you need
some knowledge of the application server's
configuration in order to effectively use this
driver type.

21
Advantages of type 4
 It

is totally developed in java so it exhibits
platform independently
 Since this driver is designed to interact with
db s/w directly. So there is no need of vendor
db library, odbc driver in the client machine.
This driver is suitable for internet application
and for untrusted applet to db connection
 This driver can be made downloading to
client machine dynamically from n/w
22
Type-4: advantages contn..
 This

driver gives better performance compare
to other drivers. So, those driver can be used
in large scale and high-end applications

23
Disadvantages of type-4
 For

every db s/w, we need separate jdbc
driver type-4

24
25
1. Establish a connection
import java.sql.*;
 Load the vendor specific driver




Class.forName("oracle.jdbc.driver.OracleDriver");
 What

do you think this statement does, and how?
 Dynamically loads a driver class, for Oracle database



Make the connection


Connection con =
DriverManager.getConnection( "jdbc:oracle:thin:@oracl
e-prod:1521:OPROD", username, passwd);
 What

do you think this statement does?
 Establishes connection to database by obtaining
a Connection object
26
27
2. Create JDBC statement(s)
 Statement


stmt = con.createStatement() ;

Creates a Statement object for sending SQL statements
to the database

28
Executing SQL Statements




String createLehigh = "Create table Lehigh " +
"(SSN Integer not null, Name VARCHAR(32), " +
"Marks Integer)";
stmt.executeUpdate(createLehigh);
//What does this statement do?
String insertLehigh = "Insert into Lehigh values“
+ "(123456789,abc,100)";
stmt.executeUpdate(insertLehigh);
29
Get ResultSet
String queryLehigh = "select * from Lehigh";
ResultSet rs = Stmt.executeQuery(queryLehigh);
//What does this statement do?
while (rs.next()) {
int ssn = rs.getInt("SSN");
String name = rs.getString("NAME");
int marks = rs.getInt("MARKS");
}

30
Close connection
 stmt.close();
 con.close();

31
Transactions and JDBC









JDBC allows SQL statements to be grouped together into a
single transaction
Transaction control is performed by the Connection object,
default mode is auto-commit, I.e., each sql statement is treated
as a transaction
We can turn off the auto-commit mode with
con.setAutoCommit(false);
And turn it back on with con.setAutoCommit(true);
Once auto-commit is off, no SQL statement will be committed
until an explicit is invoked con.commit();
At this point all changes done by the SQL statements will be
made permanent in the database.

32
Handling Errors with
Exceptions







Programs should recover and leave the database in
a consistent state.
If a statement in the try block throws an exception or
warning, it can be caught in one of the
corresponding catch statements
How might a finally {…} block be helpful here?
E.g., you could rollback your transaction in a
catch { …} block or close database connection and
free database related resources in finally {…} block
33
Another way to access database
(JDBC-ODBC)

What’s a bit different
about this
architecture?
Why add yet
another layer?

34
Sample program
import java.sql.*;
class Test {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //dynamic loading of driver
String filename = "c:/db1.mdb"; //Location of an Access database
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database+= filename.trim() + ";DriverID=22;READONLY=true}"; //add on to end
Connection con = DriverManager.getConnection( database ,"","");
Statement s = con.createStatement();
s.execute("create table TEST12345 ( firstcolumn integer )");
s.execute("insert into TEST12345 values(1)");
s.execute("select firstcolumn from TEST12345");

35
Sample program(cont)
ResultSet rs = s.getResultSet();
if (rs != null) // if rs == null, then there is no ResultSet to view
while ( rs.next() ) // this will step through our data row-by-row
{ /* the next line will get the first column in our current row's ResultSet
as a String ( getString( columnNumber) ) and output it to the screen */
System.out.println("Data from column_name: " + rs.getString(1) );
}
s.close(); // close Statement to let the database know we're done with it
con.close(); //close connection
}
catch (Exception err) { System.out.println("ERROR: " + err); }
}
}

36
Mapping types JDBC - Java

37
JDBC 2 – Scrollable Result Set
…
Statement stmt =
con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
String query = “select students from class where type=‘not sleeping’ “;
ResultSet rs = stmt.executeQuery( query );
rs.previous(); / / go back in the RS (not possible in JDBC 1…)
rs.relative(-5); / / go 5 records back
rs.relative(7); / / go 7 records forward
rs.absolute(100); / / go to 100th record
…
38
JDBC 2 – Updateable ResultSet
…
Statement stmt =
con.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE );
String query = " select students, grade from class
where type=‘really listening this presentation’ “;
ResultSet rs = stmt.executeQuery ( query );
…
while ( rs.next() )
{
int grade = rs.getInt(“grade”);
rs.updateInt(“grade”, grade+10);
rs.updateRow();
}
39
Metadata from DB
A

Connection's database is able
to provide schema information
describing its tables,
its supported SQL grammar,
its stored procedures
the capabilities of this connection, and so on



What is a stored procedure?
Group of SQL statements that form a logical unit
and perform a particular task

This information is made available through
a DatabaseMetaData object.
40
Metadata from DB - example
…
Connection con = …. ;
DatabaseMetaData dbmd = con.getMetaData();
String catalog = null;
String schema = null;
String table = “sys%”;
String[ ] types = null;
ResultSet rs =
dbmd.getTables(catalog , schema , table , types );
…
41
JDBC – Metadata from RS
public static void printRS(ResultSet rs) throws SQLException
{
ResultSetMetaData md = rs.getMetaData();
// get number of columns
int nCols = md.getColumnCount();
// print column names
for(int i=1; i < nCols; ++i)
System.out.print( md.getColumnName( i) +",");
/ / output resultset
while ( rs.next() )
{
for(int i=1; i < nCols; ++i)
System.out.print( rs.getString( i)+",");
System.out.println( rs.getString(nCols) );
}
}

42
JDBC and beyond


(JNDI) Java Naming and Directory Interface





(JDO) Java Data Object





API for network-wide sharing of information about users,
machines, networks, services, and applications
Preserves Java’s object model
Models persistence of objects, using RDBMS as repository
Save, load objects from RDBMS

(SQLJ) Embedded SQL in Java





Standardized and optimized by Sybase, Oracle and IBM
Java extended with directives: # sql
SQL routines can invoke Java methods
Maps SQL types to Java classes
43
JDBC references


JDBC Data Access API – JDBC Technology Homepage




JDBC Database Access – The Java Tutorial




http://java.sun.com/j2se/1.4.2/docs/api/java/sql/package-summary.html

JDBC Technology Guide: Getting Started




http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/index.html

java.sql package




http://java.sun.com/docs/books/tutorial/jdbc/index.html

JDBC Documentation




http://java.sun.com/products/jdbc/index.html

http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/getstart/GettingStartedTOC.fm.html

JDBC API Tutorial and Reference (book)


http://java.sun.com/docs/books/jdbc/

45
JDBC


JDBC Data Access API – JDBC Technology Homepage




JDBC Database Access – The Java Tutorial




http://java.sun.com/j2se/1.4.2/docs/api/java/sql/package-summary.html

JDBC Technology Guide: Getting Started




http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/index.html

java.sql package




http://java.sun.com/docs/books/tutorial/jdbc/index.html

JDBC Documentation




http://java.sun.com/products/jdbc/index.html

http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/getstart/GettingStartedTOC.fm.html

JDBC API Tutorial and Reference (book)


http://java.sun.com/docs/books/jdbc/

46

More Related Content

What's hot

What's hot (20)

Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Overview Of JDBC
Overview Of JDBCOverview Of JDBC
Overview Of JDBC
 
jdbc document
jdbc documentjdbc document
jdbc document
 
JDBC Architecture and Drivers
JDBC Architecture and DriversJDBC Architecture and Drivers
JDBC Architecture and Drivers
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc connectivity in java
Jdbc connectivity in javaJdbc connectivity in java
Jdbc connectivity in java
 
Database and Java Database Connectivity
Database and Java Database ConnectivityDatabase and Java Database Connectivity
Database and Java Database Connectivity
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)
 
Jdbc complete
Jdbc completeJdbc complete
Jdbc complete
 
Basic Java Database Connectivity(JDBC)
Basic Java Database Connectivity(JDBC)Basic Java Database Connectivity(JDBC)
Basic Java Database Connectivity(JDBC)
 
Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
 
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...
 
JDBC
JDBCJDBC
JDBC
 
java Jdbc
java Jdbc java Jdbc
java Jdbc
 
Jdbc Ppt
Jdbc PptJdbc Ppt
Jdbc Ppt
 
JDBC Connectivity Model
JDBC Connectivity ModelJDBC Connectivity Model
JDBC Connectivity Model
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
 
JDBC
JDBCJDBC
JDBC
 
JDBC
JDBCJDBC
JDBC
 

Viewers also liked

jstl ( jsp standard tag library )
jstl ( jsp standard tag library )jstl ( jsp standard tag library )
jstl ( jsp standard tag library )Adarsh Patel
 
Fişinqdən necə qorunmaq olar?
Fişinqdən necə qorunmaq olar?Fişinqdən necə qorunmaq olar?
Fişinqdən necə qorunmaq olar?Irkan Akhmedov
 
Təhlükəsiz proqram təminatının java mühitində hazırlanma texnologiyaları
Təhlükəsiz proqram təminatının java mühitində hazırlanma texnologiyalarıTəhlükəsiz proqram təminatının java mühitində hazırlanma texnologiyaları
Təhlükəsiz proqram təminatının java mühitində hazırlanma texnologiyalarıIrkan Akhmedov
 
Java proqramlaşdirma mühitində təhlükəsiz proqram təminatinin hazirlanma texn...
Java proqramlaşdirma mühitində təhlükəsiz proqram təminatinin hazirlanma texn...Java proqramlaşdirma mühitində təhlükəsiz proqram təminatinin hazirlanma texn...
Java proqramlaşdirma mühitində təhlükəsiz proqram təminatinin hazirlanma texn...Irkan Akhmedov
 
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in JavaAbhilash Nair
 
Data types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in javaData types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in javaJaved Rashid
 
JSP Standart Tag Lİbrary - JSTL
JSP Standart Tag Lİbrary - JSTLJSP Standart Tag Lİbrary - JSTL
JSP Standart Tag Lİbrary - JSTLseleciii44
 
JSP Standard Tag Library
JSP Standard Tag LibraryJSP Standard Tag Library
JSP Standard Tag LibraryIlio Catallo
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types pptkamal kotecha
 
MVC ppt presentation
MVC ppt presentationMVC ppt presentation
MVC ppt presentationBhavin Shah
 

Viewers also liked (18)

Basic JSTL
Basic JSTLBasic JSTL
Basic JSTL
 
jstl ( jsp standard tag library )
jstl ( jsp standard tag library )jstl ( jsp standard tag library )
jstl ( jsp standard tag library )
 
Jdbc
JdbcJdbc
Jdbc
 
Fişinqdən necə qorunmaq olar?
Fişinqdən necə qorunmaq olar?Fişinqdən necə qorunmaq olar?
Fişinqdən necə qorunmaq olar?
 
Təhlükəsiz proqram təminatının java mühitində hazırlanma texnologiyaları
Təhlükəsiz proqram təminatının java mühitində hazırlanma texnologiyalarıTəhlükəsiz proqram təminatının java mühitində hazırlanma texnologiyaları
Təhlükəsiz proqram təminatının java mühitində hazırlanma texnologiyaları
 
Java proqramlaşdirma mühitində təhlükəsiz proqram təminatinin hazirlanma texn...
Java proqramlaşdirma mühitində təhlükəsiz proqram təminatinin hazirlanma texn...Java proqramlaşdirma mühitində təhlükəsiz proqram təminatinin hazirlanma texn...
Java proqramlaşdirma mühitində təhlükəsiz proqram təminatinin hazirlanma texn...
 
Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in Java
 
Data types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in javaData types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
JSP Standart Tag Lİbrary - JSTL
JSP Standart Tag Lİbrary - JSTLJSP Standart Tag Lİbrary - JSTL
JSP Standart Tag Lİbrary - JSTL
 
JSP Standard Tag Library
JSP Standard Tag LibraryJSP Standard Tag Library
JSP Standard Tag Library
 
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
MVC ppt presentation
MVC ppt presentationMVC ppt presentation
MVC ppt presentation
 
Jsp presentation
Jsp presentationJsp presentation
Jsp presentation
 
Model View Controller (MVC)
Model View Controller (MVC)Model View Controller (MVC)
Model View Controller (MVC)
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Operators in java
Operators in javaOperators in java
Operators in java
 

Similar to Jdbc (20)

Jdbc sasidhar
Jdbc  sasidharJdbc  sasidhar
Jdbc sasidhar
 
Prashanthi
PrashanthiPrashanthi
Prashanthi
 
JDBC.ppt
JDBC.pptJDBC.ppt
JDBC.ppt
 
Jdbc
JdbcJdbc
Jdbc
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
 
Jdbc introduction
Jdbc introductionJdbc introduction
Jdbc introduction
 
Chap3 3 12
Chap3 3 12Chap3 3 12
Chap3 3 12
 
JDBC
JDBCJDBC
JDBC
 
Final Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.pptFinal Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.ppt
 
java arlow jdbc tutorial(java programming tutorials)
java arlow jdbc tutorial(java programming tutorials)java arlow jdbc tutorial(java programming tutorials)
java arlow jdbc tutorial(java programming tutorials)
 
Jdbc
JdbcJdbc
Jdbc
 
Unit 5-jdbc2
Unit 5-jdbc2Unit 5-jdbc2
Unit 5-jdbc2
 
Core jdbc basics
Core jdbc basicsCore jdbc basics
Core jdbc basics
 
Jdbcdriver
JdbcdriverJdbcdriver
Jdbcdriver
 
Jdbc
JdbcJdbc
Jdbc
 
Core Java Programming Language (JSE) : Chapter XIII - JDBC
Core Java Programming Language (JSE) : Chapter XIII -  JDBCCore Java Programming Language (JSE) : Chapter XIII -  JDBC
Core Java Programming Language (JSE) : Chapter XIII - JDBC
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
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
 
Advance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-databaseAdvance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-database
 
Jdbc
JdbcJdbc
Jdbc
 

More from myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
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
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Recently uploaded

Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 

Recently uploaded (20)

Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 

Jdbc

  • 1. JDBC – Java DataBase Connectivity Biz on Cloud System
  • 2. What is JDBC?     “JDBC an API(Application Programming Interface) to write programs to connect to a database, retrieve the data from the database and utilize the data in a java program.” What is ODBC? It is an Open DataBase Connectivity API Specification given by X open company containing set of rules and guidelines to develop ODBC Drivers. JDBC Specification is given in Java language to interact with Database software's. ODBC specification is given in C language to interact with “VB, VB.Net, D2K, 2
  • 3. General Architecture    What design pattern is implied in this architecture? What does it buy for us? Why is this architecture also multi-tiered? 3
  • 4. 4
  • 5. JDBC Driver Types  There 1. 2. 3. 4. are 4 JDBC Driver Types Type-1 JDBC Driver Type-2 JDBC Driver Type-3 JDBC Driver Type-4 JDBC Driver 5
  • 7. Type-1  JDBC driver type-1 is designed to interact with ODBC Drivers. Each ODBC Driver uses the database specific vendor DB Library to communicate with DB S/W  In a Type 1 driver, a JDBC bridge is used to access ODBC drivers installed on each client machine. Using ODBC requires configuring on your system a Data Source Name (DSN) that represents the target database. 7
  • 8. TYPE-1  When Java first came out, this was a useful driver because most databases only supported ODBC access but now this type of driver is recommended only for experimental use or when no other alternative is available.  The type 1 driver is not considered a deployment-level driver and is typically used for development and testing purposes only. 8
  • 9. Advantages of Type-1  1. It is a built in JDBC driver of J2SDK s/w. So there is no need of arranging JDBC Driver separately.  2. since ODBC drivers and vendor DB Libraries are available almost for all the DB s/w’s , we can use JDBC TYPE 1 driver to interact with all the DB S/ws 9
  • 10. Disadvantages of type-1  Since multiple converssions are involved, performance of jdbc is very poor  Due to performance problem, this driver is not suitable for large scale applications 10
  • 11. Type 2: JDBC-Native API:  In a Type 2 driver, JDBC API calls are converted into native C/C++ API calls which are unique to the database. These drivers typically provided by the database vendors and used in the same manner as the JDBCODBC Bridge, the vendor-specific driver must be installed on each client machine. 11
  • 12.  If we change the Database we have to change the native API as it is specific to a database and they are mostly obsolete now but you may realize some speed increase with a Type 2 driver, because it eliminates ODBC's overhead. 12
  • 13. 13
  • 14. Type 3: JDBC-Net pure Java:  In a Type 3 driver, a three-tier approach is used to accessing databases. The JDBC clients use standard network sockets to communicate with an middleware application server. The socket information is then translated by the middleware application server into the call format required by the DBMS, and forwarded to the database server. 14
  • 15. Type-3: JDBC-Net pure java  This kind of driver is extremely flexible, since it requires no code installed on the client and a single driver can actually provide access to multiple databases. 15
  • 16. 16
  • 17. Type-3: JDBC-Net pure java  You can think of the application server as a JDBC "proxy," meaning that it makes calls for the client application. As a result, you need some knowledge of the application server's configuration in order to effectively use this driver type. 17
  • 18. Basic steps to use a database in Java  1.Establish a connection  2.Create JDBC Statements  3.Execute SQL Statements  4.GET ResultSet  5.Close connections 18
  • 19. Type 4: 100% pure Java:  In a Type 4 driver, a pure Java-based driver that communicates directly with vendor's database through socket connection. This is the highest performance driver available for the database and is usually provided by the vendor itself.  This kind of driver is extremely flexible, you don't need to install special software on the client or server. Further, these drivers can be downloaded dynamically. 19
  • 20. 20
  • 21.  You can think of the application server as a JDBC "proxy," meaning that it makes calls for the client application. As a result, you need some knowledge of the application server's configuration in order to effectively use this driver type. 21
  • 22. Advantages of type 4  It is totally developed in java so it exhibits platform independently  Since this driver is designed to interact with db s/w directly. So there is no need of vendor db library, odbc driver in the client machine. This driver is suitable for internet application and for untrusted applet to db connection  This driver can be made downloading to client machine dynamically from n/w 22
  • 23. Type-4: advantages contn..  This driver gives better performance compare to other drivers. So, those driver can be used in large scale and high-end applications 23
  • 24. Disadvantages of type-4  For every db s/w, we need separate jdbc driver type-4 24
  • 25. 25
  • 26. 1. Establish a connection import java.sql.*;  Load the vendor specific driver   Class.forName("oracle.jdbc.driver.OracleDriver");  What do you think this statement does, and how?  Dynamically loads a driver class, for Oracle database  Make the connection  Connection con = DriverManager.getConnection( "jdbc:oracle:thin:@oracl e-prod:1521:OPROD", username, passwd);  What do you think this statement does?  Establishes connection to database by obtaining a Connection object 26
  • 27. 27
  • 28. 2. Create JDBC statement(s)  Statement  stmt = con.createStatement() ; Creates a Statement object for sending SQL statements to the database 28
  • 29. Executing SQL Statements   String createLehigh = "Create table Lehigh " + "(SSN Integer not null, Name VARCHAR(32), " + "Marks Integer)"; stmt.executeUpdate(createLehigh); //What does this statement do? String insertLehigh = "Insert into Lehigh values“ + "(123456789,abc,100)"; stmt.executeUpdate(insertLehigh); 29
  • 30. Get ResultSet String queryLehigh = "select * from Lehigh"; ResultSet rs = Stmt.executeQuery(queryLehigh); //What does this statement do? while (rs.next()) { int ssn = rs.getInt("SSN"); String name = rs.getString("NAME"); int marks = rs.getInt("MARKS"); } 30
  • 32. Transactions and JDBC       JDBC allows SQL statements to be grouped together into a single transaction Transaction control is performed by the Connection object, default mode is auto-commit, I.e., each sql statement is treated as a transaction We can turn off the auto-commit mode with con.setAutoCommit(false); And turn it back on with con.setAutoCommit(true); Once auto-commit is off, no SQL statement will be committed until an explicit is invoked con.commit(); At this point all changes done by the SQL statements will be made permanent in the database. 32
  • 33. Handling Errors with Exceptions     Programs should recover and leave the database in a consistent state. If a statement in the try block throws an exception or warning, it can be caught in one of the corresponding catch statements How might a finally {…} block be helpful here? E.g., you could rollback your transaction in a catch { …} block or close database connection and free database related resources in finally {…} block 33
  • 34. Another way to access database (JDBC-ODBC) What’s a bit different about this architecture? Why add yet another layer? 34
  • 35. Sample program import java.sql.*; class Test { public static void main(String[] args) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //dynamic loading of driver String filename = "c:/db1.mdb"; //Location of an Access database String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="; database+= filename.trim() + ";DriverID=22;READONLY=true}"; //add on to end Connection con = DriverManager.getConnection( database ,"",""); Statement s = con.createStatement(); s.execute("create table TEST12345 ( firstcolumn integer )"); s.execute("insert into TEST12345 values(1)"); s.execute("select firstcolumn from TEST12345"); 35
  • 36. Sample program(cont) ResultSet rs = s.getResultSet(); if (rs != null) // if rs == null, then there is no ResultSet to view while ( rs.next() ) // this will step through our data row-by-row { /* the next line will get the first column in our current row's ResultSet as a String ( getString( columnNumber) ) and output it to the screen */ System.out.println("Data from column_name: " + rs.getString(1) ); } s.close(); // close Statement to let the database know we're done with it con.close(); //close connection } catch (Exception err) { System.out.println("ERROR: " + err); } } } 36
  • 37. Mapping types JDBC - Java 37
  • 38. JDBC 2 – Scrollable Result Set … Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); String query = “select students from class where type=‘not sleeping’ “; ResultSet rs = stmt.executeQuery( query ); rs.previous(); / / go back in the RS (not possible in JDBC 1…) rs.relative(-5); / / go 5 records back rs.relative(7); / / go 7 records forward rs.absolute(100); / / go to 100th record … 38
  • 39. JDBC 2 – Updateable ResultSet … Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE ); String query = " select students, grade from class where type=‘really listening this presentation’ “; ResultSet rs = stmt.executeQuery ( query ); … while ( rs.next() ) { int grade = rs.getInt(“grade”); rs.updateInt(“grade”, grade+10); rs.updateRow(); } 39
  • 40. Metadata from DB A Connection's database is able to provide schema information describing its tables, its supported SQL grammar, its stored procedures the capabilities of this connection, and so on   What is a stored procedure? Group of SQL statements that form a logical unit and perform a particular task This information is made available through a DatabaseMetaData object. 40
  • 41. Metadata from DB - example … Connection con = …. ; DatabaseMetaData dbmd = con.getMetaData(); String catalog = null; String schema = null; String table = “sys%”; String[ ] types = null; ResultSet rs = dbmd.getTables(catalog , schema , table , types ); … 41
  • 42. JDBC – Metadata from RS public static void printRS(ResultSet rs) throws SQLException { ResultSetMetaData md = rs.getMetaData(); // get number of columns int nCols = md.getColumnCount(); // print column names for(int i=1; i < nCols; ++i) System.out.print( md.getColumnName( i) +","); / / output resultset while ( rs.next() ) { for(int i=1; i < nCols; ++i) System.out.print( rs.getString( i)+","); System.out.println( rs.getString(nCols) ); } } 42
  • 43. JDBC and beyond  (JNDI) Java Naming and Directory Interface    (JDO) Java Data Object    API for network-wide sharing of information about users, machines, networks, services, and applications Preserves Java’s object model Models persistence of objects, using RDBMS as repository Save, load objects from RDBMS (SQLJ) Embedded SQL in Java     Standardized and optimized by Sybase, Oracle and IBM Java extended with directives: # sql SQL routines can invoke Java methods Maps SQL types to Java classes 43
  • 44. JDBC references  JDBC Data Access API – JDBC Technology Homepage   JDBC Database Access – The Java Tutorial   http://java.sun.com/j2se/1.4.2/docs/api/java/sql/package-summary.html JDBC Technology Guide: Getting Started   http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/index.html java.sql package   http://java.sun.com/docs/books/tutorial/jdbc/index.html JDBC Documentation   http://java.sun.com/products/jdbc/index.html http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/getstart/GettingStartedTOC.fm.html JDBC API Tutorial and Reference (book)  http://java.sun.com/docs/books/jdbc/ 45
  • 45. JDBC  JDBC Data Access API – JDBC Technology Homepage   JDBC Database Access – The Java Tutorial   http://java.sun.com/j2se/1.4.2/docs/api/java/sql/package-summary.html JDBC Technology Guide: Getting Started   http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/index.html java.sql package   http://java.sun.com/docs/books/tutorial/jdbc/index.html JDBC Documentation   http://java.sun.com/products/jdbc/index.html http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/getstart/GettingStartedTOC.fm.html JDBC API Tutorial and Reference (book)  http://java.sun.com/docs/books/jdbc/ 46