SlideShare ist ein Scribd-Unternehmen logo
1 von 16
JDBC
Nitesh Kumar Pandey
JDBC is the Java API(Application Programming Interface) for accessing relational
database.
• The Java API for developing Java database application is called JDBC. JDBC is the
trademark name of Java API that supports Java programs that access relational
databases.
Do you believe that JDBC is not acronym?
• Yes, JDBC is not an acronym, but it is often thought to stand for Java Database
Connectivity.
• Using the JDBC API, applications written in the Java programming language can
execute SQL statements, retrieve results, present data in a user friendly interface
and propagate changes back to the database.
JDBC
The relationship between Java Programs, JDBC
API , Relational Database
• In simplest terms, a JDBC technology-based
driver(“JDBC driver”) makes it possible to do
three things:
1. Establish a connection with a data source
2. Send queries and update statements to the
data source
3. Process the results
What Does the JDBC API Do?
JDBC Components
Developing Database Application Using JDBC
• JDBC API consists of classes and interfaces for establishing connections with
databases, sending SQL statements to databases, processing the results of SQL
statements, and obtaining database metadata.
• There are Four key interfaces are needed to develop any database applications using
Java:
Driver, Connection, Statement, and ResultSet.
• A JDBC application loads an appropriate driver using the Driver interface.
• connects to the database using the Connection interface,
• creates and executes SQL statements using Statement interface,
• and processes the result using the ResultSet interface if the statement return
results.
Relationship of interfaces
1. Loading Driver
An appropriate driver must be loaded using the statement:
Class.forName(“JDBCDriverClass”);
• A driver is concrete class that implements the java.sql.Driver interface.
• The Driver for MS Access, MySQL, and Oracle all are different we must need load
with specific driver all database
• For example if we use MS Access Database then
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
• For Oracle
Class.forName(“oracle.jdbc.driver.OracleDriver”);
•For MySQL
Class.forName(“com.jdbc.mysql.Driver”);
Database Driver Class Source
Access sun.jdbc.odbc.JdbcOdbcDriver Already in JDK
Mysql com.mysql.jdbc.Driver mysql-connector-java-5.1.26.jar
Oracle oracle.jdbc.driver.OracleDriver ojdbc6.jar,ojdbc7.jar,ojdbc14.jar
We need to add mysql-connector-java-5.1.26.jar and ojdbc6.jar in the classpath using
the DOS command on windows:
Set classpath=%classpath%;c:mysql-connector-java-5.1.26.jar;c:ojdbc6.jar
Where we assume that jar file in C Drive
JDBC Drivers
2. Establishing Connection
• To connect to a database, use the static method getConnection(databaseURL) in the
DriverManager class,
Connection con = DriverManager.getConnection(databaseURL);
Where databaseURL is the unique identifier of the databse on the Internet.
Database URL Pattern
Access jdbc:odbc:dataSource
MySQL jdbc:mysql://hostname/dbname
Oracle jdbc:oracle:thin:@hostname:port#:oracleDBSID
JDBC URLs
• Suppose a data source named MSAccessDataSource has been created for an
Access database
Connection con = DriverManager.getConnection
(“jdbc:odbc:MSAccessDataSoucre”);
3. Creating Statements
Once a Connection object is created, you can create statements for executing SQL
statement for example:
Statement st = con.createStatement();
This statement common for all database.
4. Executing statement
• SQL Data Definition Language(DDL) and update statements can be executed using
executeUpdate(String sql)
• An SQL query statement can be executed using executeQuery(String sql)
• The result of the query is returned in ResultSet.
• For example, the following code executes the SQL statement create table
Student(RollNo number, Fname varchar(12), Mname varchar(10), Lname varchar(10)):
st.executeUpdate
(“create table Student(RollNo number, Fname varchar(12), Mname varchar(10),
Lname varchar(10))”);
• next code executes the SQL query select * from students
st.executeQuery(“select * from student”);
**Whare st is reference variable for Statement.
5. Processing ResultSet
• The ResultSet maintains a table whose current row can be retrieved. The initial row
position is null. We can use the next method to move to the next row and the various
getter methods to retrieve values from a current row.
while (resultSet.next())
System.out.println(resultSet.getString(1)+” ”+ resultSet.getString(2)+ “ ” +
resultSet.getString(3));
• Suppose a table contain three column fname, mi, lname.
The getString(1), getString(2), and getString(3) methods retrieve the column values
for fname, mi and lname respectively.
• Alternatively, we use getString(“fname”), getString(“mi”), and getString(“lname”) to
retrieve the same three column value.
import java.sql.*;
public class SimpleJDBC{
public static void main(String args[])
throws SQLException, ClassNotFoundException{
Class.forName(“oracle.jdbc.driver.OracleDriver”); //Load driver
Connection con=DriverManager.getConnection
(jdbc:oracle:thin:@localhost:1521:xe, ”root” , ”root”); //Connect to a database
Statement st=con.createStatement(); //Create Statement
ResultSet rs=st.executeQuery(“select * from student”); //Execute a statement
//Iterate through the result and print the student names
While(rs.next())
System.out.print(rs.getString(1));
System.out.println(“t”rs.getString(2));
con.close(); //Close the connection
}
}
SimpleJDBC.java

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERS
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Java Spring Framework
Java Spring FrameworkJava Spring Framework
Java Spring Framework
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
 
