SlideShare a Scribd company logo
1 of 24
Java Database Connectivity (JDBC)
•  JDBC  ( J ava  D ata B ase  C onnectivity) -  provides access to  relational database  systems •  JDBC is a vendor independent API for accessing relational data from different vendors (Microsoft Access, Oracle) in a consistent way •  The language SQL (Structured Query Language) is normally used to make queries on relational data •  JDBC API provides methods for executing SQL statements and obtaining results:  SELECT, UPDATE, INSERT, DELETE   etc. •  Provides portability (eliminates rewriting code for different databases and recompiling for different platforms) and faster, reusable object developing environment •  JDBC API is part of core Java; JDBC 1.O is bundled with JDK 1.1 or higher (package: java.sql) and JDBC 2.0 (needs compliant driver; additional package: javax.sql;  JDBC
JDBC-to-database communication
•   Database vendors provide proprietary APIs for accessing data managed by the server •  Languages such as C/C++ can make use of these APIs to interface with the database •  JDBC aims at providing an API that eliminates vendor specific nature in accessing a database •  However, JDBC still requires a vendor-specific driver for accessing database from a particular vendor •  The driver provides interface between JDBC API and vendor database by converting calls from JDBC API to vendor’s database calls •  With additional functionality of the driver, the same application may be reusable with a different vendor’s database by simply switching to that driver •  Example drivers: - JDBC/ODBC driver:  sun.jdbc.odbc.JdbcOdbcDriver  (used in this course) -  Oracle driver:  oracle.jdbc.driver.OracleDriver Vendor specific APIs - JDBC Drivers
Type 1: JDBC-ODBC Bridge •   ODBC (Open Database Connectivity) is Microsoft’s API for SQL; popular on Windows platform •  ODBC API provides a set of functions for accessing a database •  JDBC drivers of this type translate calls from JDBC into corresponding ODBC calls JDBC Driver Types
[object Object],[object Object],[object Object],Type 2 - Part Java, Part Native Driver
•  An intermediate access server between client application and the data source acts as a gateway to connect to multiple database servers •  application sends JDBC calls to the intermediate server via JDBC driver •  The intermediate server handles the request using a native driver Type 3 - Intermediate Database Access Server
•  JDBC calls are directly translated to database calls specific to vendor •  very efficient in terms of performance and development time Type 4 - Pure Java Driver
•   JDBC key components:  DriverManager ,  Connection ,  Statement ,  ResultSet •  DriverManager handles communication with different drivers that conform to JDBC Driver API. - The static class  DriverManager   manages the loaded drivers and contains methods for accessing connections to the databases JDBC Driver Manager
Step 1:   load a database  driver Step 2:   make a database  connection Step 3:   create and execute SQL  statement Step 4:   process the  result   set , if necessary Four steps in creating a database application
•  A driver is always needed to obtain a connection •  Loading a driver requires class name of the driver.  For JDBC-ODBC driver the class name is: sun.jdbc.odbc.JdbcOdbcDriver Oracle driver is:  oracle.jdbc.driver.OracleDriver •  The class definition of the driver is loaded using  forName   static method of the class  Class   (in package  java.lang ) try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (Exception e) { out.println( e.getMessage() + " Class not found Exception."); } •  It is possible to load several drivers to access various databases (see for vendors list at  java.sun.com/products/jdbc  ) •  The class  DriverManager   manages the loaded drivers Step 1: Loading a Driver
•   getConnection   method of  DriverManager   class returns a connection •  When there are several databases used by the same application, the driver required to access the database is uniquely identified using JDBC URLs JDBC URL:  Represents a driver and has following three-part syntax Examples:   " jdbc : odbc : books ” :  specifies database  books  as ODBC data source (books is a logical name linked to actual database) Step 2: Opening a Database Connection
•   Getting a connection to ODBC data source  books (MS Access database) •  DriverManager  has other variants of  getConnection   method that accept different set of parameters •  Connection  is an interface defined in  java.sql   package. A Connection object represents a connection with the database. The interface has methods to create statements which can be used to manipulate the database Step 2: Opening a Database Connection(contd.)
•   Connection objects can be used to create statement objects. statement = connection.createStatement(); •  Statement is an interface that contains methods for executing SQL queries •  sqlStr  contains a string which is an SQL statement for inserting a new record in the table  Authors   in the  books  database •  The SQL statement is executed using  executeUpdate  method of the statement object •  The method is used to execute statements like  INSERT ,  UPDATE ,  DELETE  that do not return any results. It returns  number   of   rows  affected •  Authors   is one of the tables in the  books.mdb  database.  Books, Quotations, Topics   are some of the other fields. The  Authors   table consists of four fields •  Authord Id, first name, last name  and  notes . In the above example, they have the values  ’5’ , ‘Walter’, ‘Lippman’  and  ‘Journalist’  respectively Step 3: Creating Statement & executing SQL Statements
•  The  Statement  object returns a  j ava.sql.ResultSet  object upon executing an SQL statement using  executeQuery  method •  The method returns all rows in  Authors  table as a  ResultSet •  The  next ()  method of  ResultSet   allows to move from one row to the next while  ( rs.next ()) {   //  rs  stands for a row in each iteration; print author details } •  ResultSet   contains several methods for extracting various fields in a row. The methods require column name or column index as an argument. The method used depends on type of the field. For example, author’s first name can be obtained by rs.getString( "FirstName" ) FirstName   is the name of the field assigned while creating the database. The method returns author’s first name which is a string Step 4: Enquiring the Database
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Before compiling - Register a database as an ODBC Data Source
•   Click  Advanced  button in  ODBC Microsoft Access Setup  dialog.  Set Advanced Options  dialog appears •  Enter authorisation information  Login   name  and  Password . In this example, the login name is  anonymous   and password is  guest •  Click  OK  to dismiss the dialog •  Dismiss the  ODBC Microsoft Access Setup dialog  by clicking  OK. •  Now  ODBC Data Source Administrator  dialog contains data source  books  with  Microsoft Access Driver  associated with it. •  Now the database can be accessed using JDBC-ODBC bridge driver Registering as an ODBC Data Source
JDBC key components
The DriverManager Object. ,[object Object],[object Object],[object Object]
Database Connection Interface. ,[object Object],[object Object],[object Object]
Database Statement Object. ,[object Object],[object Object]
ResultSet Access Control. ,[object Object]
Basic Database operations ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Basic Database operations ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

More Related Content

What's hot

Web based database application design using vb.net and sql server
Web based database application design using vb.net and sql serverWeb based database application design using vb.net and sql server
Web based database application design using vb.net and sql serverAmmara Arooj
 
Database application and design
Database application and designDatabase application and design
Database application and designsieedah
 
5\9 SSIS 2008R2_Training - DataFlow Basics
5\9 SSIS 2008R2_Training - DataFlow Basics5\9 SSIS 2008R2_Training - DataFlow Basics
5\9 SSIS 2008R2_Training - DataFlow BasicsPramod Singla
 
Database Systems - Introduction to SQL (Chapter 3/1)
Database Systems - Introduction to SQL (Chapter 3/1)Database Systems - Introduction to SQL (Chapter 3/1)
Database Systems - Introduction to SQL (Chapter 3/1)Vidyasagar Mundroy
 
Database programming in vb net
Database programming in vb netDatabase programming in vb net
Database programming in vb netZishan yousaf
 
Dbms & prog lang
Dbms & prog langDbms & prog lang
Dbms & prog langTech_MX
 
Java session16
Java session16Java session16
Java session16Niit Care
 
6.2\9 SSIS 2008R2_Training - DataFlow Transformations
6.2\9 SSIS 2008R2_Training - DataFlow Transformations6.2\9 SSIS 2008R2_Training - DataFlow Transformations
6.2\9 SSIS 2008R2_Training - DataFlow TransformationsPramod Singla
 
What is SQL Server?
What is SQL Server?What is SQL Server?
What is SQL Server?CPD INDIA
 
Jdbc session01
Jdbc session01Jdbc session01
Jdbc session01Niit Care
 
BI Publisher Data model design document
BI Publisher Data model design documentBI Publisher Data model design document
BI Publisher Data model design documentadivasoft
 
6.1\9 SSIS 2008R2_Training - DataFlow Transformations
6.1\9 SSIS 2008R2_Training - DataFlow Transformations6.1\9 SSIS 2008R2_Training - DataFlow Transformations
6.1\9 SSIS 2008R2_Training - DataFlow TransformationsPramod Singla
 

What's hot (19)

Oracle SQL Part1
Oracle SQL Part1Oracle SQL Part1
Oracle SQL Part1
 
Web based database application design using vb.net and sql server
Web based database application design using vb.net and sql serverWeb based database application design using vb.net and sql server
Web based database application design using vb.net and sql server
 
Sql server
Sql serverSql server
Sql server
 
Database application and design
Database application and designDatabase application and design
Database application and design
 
5\9 SSIS 2008R2_Training - DataFlow Basics
5\9 SSIS 2008R2_Training - DataFlow Basics5\9 SSIS 2008R2_Training - DataFlow Basics
5\9 SSIS 2008R2_Training - DataFlow Basics
 
Database Systems - Introduction to SQL (Chapter 3/1)
Database Systems - Introduction to SQL (Chapter 3/1)Database Systems - Introduction to SQL (Chapter 3/1)
Database Systems - Introduction to SQL (Chapter 3/1)
 
Database programming in vb net
Database programming in vb netDatabase programming in vb net
Database programming in vb net
 
Dbms & prog lang
Dbms & prog langDbms & prog lang
Dbms & prog lang
 
Java session16
Java session16Java session16
Java session16
 
6.2\9 SSIS 2008R2_Training - DataFlow Transformations
6.2\9 SSIS 2008R2_Training - DataFlow Transformations6.2\9 SSIS 2008R2_Training - DataFlow Transformations
6.2\9 SSIS 2008R2_Training - DataFlow Transformations
 
Database Connection
Database ConnectionDatabase Connection
Database Connection
 
The Smartpath Information Systems | BASIC RDBMS CONCEPTS
The Smartpath Information Systems | BASIC RDBMS CONCEPTSThe Smartpath Information Systems | BASIC RDBMS CONCEPTS
The Smartpath Information Systems | BASIC RDBMS CONCEPTS
 
What is SQL Server?
What is SQL Server?What is SQL Server?
What is SQL Server?
 
Xml
XmlXml
Xml
 
Jdbc session01
Jdbc session01Jdbc session01
Jdbc session01
 
embedded-static-&dynamic
embedded-static-&dynamicembedded-static-&dynamic
embedded-static-&dynamic
 
BI Publisher Data model design document
BI Publisher Data model design documentBI Publisher Data model design document
BI Publisher Data model design document
 
JAXB
JAXBJAXB
JAXB
 
6.1\9 SSIS 2008R2_Training - DataFlow Transformations
6.1\9 SSIS 2008R2_Training - DataFlow Transformations6.1\9 SSIS 2008R2_Training - DataFlow Transformations
6.1\9 SSIS 2008R2_Training - DataFlow Transformations
 

Viewers also liked

7.Canon & Dt
7.Canon & Dt7.Canon & Dt
7.Canon & Dtphanleson
 
2.Public Vulnerability Databases
2.Public Vulnerability Databases2.Public Vulnerability Databases
2.Public Vulnerability Databasesphanleson
 
30 5 Database Jdbc
30 5 Database Jdbc30 5 Database Jdbc
30 5 Database Jdbcphanleson
 
5.Dns Rpc Nfs
5.Dns Rpc Nfs5.Dns Rpc Nfs
5.Dns Rpc Nfsphanleson
 
7.Trust Management
7.Trust Management7.Trust Management
7.Trust Managementphanleson
 
Ch09 Information Security Best Practices
Ch09 Information Security Best PracticesCh09 Information Security Best Practices
Ch09 Information Security Best Practicesphanleson
 

Viewers also liked (9)

7.Canon & Dt
7.Canon & Dt7.Canon & Dt
7.Canon & Dt
 
Thread
ThreadThread
Thread
 
2.Public Vulnerability Databases
2.Public Vulnerability Databases2.Public Vulnerability Databases
2.Public Vulnerability Databases
 
30 5 Database Jdbc
30 5 Database Jdbc30 5 Database Jdbc
30 5 Database Jdbc
 
5.Dns Rpc Nfs
5.Dns Rpc Nfs5.Dns Rpc Nfs
5.Dns Rpc Nfs
 
Rmi
RmiRmi
Rmi
 
Ch06 Policy
Ch06 PolicyCh06 Policy
Ch06 Policy
 
7.Trust Management
7.Trust Management7.Trust Management
7.Trust Management
 
Ch09 Information Security Best Practices
Ch09 Information Security Best PracticesCh09 Information Security Best Practices
Ch09 Information Security Best Practices
 

Similar to 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.pptkingkolju
 
Jdbc connectivity
Jdbc connectivityJdbc connectivity
Jdbc connectivityarikazukito
 
4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.pptNaveenKumar648465
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivityVaishali Modi
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivityVaishali Modi
 
java.pptx
java.pptxjava.pptx
java.pptxbfgd1
 
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
 
Mobile Application Devlopement-Database connections-UNIT-5
Mobile Application Devlopement-Database connections-UNIT-5Mobile Application Devlopement-Database connections-UNIT-5
Mobile Application Devlopement-Database connections-UNIT-5Pallepati Vasavi
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Pooja Talreja
 
Final Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.pptFinal Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.pptTabassumMaktum
 
Java Database Connectivity (Advanced programming)
Java Database Connectivity (Advanced programming)Java Database Connectivity (Advanced programming)
Java Database Connectivity (Advanced programming)Gera Paulos
 

Similar to Jdbc (20)

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
 
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
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
java.pptx
java.pptxjava.pptx
java.pptx
 
JDBC
JDBCJDBC
JDBC
 
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...
 
Mobile Application Devlopement-Database connections-UNIT-5
Mobile Application Devlopement-Database connections-UNIT-5Mobile Application Devlopement-Database connections-UNIT-5
Mobile Application Devlopement-Database connections-UNIT-5
 
Chap3 3 12
Chap3 3 12Chap3 3 12
Chap3 3 12
 
Assignment#10
Assignment#10Assignment#10
Assignment#10
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
 
Final Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.pptFinal Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.ppt
 
jdbc
jdbcjdbc
jdbc
 
Java Database Connectivity (Advanced programming)
Java Database Connectivity (Advanced programming)Java Database Connectivity (Advanced programming)
Java Database Connectivity (Advanced programming)
 
JDBC
JDBCJDBC
JDBC
 
Unit 5.pdf
Unit 5.pdfUnit 5.pdf
Unit 5.pdf
 
JDBC.ppt
JDBC.pptJDBC.ppt
JDBC.ppt
 
Jdbc introduction
Jdbc introductionJdbc introduction
Jdbc introduction
 

More from phanleson

Learning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with SparkLearning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with Sparkphanleson
 
Firewall - Network Defense in Depth Firewalls
Firewall - Network Defense in Depth FirewallsFirewall - Network Defense in Depth Firewalls
Firewall - Network Defense in Depth Firewallsphanleson
 
Mobile Security - Wireless hacking
Mobile Security - Wireless hackingMobile Security - Wireless hacking
Mobile Security - Wireless hackingphanleson
 
Authentication in wireless - Security in Wireless Protocols
Authentication in wireless - Security in Wireless ProtocolsAuthentication in wireless - Security in Wireless Protocols
Authentication in wireless - Security in Wireless Protocolsphanleson
 
E-Commerce Security - Application attacks - Server Attacks
E-Commerce Security - Application attacks - Server AttacksE-Commerce Security - Application attacks - Server Attacks
E-Commerce Security - Application attacks - Server Attacksphanleson
 
Hacking web applications
Hacking web applicationsHacking web applications
Hacking web applicationsphanleson
 
HBase In Action - Chapter 04: HBase table design
HBase In Action - Chapter 04: HBase table designHBase In Action - Chapter 04: HBase table design
HBase In Action - Chapter 04: HBase table designphanleson
 
HBase In Action - Chapter 10 - Operations
HBase In Action - Chapter 10 - OperationsHBase In Action - Chapter 10 - Operations
HBase In Action - Chapter 10 - Operationsphanleson
 
Hbase in action - Chapter 09: Deploying HBase
Hbase in action - Chapter 09: Deploying HBaseHbase in action - Chapter 09: Deploying HBase
Hbase in action - Chapter 09: Deploying HBasephanleson
 
Learning spark ch11 - Machine Learning with MLlib
Learning spark ch11 - Machine Learning with MLlibLearning spark ch11 - Machine Learning with MLlib
Learning spark ch11 - Machine Learning with MLlibphanleson
 
Learning spark ch10 - Spark Streaming
Learning spark ch10 - Spark StreamingLearning spark ch10 - Spark Streaming
Learning spark ch10 - Spark Streamingphanleson
 
Learning spark ch09 - Spark SQL
Learning spark ch09 - Spark SQLLearning spark ch09 - Spark SQL
Learning spark ch09 - Spark SQLphanleson
 
Learning spark ch07 - Running on a Cluster
Learning spark ch07 - Running on a ClusterLearning spark ch07 - Running on a Cluster
Learning spark ch07 - Running on a Clusterphanleson
 
Learning spark ch06 - Advanced Spark Programming
Learning spark ch06 - Advanced Spark ProgrammingLearning spark ch06 - Advanced Spark Programming
Learning spark ch06 - Advanced Spark Programmingphanleson
 
Learning spark ch05 - Loading and Saving Your Data
Learning spark ch05 - Loading and Saving Your DataLearning spark ch05 - Loading and Saving Your Data
Learning spark ch05 - Loading and Saving Your Dataphanleson
 
Learning spark ch04 - Working with Key/Value Pairs
Learning spark ch04 - Working with Key/Value PairsLearning spark ch04 - Working with Key/Value Pairs
Learning spark ch04 - Working with Key/Value Pairsphanleson
 
Learning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with SparkLearning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with Sparkphanleson
 
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagia
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about LibertagiaHướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagia
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagiaphanleson
 
Lecture 1 - Getting to know XML
Lecture 1 - Getting to know XMLLecture 1 - Getting to know XML
Lecture 1 - Getting to know XMLphanleson
 
Lecture 4 - Adding XTHML for the Web
Lecture  4 - Adding XTHML for the WebLecture  4 - Adding XTHML for the Web
Lecture 4 - Adding XTHML for the Webphanleson
 

More from phanleson (20)

Learning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with SparkLearning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with Spark
 
Firewall - Network Defense in Depth Firewalls
Firewall - Network Defense in Depth FirewallsFirewall - Network Defense in Depth Firewalls
Firewall - Network Defense in Depth Firewalls
 
Mobile Security - Wireless hacking
Mobile Security - Wireless hackingMobile Security - Wireless hacking
Mobile Security - Wireless hacking
 
Authentication in wireless - Security in Wireless Protocols
Authentication in wireless - Security in Wireless ProtocolsAuthentication in wireless - Security in Wireless Protocols
Authentication in wireless - Security in Wireless Protocols
 
E-Commerce Security - Application attacks - Server Attacks
E-Commerce Security - Application attacks - Server AttacksE-Commerce Security - Application attacks - Server Attacks
E-Commerce Security - Application attacks - Server Attacks
 
Hacking web applications
Hacking web applicationsHacking web applications
Hacking web applications
 
HBase In Action - Chapter 04: HBase table design
HBase In Action - Chapter 04: HBase table designHBase In Action - Chapter 04: HBase table design
HBase In Action - Chapter 04: HBase table design
 
HBase In Action - Chapter 10 - Operations
HBase In Action - Chapter 10 - OperationsHBase In Action - Chapter 10 - Operations
HBase In Action - Chapter 10 - Operations
 
Hbase in action - Chapter 09: Deploying HBase
Hbase in action - Chapter 09: Deploying HBaseHbase in action - Chapter 09: Deploying HBase
Hbase in action - Chapter 09: Deploying HBase
 
Learning spark ch11 - Machine Learning with MLlib
Learning spark ch11 - Machine Learning with MLlibLearning spark ch11 - Machine Learning with MLlib
Learning spark ch11 - Machine Learning with MLlib
 
Learning spark ch10 - Spark Streaming
Learning spark ch10 - Spark StreamingLearning spark ch10 - Spark Streaming
Learning spark ch10 - Spark Streaming
 
Learning spark ch09 - Spark SQL
Learning spark ch09 - Spark SQLLearning spark ch09 - Spark SQL
Learning spark ch09 - Spark SQL
 
Learning spark ch07 - Running on a Cluster
Learning spark ch07 - Running on a ClusterLearning spark ch07 - Running on a Cluster
Learning spark ch07 - Running on a Cluster
 
Learning spark ch06 - Advanced Spark Programming
Learning spark ch06 - Advanced Spark ProgrammingLearning spark ch06 - Advanced Spark Programming
Learning spark ch06 - Advanced Spark Programming
 
Learning spark ch05 - Loading and Saving Your Data
Learning spark ch05 - Loading and Saving Your DataLearning spark ch05 - Loading and Saving Your Data
Learning spark ch05 - Loading and Saving Your Data
 
Learning spark ch04 - Working with Key/Value Pairs
Learning spark ch04 - Working with Key/Value PairsLearning spark ch04 - Working with Key/Value Pairs
Learning spark ch04 - Working with Key/Value Pairs
 
Learning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with SparkLearning spark ch01 - Introduction to Data Analysis with Spark
Learning spark ch01 - Introduction to Data Analysis with Spark
 
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagia
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about LibertagiaHướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagia
Hướng Dẫn Đăng Ký LibertaGia - A guide and introduciton about Libertagia
 
Lecture 1 - Getting to know XML
Lecture 1 - Getting to know XMLLecture 1 - Getting to know XML
Lecture 1 - Getting to know XML
 
Lecture 4 - Adding XTHML for the Web
Lecture  4 - Adding XTHML for the WebLecture  4 - Adding XTHML for the Web
Lecture 4 - Adding XTHML for the Web
 

Jdbc

  • 2. • JDBC ( J ava D ata B ase C onnectivity) - provides access to relational database systems • JDBC is a vendor independent API for accessing relational data from different vendors (Microsoft Access, Oracle) in a consistent way • The language SQL (Structured Query Language) is normally used to make queries on relational data • JDBC API provides methods for executing SQL statements and obtaining results: SELECT, UPDATE, INSERT, DELETE etc. • Provides portability (eliminates rewriting code for different databases and recompiling for different platforms) and faster, reusable object developing environment • JDBC API is part of core Java; JDBC 1.O is bundled with JDK 1.1 or higher (package: java.sql) and JDBC 2.0 (needs compliant driver; additional package: javax.sql; JDBC
  • 4. Database vendors provide proprietary APIs for accessing data managed by the server • Languages such as C/C++ can make use of these APIs to interface with the database • JDBC aims at providing an API that eliminates vendor specific nature in accessing a database • However, JDBC still requires a vendor-specific driver for accessing database from a particular vendor • The driver provides interface between JDBC API and vendor database by converting calls from JDBC API to vendor’s database calls • With additional functionality of the driver, the same application may be reusable with a different vendor’s database by simply switching to that driver • Example drivers: - JDBC/ODBC driver: sun.jdbc.odbc.JdbcOdbcDriver (used in this course) - Oracle driver: oracle.jdbc.driver.OracleDriver Vendor specific APIs - JDBC Drivers
  • 5. Type 1: JDBC-ODBC Bridge • ODBC (Open Database Connectivity) is Microsoft’s API for SQL; popular on Windows platform • ODBC API provides a set of functions for accessing a database • JDBC drivers of this type translate calls from JDBC into corresponding ODBC calls JDBC Driver Types
  • 6.
  • 7. • An intermediate access server between client application and the data source acts as a gateway to connect to multiple database servers • application sends JDBC calls to the intermediate server via JDBC driver • The intermediate server handles the request using a native driver Type 3 - Intermediate Database Access Server
  • 8. • JDBC calls are directly translated to database calls specific to vendor • very efficient in terms of performance and development time Type 4 - Pure Java Driver
  • 9. JDBC key components: DriverManager , Connection , Statement , ResultSet • DriverManager handles communication with different drivers that conform to JDBC Driver API. - The static class DriverManager manages the loaded drivers and contains methods for accessing connections to the databases JDBC Driver Manager
  • 10. Step 1: load a database driver Step 2: make a database connection Step 3: create and execute SQL statement Step 4: process the result set , if necessary Four steps in creating a database application
  • 11. • A driver is always needed to obtain a connection • Loading a driver requires class name of the driver. For JDBC-ODBC driver the class name is: sun.jdbc.odbc.JdbcOdbcDriver Oracle driver is: oracle.jdbc.driver.OracleDriver • The class definition of the driver is loaded using forName static method of the class Class (in package java.lang ) try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (Exception e) { out.println( e.getMessage() + " Class not found Exception."); } • It is possible to load several drivers to access various databases (see for vendors list at java.sun.com/products/jdbc ) • The class DriverManager manages the loaded drivers Step 1: Loading a Driver
  • 12. getConnection method of DriverManager class returns a connection • When there are several databases used by the same application, the driver required to access the database is uniquely identified using JDBC URLs JDBC URL: Represents a driver and has following three-part syntax Examples: " jdbc : odbc : books ” : specifies database books as ODBC data source (books is a logical name linked to actual database) Step 2: Opening a Database Connection
  • 13. Getting a connection to ODBC data source books (MS Access database) • DriverManager has other variants of getConnection method that accept different set of parameters • Connection is an interface defined in java.sql package. A Connection object represents a connection with the database. The interface has methods to create statements which can be used to manipulate the database Step 2: Opening a Database Connection(contd.)
  • 14. Connection objects can be used to create statement objects. statement = connection.createStatement(); • Statement is an interface that contains methods for executing SQL queries • sqlStr contains a string which is an SQL statement for inserting a new record in the table Authors in the books database • The SQL statement is executed using executeUpdate method of the statement object • The method is used to execute statements like INSERT , UPDATE , DELETE that do not return any results. It returns number of rows affected • Authors is one of the tables in the books.mdb database. Books, Quotations, Topics are some of the other fields. The Authors table consists of four fields • Authord Id, first name, last name and notes . In the above example, they have the values ’5’ , ‘Walter’, ‘Lippman’ and ‘Journalist’ respectively Step 3: Creating Statement & executing SQL Statements
  • 15. • The Statement object returns a j ava.sql.ResultSet object upon executing an SQL statement using executeQuery method • The method returns all rows in Authors table as a ResultSet • The next () method of ResultSet allows to move from one row to the next while ( rs.next ()) { // rs stands for a row in each iteration; print author details } • ResultSet contains several methods for extracting various fields in a row. The methods require column name or column index as an argument. The method used depends on type of the field. For example, author’s first name can be obtained by rs.getString( "FirstName" ) FirstName is the name of the field assigned while creating the database. The method returns author’s first name which is a string Step 4: Enquiring the Database
  • 16.
  • 17. Click Advanced button in ODBC Microsoft Access Setup dialog. Set Advanced Options dialog appears • Enter authorisation information Login name and Password . In this example, the login name is anonymous and password is guest • Click OK to dismiss the dialog • Dismiss the ODBC Microsoft Access Setup dialog by clicking OK. • Now ODBC Data Source Administrator dialog contains data source books with Microsoft Access Driver associated with it. • Now the database can be accessed using JDBC-ODBC bridge driver Registering as an ODBC Data Source
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.