SlideShare a Scribd company logo
1 of 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

More Related Content

What's hot (20)

Sql views
Sql viewsSql views
Sql views
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
JDBC
JDBCJDBC
JDBC
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
 
Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
 
CORE JAVA
CORE JAVACORE JAVA
CORE JAVA
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
 
React js
React jsReact js
React js
 
Spring Core
Spring CoreSpring Core
Spring Core
 
Oracle Database 19c (19.3) Installation on Windows (Step-by-Step)
Oracle Database 19c (19.3) Installation on Windows (Step-by-Step)Oracle Database 19c (19.3) Installation on Windows (Step-by-Step)
Oracle Database 19c (19.3) Installation on Windows (Step-by-Step)
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
 
Spring boot
Spring bootSpring boot
Spring boot
 
4.3 MySQL + PHP
4.3 MySQL + PHP4.3 MySQL + PHP
4.3 MySQL + PHP
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
Spring Boot Tutorial
Spring Boot TutorialSpring Boot Tutorial
Spring Boot Tutorial
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
A brief introduction to SQLite PPT
A brief introduction to SQLite PPTA brief introduction to SQLite PPT
A brief introduction to SQLite PPT
 
jdbc document
jdbc documentjdbc document
jdbc document
 

Viewers also liked (20)

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 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
 
Java.sql package
Java.sql packageJava.sql package
Java.sql package
 
JDBC
JDBCJDBC
JDBC
 
Jdbc
JdbcJdbc
Jdbc
 

Similar to 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 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
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.pptDrMeenakshiS
 

Similar to 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
 
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
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
 

Recently uploaded

Ultra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxUltra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxDr. Asif Anas
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE
 
Benefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationBenefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationMJDuyan
 
The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice documentXsasf Sfdfasd
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...CaraSkikne1
 
Diploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfDiploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfMohonDas
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...Nguyen Thanh Tu Collection
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxraviapr7
 
How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17Celine George
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesMohammad Hassany
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxEduSkills OECD
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.EnglishCEIPdeSigeiro
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfMohonDas
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxMYDA ANGELICA SUAN
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?TechSoup
 
Practical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxPractical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxKatherine Villaluna
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...raviapr7
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxheathfieldcps1
 

Recently uploaded (20)

Ultra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxUltra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptx
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024
 
Benefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationBenefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive Education
 
The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice document
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...
 
Diploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfDiploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdf
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptx
 
How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming Classes
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdf
 
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdfPersonal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptx
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?
 
Practical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxPractical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptx
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptx
 
Finals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quizFinals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quiz
 

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