Introduction to java
Introduction to java Introduction to java
Introduction to java
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java Spring
Java SpringJava Spring
Java Spring
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
 
Introduction to Spring Framework and Spring IoC
Introduction to Spring Framework and Spring IoCIntroduction to Spring Framework and Spring IoC
Introduction to Spring Framework and Spring IoC
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
JDBC
JDBCJDBC
JDBC
 
JPA and Hibernate
JPA and HibernateJPA and Hibernate
JPA and Hibernate
 
Oracle Office Hours - Exposing REST services with APEX and ORDS
Oracle Office Hours - Exposing REST services with APEX and ORDSOracle Office Hours - Exposing REST services with APEX and ORDS
Oracle Office Hours - Exposing REST services with APEX and ORDS
 

Andere mochten auch (20)

JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
Jdbc Ppt
Jdbc PptJdbc Ppt
Jdbc Ppt
 
JDBC Tutorial
JDBC TutorialJDBC Tutorial
JDBC Tutorial
 
Jdbc
JdbcJdbc
Jdbc
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
 
Jdbc
JdbcJdbc
Jdbc
 
JDBC ppt
JDBC pptJDBC ppt
JDBC ppt
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
 
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 )
 
Jsp presentation
Jsp presentationJsp presentation
Jsp presentation
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Weather patterns
Weather patternsWeather patterns
Weather patterns
 
Types of Drivers in JDBC
Types of Drivers in JDBCTypes of Drivers in JDBC
Types of Drivers in JDBC
 
Overview Of JDBC
Overview Of JDBCOverview Of JDBC
Overview Of JDBC
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)
 
Jdbc
JdbcJdbc
Jdbc
 
Jsp sasidhar
Jsp sasidharJsp sasidhar
Jsp sasidhar
 

Ähnlich wie Jdbc

Jdbc connectivity
Jdbc connectivityJdbc connectivity
Jdbc connectivityarikazukito
 
BI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
BI, Integration, and Apps on Couchbase using Simba ODBC and JDBCBI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
BI, Integration, and Apps on Couchbase using Simba ODBC and JDBCSimba Technologies
 
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 ISivaSankari36
 
4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.pptNaveenKumar648465
 
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 Complete Notes by Java Training Center (Som Sir)
Jdbc Complete Notes by Java Training Center (Som Sir)Jdbc Complete Notes by Java Training Center (Som Sir)
Jdbc Complete Notes by Java Training Center (Som Sir)Som Prakash Rai
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Pooja Talreja
 
Java- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionJava- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionMazenetsolution
 
Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Luzan Baral
 

Ähnlich wie Jdbc (20)

Jdbc connectivity
Jdbc connectivityJdbc connectivity
Jdbc connectivity
 
Chap3 3 12
Chap3 3 12Chap3 3 12
Chap3 3 12
 
JDBC
JDBCJDBC
JDBC
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
BI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
BI, Integration, and Apps on Couchbase using Simba ODBC and JDBCBI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
BI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
 
Jdbc
JdbcJdbc
Jdbc
 
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
 
4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.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
 
Java JDBC
Java JDBCJava JDBC
Java JDBC
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Jdbc Complete Notes by Java Training Center (Som Sir)
Jdbc Complete Notes by Java Training Center (Som Sir)Jdbc Complete Notes by Java Training Center (Som Sir)
Jdbc Complete Notes by Java Training Center (Som Sir)
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
 
Lecture 1. java database connectivity
Lecture 1. java database connectivityLecture 1. java database connectivity
Lecture 1. java database connectivity
 
Java- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionJava- JDBC- Mazenet Solution
Java- JDBC- Mazenet Solution
 
Unit 5.pdf
Unit 5.pdfUnit 5.pdf
Unit 5.pdf
 
Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)
 
10 J D B C
10  J D B C10  J D B C
10 J D B C
 

Kürzlich hochgeladen

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 

