SlideShare a Scribd company logo
JDBC
JAVA DATABASE CONNECTIVITY
INTRODUCTION TO JDBC
1.JDBC stands for Java Database Connectivity.
2.JDBC is used to communicate with the database.
3.JDBC API is the one API which is used to connect with the database.
4.JDBC API uses the Drivers to communicate.
5. JDBC is the standard java API for independent database connection and it is Driver
dependent.
6.It is composed of number of classes and interfaces that represent a connection to the
database.
7.JDBC acts like a translator or it is API which translates java programming language to
SQL and vice-versa.
JDBC ARCHITECTURE
JAVA APPLICATION JDBC API
DB1
MYSQL
DB2
STEPS TO CONNECT
1.Load or register the driver
2.Establish Connection
3.Create Statement
4.Execute Statement
5.Close
JDBC
CONCEPTUAL COMPONENTS
1. Driver Manager: Loads database drivers and manages connections between
the application and the driver
2. Driver: Translates API calls into operations for specific database
3. Connection: Session between application and data source
4. Statement: SQL statement to perform query or update
5. Metadata: Information about returned data, database, & driver
6. Result Set: Logical set of columns and rows of data returned by executing a
statement
1.Driver Manager manages the set of Java Database Connectivity (JDBC)drivers
that are available for an application to use
2.Checks with each driver to determine if it can handle the specified URL
3.Driver Manager class can not be instantiated
• All methods of Driver Manager are static
• Constructor is private
JDBC
DRIVERMANAGER
JDBC
DRIVERS
1. JDBC Driver is a software component that enables java application to interact with the
database
2. To connect with individual databases JDBC API requires drivers for each databases
3. There are 4 types of JDBC drivers:
• JDBC-ODBC bridge driver
• Native-API driver (partially java driver)
• Network Protocol driver (fully java driver)
• Thin driver (fully java driver)
JDBC
CONNECTION
1. Required to communicate with a database via JDBC
2. Three separate methods:
public static Connection getConnection(String url)
public static Connection getConnection(String url, Properties info)
public static Connection getConnection(String url, String user, String password)
{// Load the driver class
System.out.println("Loading Class driver");
Class.forName(“com.mysql.cj.jdbc.driver");
// Define the data source for the driver
String sourceURL = "jdbc:mysql://localhost:3306/employedb”
// Create a connection through the DriverManager class
System.out.println("Getting Connection");
Connection connection = DriverManager.getConnection(sourceURL);
}
JDBC
STATEMENT
1. Statements in JDBC abstract the SQL statements
2. Primary interface to the tables in the database
3. Used to create, retrieve, update & delete data (CRUD) from a table
Syntax: Statement statement = connection.createStatement();
4. Three types of statements each reflecting a specific SQL statements
• Statement
• PreparedStatement
• CallableStatement
JDBC
STATEMENT AND PREPARED STATEMENT
STATEMENT PREPARED STATEMENT
It is used when SQL query is to be executed only
once.
It is used when SQL query is to be executed
multiple times.
You can not pass parameters at runtime. You can pass parameters at runtime.
Used for CREATE, ALTER, DROP statements. Used for the queries which are to be executed
multiple times.
Performance is very low. Performance is better than Statement.
It is base interface. It extends statement interface.
Used to execute normal SQL queries. Used to execute dynamic SQL queries.
This interface cannot be used for retrieving data
from database.
This interface can be used for retrieving data
from database.
JDBC
METHODS TO EXECUTE STATEMENT
There are 3 Methods to execute statement
execute():
• This method is used for all the commands like DDL,DML,DQL.
• Return type of execute method is Boolean.
• Return true when DQL commands are used.
• Return false when other than DQL commands are used.
executeUpdate():
• This method is used for DML commands
• Return type is int.
• DML commands like update,insert,delete.
executeQuery():
• This method is used for DQL commands
• Return type is Resultset.
• This method is used for Select query.
JDBC
RESULTSET
1. ResultSet interface represents the result set of a database query.
2. The SQL statements that read data from a database query, return the data in a
result set.
3. A ResultSet object maintains a cursor that points to the current row in the
result set.
4. The term "result set" refers to the row and column data contained in a
ResultSet object.
JDBC
PROPERTY FILE
1. A property file is one type of the file which organizing the data in the form of (key, value) pair.
2. Properties class object organizes the data in the form of (key, value) pair and it displays in the same order
in whichever order it is added.
3. Property file always resides in secondary memory.
4. Each parameter is stored as a pair of strings
5. Main advantage of Properties file is that they are outside the source code and can change any time.
6. public void load(FileInputStream); is used for loading the content of property file into properties class
object by opening the properties file in read mode with the help of FileInputStream class.
public class User{
public connection getConnection(){
Driver driver=new Driver();
DriverManager.registerDriver(driver);
FileInputStream fileinputstream=new FileInputStream(“dbconfig.properties”);
Properties properties=new Properties();
Connection connection=DriverManager.getConnection(properties.getproperty(“url”),properties.getproperties(“username”),properties.getproperties(“password”);
return connection;
}
JDBC
STATEMENT EXAMPLE
//1.load or register driver
String className="com.mysql.cj.jdbc.Driver";
Class.forName(className);
//2.establish connection
String url="jdbc:mysql://localhost:3306/studentdb";
String username="root";
String pwd="root";
Connection connection=DriverManager.getConnection(url,username,pwd);
//3.create statement
Statement statement=connection.createStatement();
//4.execute statement
String query="INSERT INTO STUDENT VALUES(4,'SHRAVAN',50,'HYDERABAD','VIJAY')";
statement.e xecute(query);
//5.CLOSE
connection.close();
JDBC
PREPARED STATEMENT CODE EXAMPLE
String className="com.mysql.cj.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/studentdb";
String username="root";
String pwd="root";
//1.LOAD OR REGISTER DRIVER
Class.forName(className);
//2.ESTABLISH CONNECTION
Connection connection=DriverManager.getConnection(url, username, pwd);
//3.CREATE STATEMENT
PreparedStatement preparedStatement = connection.prepareStatement("select * from student");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
System.out.println(resultSet.getInt(1));
System.out.println(resultSet.getString(2));
System.out.println(resultSet.getInt(3));
System.out.println(resultSet.getString(4));
System.out.println(resultSet.getString(5));
}
preparedStatement.execute();
preparedStatement.close();
connection.close();
THANK YOU

More Related Content

PPT
Final Database Connectivity in JAVA.ppt
PPT
JDBC java for learning java for learn.ppt
PPT
Jdbc (database in java)
PPT
JDBC.ppt
PPT
Java database connectivity
PPT
Java database connectivity
PPTX
Jdbc introduction
PPTX
Java DataBase Connectivity API (JDBC API)
Final Database Connectivity in JAVA.ppt
JDBC java for learning java for learn.ppt
Jdbc (database in java)
JDBC.ppt
Java database connectivity
Java database connectivity
Jdbc introduction
Java DataBase Connectivity API (JDBC API)

Similar to JDBC PPT(4).pptx java Database Connectivity (20)

PPT
jdbc_presentation.ppt
DOC
PPT
4-INTERDUCATION TO JDBC-2019.ppt
PPTX
03-JDBC.pptx
PPTX
PPT
Jdbc connectivity
PPT
Jdbc ppt
PDF
JDBC with MySQL.pdf
PDF
JDBC with MySQL.pdf
PPTX
Java database connectivity with MySql
PPTX
Java database connectivity with MySql
PDF
Overview Of JDBC
PPTX
Enterprise Spring
PPTX
Spring jdbc dao
PDF
Chapter 5 JDBC.pdf for stufent of computer andtudent It s
PDF
JDBC-Introduction
PDF
Advance Java Practical file
PPTX
PPTX
Java Database Connectivity by shreyash simu dbce.pptx
PPT
Jdbc complete
jdbc_presentation.ppt
4-INTERDUCATION TO JDBC-2019.ppt
03-JDBC.pptx
Jdbc connectivity
Jdbc ppt
JDBC with MySQL.pdf
JDBC with MySQL.pdf
Java database connectivity with MySql
Java database connectivity with MySql
Overview Of JDBC
Enterprise Spring
Spring jdbc dao
Chapter 5 JDBC.pdf for stufent of computer andtudent It s
JDBC-Introduction
Advance Java Practical file
Java Database Connectivity by shreyash simu dbce.pptx
Jdbc complete
Ad

Recently uploaded (20)

PPTX
Geodesy 1.pptx...............................................
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
Well-logging-methods_new................
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Sustainable Sites - Green Building Construction
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPT
introduction to datamining and warehousing
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Digital Logic Computer Design lecture notes
Geodesy 1.pptx...............................................
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Well-logging-methods_new................
CYBER-CRIMES AND SECURITY A guide to understanding
Sustainable Sites - Green Building Construction
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Operating System & Kernel Study Guide-1 - converted.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
R24 SURVEYING LAB MANUAL for civil enggi
introduction to datamining and warehousing
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Internet of Things (IOT) - A guide to understanding
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Digital Logic Computer Design lecture notes
Ad

JDBC PPT(4).pptx java Database Connectivity

  • 2. INTRODUCTION TO JDBC 1.JDBC stands for Java Database Connectivity. 2.JDBC is used to communicate with the database. 3.JDBC API is the one API which is used to connect with the database. 4.JDBC API uses the Drivers to communicate. 5. JDBC is the standard java API for independent database connection and it is Driver dependent. 6.It is composed of number of classes and interfaces that represent a connection to the database. 7.JDBC acts like a translator or it is API which translates java programming language to SQL and vice-versa.
  • 3. JDBC ARCHITECTURE JAVA APPLICATION JDBC API DB1 MYSQL DB2
  • 4. STEPS TO CONNECT 1.Load or register the driver 2.Establish Connection 3.Create Statement 4.Execute Statement 5.Close
  • 5. JDBC CONCEPTUAL COMPONENTS 1. Driver Manager: Loads database drivers and manages connections between the application and the driver 2. Driver: Translates API calls into operations for specific database 3. Connection: Session between application and data source 4. Statement: SQL statement to perform query or update 5. Metadata: Information about returned data, database, & driver 6. Result Set: Logical set of columns and rows of data returned by executing a statement
  • 6. 1.Driver Manager manages the set of Java Database Connectivity (JDBC)drivers that are available for an application to use 2.Checks with each driver to determine if it can handle the specified URL 3.Driver Manager class can not be instantiated • All methods of Driver Manager are static • Constructor is private JDBC DRIVERMANAGER
  • 7. JDBC DRIVERS 1. JDBC Driver is a software component that enables java application to interact with the database 2. To connect with individual databases JDBC API requires drivers for each databases 3. There are 4 types of JDBC drivers: • JDBC-ODBC bridge driver • Native-API driver (partially java driver) • Network Protocol driver (fully java driver) • Thin driver (fully java driver)
  • 8. JDBC CONNECTION 1. Required to communicate with a database via JDBC 2. Three separate methods: public static Connection getConnection(String url) public static Connection getConnection(String url, Properties info) public static Connection getConnection(String url, String user, String password) {// Load the driver class System.out.println("Loading Class driver"); Class.forName(“com.mysql.cj.jdbc.driver"); // Define the data source for the driver String sourceURL = "jdbc:mysql://localhost:3306/employedb” // Create a connection through the DriverManager class System.out.println("Getting Connection"); Connection connection = DriverManager.getConnection(sourceURL); }
  • 9. JDBC STATEMENT 1. Statements in JDBC abstract the SQL statements 2. Primary interface to the tables in the database 3. Used to create, retrieve, update & delete data (CRUD) from a table Syntax: Statement statement = connection.createStatement(); 4. Three types of statements each reflecting a specific SQL statements • Statement • PreparedStatement • CallableStatement
  • 10. JDBC STATEMENT AND PREPARED STATEMENT STATEMENT PREPARED STATEMENT It is used when SQL query is to be executed only once. It is used when SQL query is to be executed multiple times. You can not pass parameters at runtime. You can pass parameters at runtime. Used for CREATE, ALTER, DROP statements. Used for the queries which are to be executed multiple times. Performance is very low. Performance is better than Statement. It is base interface. It extends statement interface. Used to execute normal SQL queries. Used to execute dynamic SQL queries. This interface cannot be used for retrieving data from database. This interface can be used for retrieving data from database.
  • 11. JDBC METHODS TO EXECUTE STATEMENT There are 3 Methods to execute statement execute(): • This method is used for all the commands like DDL,DML,DQL. • Return type of execute method is Boolean. • Return true when DQL commands are used. • Return false when other than DQL commands are used. executeUpdate(): • This method is used for DML commands • Return type is int. • DML commands like update,insert,delete. executeQuery(): • This method is used for DQL commands • Return type is Resultset. • This method is used for Select query.
  • 12. JDBC RESULTSET 1. ResultSet interface represents the result set of a database query. 2. The SQL statements that read data from a database query, return the data in a result set. 3. A ResultSet object maintains a cursor that points to the current row in the result set. 4. The term "result set" refers to the row and column data contained in a ResultSet object.
  • 13. JDBC PROPERTY FILE 1. A property file is one type of the file which organizing the data in the form of (key, value) pair. 2. Properties class object organizes the data in the form of (key, value) pair and it displays in the same order in whichever order it is added. 3. Property file always resides in secondary memory. 4. Each parameter is stored as a pair of strings 5. Main advantage of Properties file is that they are outside the source code and can change any time. 6. public void load(FileInputStream); is used for loading the content of property file into properties class object by opening the properties file in read mode with the help of FileInputStream class. public class User{ public connection getConnection(){ Driver driver=new Driver(); DriverManager.registerDriver(driver); FileInputStream fileinputstream=new FileInputStream(“dbconfig.properties”); Properties properties=new Properties(); Connection connection=DriverManager.getConnection(properties.getproperty(“url”),properties.getproperties(“username”),properties.getproperties(“password”); return connection; }
  • 14. JDBC STATEMENT EXAMPLE //1.load or register driver String className="com.mysql.cj.jdbc.Driver"; Class.forName(className); //2.establish connection String url="jdbc:mysql://localhost:3306/studentdb"; String username="root"; String pwd="root"; Connection connection=DriverManager.getConnection(url,username,pwd); //3.create statement Statement statement=connection.createStatement(); //4.execute statement String query="INSERT INTO STUDENT VALUES(4,'SHRAVAN',50,'HYDERABAD','VIJAY')"; statement.e xecute(query); //5.CLOSE connection.close();
  • 15. JDBC PREPARED STATEMENT CODE EXAMPLE String className="com.mysql.cj.jdbc.Driver"; String url="jdbc:mysql://localhost:3306/studentdb"; String username="root"; String pwd="root"; //1.LOAD OR REGISTER DRIVER Class.forName(className); //2.ESTABLISH CONNECTION Connection connection=DriverManager.getConnection(url, username, pwd); //3.CREATE STATEMENT PreparedStatement preparedStatement = connection.prepareStatement("select * from student"); ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { System.out.println(resultSet.getInt(1)); System.out.println(resultSet.getString(2)); System.out.println(resultSet.getInt(3)); System.out.println(resultSet.getString(4)); System.out.println(resultSet.getString(5)); } preparedStatement.execute(); preparedStatement.close(); connection.close();