31
JDBC Version 1.0

ILP J2EE Stream J2EE 06 Jdbc v0.3

Embed Size (px)

DESCRIPTION

The Objective of J2EE course are:To understand multi-tiered enterprise applications.To understand J2EE framework for developing enterprise applications.To understand various components of J2EE like JSP, Servlets, EJB etc. and effectively use them.To understand Application Server and its configurations.To learn and deploy web based applications in application server.To learn supporting technologies like XML, JavaScript and Strut framework

Citation preview

Page 1: ILP J2EE Stream J2EE 06 Jdbc v0.3

JDBCVersion 1.0

Page 2: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

1. What is JDBC?•JDBC is an interface which allows Java code to execute SQL statements inside relational databases

– the databases must follow the ANSI SQL-2 standard

Latest Release : JDBC 3.0

Page 3: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

JDBC in use

Application

•Java code calls JDBC library

•JDBC loads a driver

•Driver talks to a particular database

JDBC Driver

Page 4: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

2. The JDBC-ODBC Bridge•ODBC (Open Database Connectivity) is a Microsoft standard from the mid 1990’s.

•It is an API that allows C/C++ programs to execute SQL inside databases

•ODBC is supported by many products.

Continued

Page 5: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

•The JDBC-ODBC bridge allows Java code to use the C/C++ interface of ODBC

– it means that JDBC can access many different database products

•The layers of translation (Java --> C --> SQL) can slow down execution.

Continued

Page 6: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

JDBC Architectures

Java Application

JDBC driver manager

JDBC/native bridge

DBMS

Native driver (DBMS specific)

JDBC/ODBC bridge

ODBC Driver

JDBC middleware (various DBMS)

JDBC Driver (DBMS Specific)

Page 7: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

3. Four Kinds of JDBC Driver

•1. JDBC-ODBC Bridge– translate Java to the ODBC API– requires ODBC driver installed on client

•2. Native API– translate Java to the database’s own API(c, c++)– access database’s API through JNI.

Continued

Page 8: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

• 3. Native Protocol– use Java to access the database more directly using its low level

protocols (database specific protocols)

•4. Net Protocol– use Java to access the database via networking middleware

(usually TCP/IP)– takes JDBC requests and translates into a network protocol that

is not database specific.– required for networked applications

Page 9: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

4. JDBC PseudoCode – six steps•All JDBC programs do the following:

– 1) import the packages• import java.sql.*;• import oracle.jdbc.driver.*;

– 2) load the JDBC driver•Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

– 3) Specify the name and location of the database being used•String url = "jdbc:oracle:thin:@aardvark:1526:teach“;

Continued

Page 10: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

– 4) Connect to the database with a Connection object•Connection conn = DriverManager.getConnection (url,user, password);

– 5) Execute a SQL query using a Statement object• Statement statement = conn.createStatement();

– 6) Get the results in a ResultSet object•ResultSet rs = statement.executeQuery(

"SELECT lastName, firstName FROM Authors" );

4. JDBC PseudoCode – six steps (cont..)

Page 11: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

4. JDBC PseudoCode – six steps (cont..)–7) Finish by closing the ResultSet, Statement and

Connection objects statement.close();

conn.close();

Page 12: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

4.1. Pseudocode as a Diagram

DriverManager Connection Statement ResultSetcreates creates creates

Driver

SQL

SQL

data

data

make linkto driver

Page 13: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

4.2. DriverManager

•It is responsible for establishing the connection to the database through the driver.

•e.g.Class.forName(

"sun.jdbc.odbc.JdbcOdbcDriver");Connection conn = DriverManager.getConnection(url);

Page 14: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

4.3. Name the Database

•The name and location of the database is given as a URL– the details of the URL vary depending on the type of

database that is being used

Page 15: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

Database URLs

The commsprotocol

The machineholding the database.

The portused for the connection.

The path tothe databaseon the machine

Jdbc:odbc:// host.domain.com 2048 /data/file

Page 16: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

Database URLs contd…•jdbc:oracle:thin:@127.0.0.1:1521:UAT02•jdbc:db2:DWCTRLDB•jdbc:odbc:Books

Page 17: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

4.4. Statement Object•The Statement object provides a ‘workspace’ where SQL queries can be created, executed, and results collected.

•e.g.Statement st =

conn.createStatement():ResultSet rs = st.executeQuery(

“ select * from Authors” );:

st.close();

Page 18: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

4.5. ResultSet Object•Stores the results of a SQL query.

•A ResultSet object is similar to a ‘table’ of answers, which can be examined by moving a ‘pointer’ (cursor).

Continued

Page 19: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

•Cursor operations:– first(), last(), next(), previous(), etc.

•Typical code:while( rs.next() ) { // process the row;}

23

5

17

98

John

Mark

Paul

Peter

cursor

Page 20: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

5. simpJDBC.java

// simpJDBC.java// Displays the firstnames and lastnames// of the Authors table in the Books db.

import java.sql.*;

public class simpJDBC {

public static void main(String[] args) { // The URL for the Books database.

// ’Protected' by a login and password. String url = "jdbc:odbc:Books"; String username = "anonymous"; String password = "guest";

:

Page 21: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

try { // load the JDBC-ODBC Bridge driver

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// connect to db using DriverManager Connection conn =

DriverManager.getConnection( url, username, password );

// Create a statement object Statement statement = conn.createStatement();

// Execute the SQL query

ResultSet rs = statement.executeQuery( "SELECT lastName, firstName FROM Authors" );

:

Page 22: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

// Print the result set while( rs.next() )

System.out.println( rs.getString("lastName") + ", " + rs.getString("firstName") );

// Close down statement.close(); conn.close(); }

:

Page 23: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

catch ( ClassNotFoundException cnfex ) { System.err.println( "Failed to load JDBC/ODBC driver." ); cnfex.printStackTrace(); System.exit( 1 ); // terminate program }

catch ( SQLException sqlex ) { System.err.println( sqlex ); sqlex.printStackTrace(); }

} // end of main()

} // end of simpJDBC class

Page 24: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

Output

Page 25: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

5.1. Username & Password•The database’s link to the outside (e.g. its ODBC interface) must be configured to have a login and password

– details for ODBC are given later

Page 26: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

5.2. Accessing a ResultSet•The ResultSet class contains many methods for accessing the value of a column of the current row

– can use the column name or position– e.g. get the value in the lastName column:

rs.getString("lastName")

Continued

Page 27: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

•The ‘tricky’ aspect is that the values are SQL data, and so they must be converted to Java types/objects.

•There are many methods for accessing/converting the data, e.g.

– getString(), getDate(), getInt(), getFloat(), getObject()

Page 28: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

6. Transaction Management•The connection has a state called AutoCommit mode•if AutoCommit is true, then every statement is automatically committed

•if AutoCommit is false, then every statement is added to an ongoing transaction

•Default: true

Page 29: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

AutoCommit

•If you set AutoCommit to false, you must explicitly commit or rollback the transaction using Connection.commit() and Connection.rollback()

Connection.setAutoCommit(boolean val)

Page 30: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

Fixed Example

con.setAutoCommit(false);try { // Create a statement object

Statement statement = conn.createStatement();

// Execute the SQL query ResultSet rs = statement.executeQuery(

"SELECT lastName, firstName FROM Authors" );

con.commit();catch (Exception e) { con.rollback();}

Page 31: ILP J2EE Stream J2EE 06 Jdbc v0.3

April 8, 2023TCS Internal

Reference•Stephanie Bodoff, et. al., The J2EE Tutorial, Sun Microsystems.

•James McGovers, et. al., J2EE1.4 Bible, Wiely Publishing Inc.