Kürzlich hochgeladen (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

Jdbc

  • 2. JDBC is the Java API(Application Programming Interface) for accessing relational database. • The Java API for developing Java database application is called JDBC. JDBC is the trademark name of Java API that supports Java programs that access relational databases. Do you believe that JDBC is not acronym? • Yes, JDBC is not an acronym, but it is often thought to stand for Java Database Connectivity. • Using the JDBC API, applications written in the Java programming language can execute SQL statements, retrieve results, present data in a user friendly interface and propagate changes back to the database. JDBC
  • 3. The relationship between Java Programs, JDBC API , Relational Database
  • 4. • In simplest terms, a JDBC technology-based driver(“JDBC driver”) makes it possible to do three things: 1. Establish a connection with a data source 2. Send queries and update statements to the data source 3. Process the results What Does the JDBC API Do?
  • 6. Developing Database Application Using JDBC • JDBC API consists of classes and interfaces for establishing connections with databases, sending SQL statements to databases, processing the results of SQL statements, and obtaining database metadata. • There are Four key interfaces are needed to develop any database applications using Java: Driver, Connection, Statement, and ResultSet. • A JDBC application loads an appropriate driver using the Driver interface. • connects to the database using the Connection interface, • creates and executes SQL statements using Statement interface, • and processes the result using the ResultSet interface if the statement return results.
  • 8. 1. Loading Driver An appropriate driver must be loaded using the statement: Class.forName(“JDBCDriverClass”); • A driver is concrete class that implements the java.sql.Driver interface. • The Driver for MS Access, MySQL, and Oracle all are different we must need load with specific driver all database • For example if we use MS Access Database then Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); • For Oracle Class.forName(“oracle.jdbc.driver.OracleDriver”); •For MySQL Class.forName(“com.jdbc.mysql.Driver”);
  • 9. Database Driver Class Source Access sun.jdbc.odbc.JdbcOdbcDriver Already in JDK Mysql com.mysql.jdbc.Driver mysql-connector-java-5.1.26.jar Oracle oracle.jdbc.driver.OracleDriver ojdbc6.jar,ojdbc7.jar,ojdbc14.jar We need to add mysql-connector-java-5.1.26.jar and ojdbc6.jar in the classpath using the DOS command on windows: Set classpath=%classpath%;c:mysql-connector-java-5.1.26.jar;c:ojdbc6.jar Where we assume that jar file in C Drive JDBC Drivers
  • 10. 2. Establishing Connection • To connect to a database, use the static method getConnection(databaseURL) in the DriverManager class, Connection con = DriverManager.getConnection(databaseURL); Where databaseURL is the unique identifier of the databse on the Internet.
  • 11. Database URL Pattern Access jdbc:odbc:dataSource MySQL jdbc:mysql://hostname/dbname Oracle jdbc:oracle:thin:@hostname:port#:oracleDBSID JDBC URLs
  • 12. • Suppose a data source named MSAccessDataSource has been created for an Access database Connection con = DriverManager.getConnection (“jdbc:odbc:MSAccessDataSoucre”);
  • 13. 3. Creating Statements Once a Connection object is created, you can create statements for executing SQL statement for example: Statement st = con.createStatement(); This statement common for all database.
  • 14. 4. Executing statement • SQL Data Definition Language(DDL) and update statements can be executed using executeUpdate(String sql) • An SQL query statement can be executed using executeQuery(String sql) • The result of the query is returned in ResultSet. • For example, the following code executes the SQL statement create table Student(RollNo number, Fname varchar(12), Mname varchar(10), Lname varchar(10)): st.executeUpdate (“create table Student(RollNo number, Fname varchar(12), Mname varchar(10), Lname varchar(10))”); • next code executes the SQL query select * from students st.executeQuery(“select * from student”); **Whare st is reference variable for Statement.
  • 15. 5. Processing ResultSet • The ResultSet maintains a table whose current row can be retrieved. The initial row position is null. We can use the next method to move to the next row and the various getter methods to retrieve values from a current row. while (resultSet.next()) System.out.println(resultSet.getString(1)+” ”+ resultSet.getString(2)+ “ ” + resultSet.getString(3)); • Suppose a table contain three column fname, mi, lname. The getString(1), getString(2), and getString(3) methods retrieve the column values for fname, mi and lname respectively. • Alternatively, we use getString(“fname”), getString(“mi”), and getString(“lname”) to retrieve the same three column value.
  • 16. import java.sql.*; public class SimpleJDBC{ public static void main(String args[]) throws SQLException, ClassNotFoundException{ Class.forName(“oracle.jdbc.driver.OracleDriver”); //Load driver Connection con=DriverManager.getConnection (jdbc:oracle:thin:@localhost:1521:xe, ”root” , ”root”); //Connect to a database Statement st=con.createStatement(); //Create Statement ResultSet rs=st.executeQuery(“select * from student”); //Execute a statement //Iterate through the result and print the student names While(rs.next()) System.out.print(rs.getString(1)); System.out.println(“t”rs.getString(2)); con.close(); //Close the connection } } SimpleJDBC.java