SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados (20)

Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Jdbc Ppt
Jdbc PptJdbc Ppt
Jdbc Ppt
 
Spring ppt
Spring pptSpring ppt
Spring ppt
 
JDBC ppt
JDBC pptJDBC ppt
JDBC ppt
 
Introduction of java
Introduction  of javaIntroduction  of java
Introduction of java
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!
 
Spring annotation
Spring annotationSpring annotation
Spring annotation
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
 
Jdbc complete
Jdbc completeJdbc complete
Jdbc complete
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Basics of JAVA programming
Basics of JAVA programmingBasics of JAVA programming
Basics of JAVA programming
 
Apache tomcat
Apache tomcatApache tomcat
Apache tomcat
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
Angular 8
Angular 8 Angular 8
Angular 8
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
 
Files in java
Files in javaFiles in java
Files in java
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 

Destaque (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 Tutorial
JDBC TutorialJDBC Tutorial
JDBC Tutorial
 
JDBC Java Database Connectivity
JDBC Java Database ConnectivityJDBC Java Database Connectivity
JDBC Java Database Connectivity
 
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
 
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
 
Java.sql package
Java.sql packageJava.sql package
Java.sql package
 

Semelhante a 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
 

Semelhante a 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
 

Último

General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Último (20)

General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

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