Java Database Connectivity · 03.02.2018 · Execute the query. 4. Process the Result. 5. Close the...

34
INTRODUCTION Java Database Connectivity Dr. Syed Imtiyaz Hassan Assistant Professor, Deptt. of CSE, Jamia Hamdard (Deemed to be University), New Delhi, India. [email protected]

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

INTRODUCTION

Java Database Connectivity

Dr. Syed Imtiyaz HassanAssistant Professor, Deptt. of CSE,

Jamia Hamdard (Deemed to be University), New

Delhi, India.

[email protected]

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Questions ???

Thank You