Michigan State University€¦ · 03.02.2018 · Author: Main Office Created Date: 1/31/2018 12:03:26 PM
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...
INTRODUCTION
Java Database Connectivity
Dr. Syed Imtiyaz HassanAssistant Professor, Deptt. of CSE,
Jamia Hamdard (Deemed to be University), New
Delhi, India.
Agenda
Introduction
Product Components
Architecture
Drivers
Processing SQL
Summary
Refrences
Java API
for
database-independent connectivity
between
the Java programming language
and
a wide range of databases
Introduction
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
The JDBC API
java.sql
javax.sql
JDBC Driver Manager DataSource
JDBC Test Suite
JDBC-ODBC Bridge
Product Components
2-tier
3-tier
N-tier
Architecture
SRC: https://docs.oracle.com/javase/tutorial/jdbc/overview/index.html
Architecture … 2-tier
Client connects directly to server
e.g. HTTP, email
Pro:
simple
Con:
fat client
inflexible
Architecture … 2-tier (cont.)
SRC: https://docs.oracle.com/javase/tutorial/jdbc/overview/index.html
Architecture … 3-tier
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.)
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
Type 1: JDBC-ODBC Bridge DriverType 2: JDBC-Native API
SRC: https://www.tutorialspoint.com/jdbc/jdbc-driver-types.htm
Drivers
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.)
Video
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
1. Establishing a connection.
2. Create a statement.
3. Execute the query.
4. Process the Result.
5. Close the connection.
Processing SQL
Create Statement
Close Connection
Establish Connection
Execute Query
Process Result
Processing SQL (cont.)
Register the driver
Connect to the database
Create Statement
Close Connection
Establish Connection
Execute Query
Process Result
Datasource
DriverManager
Processing SQL … Establish Connection
jdbc:<subprotocol>:<subname>
ProtocolDatabase
identifier
jdbc:mysql://localhost:3306/students
Subprotocol
Register Class.forName(drivername)
Connect DriverManager.getConnection(URL)
Processing SQL … Establish Connection (cont.)
class.forName(“com.mysql.jdbc.Driver”);
Connection
con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/students”,
”root”,”passwd”);
Processing SQL … Establish Connection (cont.)
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.)
Create Statement
Close Connection
Establish Connection
Execute Query
Process Result
Statement stmt = con.createStatement();
Processing SQL … Create Statement
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
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.)
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.)
Create Statement
Close Connection
Establish Connection
Execute Query
Process Result
ResultSet
Others
Processing SQL … Process Result
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
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.)
Close the ResultSet
Close the Statement
Close the Connection
Create Statement
Close Connection
Establish Connection
Execute Query
Process Result
Processing SQL … Close Connection
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.)
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
Summary
JDBC Database Access
https://docs.oracle.com/javase/tutorial/jdbc/index.html
Reference
Questions ???
Thank You