Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the...
Transcript of Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the...
![Page 1: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/1.jpg)
INTRODUCTION
Java Database Connectivity
Dr. Syed Imtiyaz HassanAssistant Professor, Deptt. of CSE,
Jamia Hamdard (Deemed to be University), New
Delhi, India.
![Page 2: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/2.jpg)
Agenda
Introduction
Product Components
Architecture
Drivers
Processing SQL
Summary
Refrences
![Page 3: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/3.jpg)
Java API
for
database-independent connectivity
between
the Java programming language
and
a wide range of databases
Introduction
![Page 4: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/4.jpg)
Helps to write Java applications that manage these three
programming activities:
Connect to a data source, like a database
Send queries and update statements to the database
Retrieve and process the results received from the
database in answer to your query
Introduction
![Page 5: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/5.jpg)
The JDBC API
java.sql
javax.sql
JDBC Driver Manager DataSource
JDBC Test Suite
JDBC-ODBC Bridge
Product Components
![Page 6: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/6.jpg)
2-tier
3-tier
N-tier
Architecture
![Page 7: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/7.jpg)
SRC: https://docs.oracle.com/javase/tutorial/jdbc/overview/index.html
Architecture … 2-tier
![Page 8: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/8.jpg)
Client connects directly to server
e.g. HTTP, email
Pro:
simple
Con:
fat client
inflexible
Architecture … 2-tier (cont.)
![Page 9: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/9.jpg)
SRC: https://docs.oracle.com/javase/tutorial/jdbc/overview/index.html
Architecture … 3-tier
![Page 10: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/10.jpg)
Pros Flexible Specialization: presentation / business logic / data management Can cache queries Can implement proxies and firewallsCons Higher complexity Higher maintenance Lower network efficiency More parts to configure
Architecture … 3-tier (cont.)
![Page 11: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/11.jpg)
N-tier data applications are data applications that are
separated into multiple tiers
Presentation Tier/Middle Tier/Data Tier
Adapted from:https://msdn.microsoft.com/en-us/library/bb384398.aspx
Architecture … N-tier
![Page 12: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/12.jpg)
Type 1: JDBC-ODBC Bridge DriverType 2: JDBC-Native API
SRC: https://www.tutorialspoint.com/jdbc/jdbc-driver-types.htm
Drivers
![Page 13: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/13.jpg)
Type 3: JDBC-Net pure Java (Network Protocol)Type 4: 100% pure Java (Native-protocol)
SRC: https://www.tutorialspoint.com/jdbc/jdbc-driver-types.htm
Driver (cont.)
![Page 14: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/14.jpg)
Video
![Page 15: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/15.jpg)
Simplicity is the ultimate sophistication.
Leonardo da Vinci
Simplest explanation is the right explanation.
Occam's razor
Everything must be made as simple as possible, but not simpler.
Albert Einstein
Simplicity
![Page 16: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/16.jpg)
1. Establishing a connection.
2. Create a statement.
3. Execute the query.
4. Process the Result.
5. Close the connection.
Processing SQL
![Page 17: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/17.jpg)
Create Statement
Close Connection
Establish Connection
Execute Query
Process Result
Processing SQL (cont.)
![Page 18: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/18.jpg)
Register the driver
Connect to the database
Create Statement
Close Connection
Establish Connection
Execute Query
Process Result
Datasource
DriverManager
Processing SQL … Establish Connection
![Page 19: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/19.jpg)
jdbc:<subprotocol>:<subname>
ProtocolDatabase
identifier
jdbc:mysql://localhost:3306/students
Subprotocol
Register Class.forName(drivername)
Connect DriverManager.getConnection(URL)
Processing SQL … Establish Connection (cont.)
![Page 20: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/20.jpg)
class.forName(“com.mysql.jdbc.Driver”);
Connection
con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/students”,
”root”,”passwd”);
Processing SQL … Establish Connection (cont.)
![Page 21: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/21.jpg)
import java.sql.*;class Select {
public static void main( String argv[] ) {try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/students";
Connection con = DriverManager.getConnection(url, "root","password");
.
.
.
.
.
.} catch (Exception e) {
e.printStackTrace();}
}}
Processing SQL … Establish Connection (cont.)
![Page 22: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/22.jpg)
Create Statement
Close Connection
Establish Connection
Execute Query
Process Result
Statement stmt = con.createStatement();
Processing SQL … Create Statement
![Page 23: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/23.jpg)
Create Statement
Close Connection
Establish Connection
Execute Query
Process Result ResultSet rs = stmt.executeQuery(statement);
int count = stmt.executeUpdate(statement);
executeQuery()
ExecuteUpdate()
Processing SQL … Execute Query
![Page 24: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/24.jpg)
ResultSet rset = stmt.executeQuery ("select *from BTCSE");
int rowcount = stmt.executeUpdate ("delete from BTCSE
where student_section = ‘A’);
Select statement
Delete statement
Processing SQL … Execute Query (cont.)
![Page 25: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/25.jpg)
import java.sql.*;class Select {
public static void main( String argv[] ) {try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/students";
Connection con = DriverManager.getConnection(url, "root","password");
Statement stmt=con.createStatement();String sql = "select * from BTCSE";ResultSet rs=stmt.executeQuery(sql);
....
} catch (Exception e) {e.printStackTrace();
}}
}
Processing SQL … Execute Query (cont.)
![Page 26: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/26.jpg)
Create Statement
Close Connection
Establish Connection
Execute Query
Process Result
ResultSet
Others
Processing SQL … Process Result
![Page 27: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/27.jpg)
while (rs.next()) { … }
String val =
rs.getString(colname);
while (rset.next()) {
String enrollment = rset.getString(“enrollment_number");
String stdname = rset.getString(2);
… // Process or display the data
}
String val =
rs.getString(colIndex);
1. Step through the ResultSet.
2. Use getXXX() to get each column value.
Processing SQL … Process Result … ResultSet
![Page 28: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/28.jpg)
import java.sql.*;class Select {
public static void main( String argv[] ) {try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/students";
Connection con = DriverManager.getConnection(url, "root","password");
Statement stmt=con.createStatement();String sql = "select * from students";
ResultSet rs=stmt.executeQuery(sql);
while ( rs.next() )System.out.println(rs.getString( 1 )+","+rs.getString( 2 ));……….
} catch (Exception e) {e.printStackTrace();
}}
}
Processing SQL … Process Result (cont.)
![Page 29: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/29.jpg)
Close the ResultSet
Close the Statement
Close the Connection
Create Statement
Close Connection
Establish Connection
Execute Query
Process Result
Processing SQL … Close Connection
![Page 30: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/30.jpg)
1. Close the ResultSet object.
2. Close the Statement object.
3. Close the connection.
rs.close();
stmt.close();
con.close();
Processing SQL … Close Connection (cont.)
![Page 31: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/31.jpg)
package advancedjava.jdbc;
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;
public class SelectRecords {public static void main(String argv[]) {
Connection con = null;Statement stmt = null;ResultSet rs = null;try {
Class.forName("com.mysql.jdbc.Driver");String url = "jdbc:mysql://localhost:3306/students";con = DriverManager.getConnection(url, "root", "password");String sql = "select * from btcse";stmt = con.createStatement();rs = stmt.executeQuery(sql);System.out.println("Enrollment\tName\tSection");while (rs.next()) {
System.out.println(rs.getString(1) + "\t" + rs.getString(2) + "\t" + rs.getInt(3));
}rs.close();stmt.close();con.close();
} catch (Exception e) {System.out.println("** Error on data select. ** " + e);
}}
}
Program Example
![Page 32: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/32.jpg)
Summary
![Page 33: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/33.jpg)
JDBC Database Access
https://docs.oracle.com/javase/tutorial/jdbc/index.html
Reference
![Page 34: Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the connection. Processing SQL. Create Statement Close Connection Establish Connection](https://reader030.fdocuments.net/reader030/viewer/2022041216/5e04b942aa6f9e21c713f710/html5/thumbnails/34.jpg)
Questions ???
Thank You