ch. nonamemarks 01awt24 02networking18 03jdbc20 04swing18 05servlet20 advance java programming

35
Ch. No Name Marks 01 AWT 24 02 Networkin g 18 03 JDBC 20 04 Swing 18 05 Servlet 20 Advance Java Programming

Upload: miles-haynes

Post on 18-Jan-2018

213 views

Category:

Documents


0 download

DESCRIPTION

Advantage Simple in Design Easy to maintain and modify Communication is fast Disadvantages: Inflexible Limited Client Less secure Cost ineffective

TRANSCRIPT

Page 1: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

Ch. No Name Marks

01 AWT 24

02 Networking 18

03 JDBC 20

04 Swing 18

05 Servlet 20

Advance Java Programming

Page 2: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

2 IBM

Two-Tier Architecture

• Client connects directly to server

• e.g. HTTP

Page 3: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

Advantage• Simple in Design• Easy to maintain and modify• Communication is fast

• Disadvantages:• Inflexible• Limited Client• Less secure• Cost ineffective

Page 4: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

4 IBM

Three-Tier Architecture• Application Server

sits between client and database

User Interface

UI Logic

Business Logic

DatabaseTables

User DataDML OperationsValidationChecks

Database Server Layer

Client Layer

Application Server Layer

Business Messages

Netw

orkN

etwork

Page 5: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

Advantage

• Flexible: one part can change without affecting other

• Can connect to different database• High performance• Data integrity improve• More secure• Can implement proxy and firewall

Page 6: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

Disadvantages

• High Maintenance• High Complexity• Lower network efficiency

Page 7: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

7

Java Application Database• The below given figure shows the Employee Logging System

application developed in Java interacting with the Employee database using the JDBC API:

EmpName

E-Mail Address

Employee Logging System

EmployeeLog

Name Email

RDBMS

JDBC API

Connects to

Submit Clear

Page 8: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

8

It can be categorized into into two layers:

Java

Application

JDBC

API

Oracle

DB2

SQL Server

Driver

Driver

Driver

JDBC Application Layer JDBC Driver Layer

JDBC Architecture

Page 9: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

9 IBM

JDBC Drivers

• Type I: “Bridge” -

• Type II: “Native” -

• Type III: “Middleware” -

• Type IV: “Pure” -

JDBC-ODBC Bridge Driver

Native-API Partly-Java Driver

JDBC-Net Pure-Java Driver

Native Protocol Pure-Java Driver

Overview of All Drivers

Page 10: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

10 IBM

Type I Drivers• Use bridging technology

• Translates query obtained by JDBC into corresponding ODBC query, which is then handled by the ODBC driver.

• Almost any database for which ODBC driver is installed, can be accessed.

Page 11: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

11 IBM

Disadvantage of Type-I Driver• Performance overhead since the calls have to

go through the JDBC overhead bridge to the ODBC driver, then to the native db connectivity interface.

• The ODBC driver needs to be installed on the client machine.

• Not good for Web

Page 12: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

12 IBM

Type II Drivers• Native API drivers

• Better performance than Type 1 since no jdbc to odbc translation is needed.

• Converts JDBC calls into calls to the client API for that database.

Page 13: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

13 IBM

Disadvantage of Type-II Driver• The vendor client library needs to be installed

on the client machine. • Cannot be used in internet due the client side

software needed. • The driver is compiled for use with the

particular operating system. • Not good for Web

Page 14: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

14 IBM

Type III Drivers• Follows a three tier

communication approach.

• Calls middleware server, usually on database host

• Very flexible -- allows access to multiple databases using one driver

• Only need to download one driver

Page 15: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

15 IBM

Disadvantage of Type-III Driver• Requires database-specific coding to be done

in the middle tier.

• An extra layer added may result in a time-bottleneck.

Page 16: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

16 IBM

Type IV Drivers• 100% Pure Java --

Communicate directly with a vendor’s database through socket connection

• Use Java networking libraries to talk directly to database engines

Page 17: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

17 IBM

Disadvantage of Type-IV Driver• At client side, a separate driver is needed for

each database

Page 18: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

18 IBM

JDBC Process

1. Loading JDBC Driver2. Connect to Dbms3. Create Statement object4. Query execution5. Process the result6. Connection termination

java.sql package required

Page 19: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

19 IBM

Loading Driver• User must create database and added under

DSN in control panel

• JDBC driver must be loaded or registered before connecting to dbms

• Class. forName (“sun.jdbc.odbc.JdbcOdbcDriver”)

Page 20: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

20 IBM

Connecting to database• DriverManager class is use to make connection

to database through DSN

• Result is stored in object of Connection class

• DriverManager.getConnection(“ jdbc: odbc:DSN name”)

• DriverManager.getConnection (“jdbc:odbc:DSN name”, String username, String password)

Page 21: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

21 IBM

Create statement object• Statements are used to execute sql queries

• Three types of statement: Statement PreparedStatement CollableStatement • Connection class object use to create

staement• Statement objectname = connection class

object.createStataemnt()

Page 22: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

22 IBM

Query execution• SQL queries are executed to perform

operations on database like insert, select etc.

• It returns ResultSet class object through which database is accessed.

Page 23: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

23 IBM

Process the result• Different methods are use to accessed rows and

columns of table.

• getX() method is use to obtain value of column of perticular row.

• getX( column index/name)

• X may be Int, Float, Boolean, String etc.• e.g. getInt(2)

Page 24: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

24 IBM

Connection termination

• Connection are closed using close method.

Page 25: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

25 IBM

Methods of Statement class

• ExecuteQuery()- It used to obtain information form database. Select query is used for this method.

Returntype is Object of resultset class

e.g. executeQuery(“select * from tablename”)

Page 26: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

26 IBM

Methods of Statement class

• ExecuteUpdate()- It used to perfrom operation on database. Insert, update, delete query is used for this method.

Returntype is integer which represent total number of rows affected

e.g. executeUpdate(“insert into tablename values”)

Page 27: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

27 IBM

Methods of Statement class

• Execute()- It used to perform any type operation on database.

Return type is BooleanIf value is true then result stored in resultset object and

if value is false then result stored in integer.

If true execute getResultSet() and false execute getUpdateCount() method

e.g. execute(“insert into tablename values”)

Page 28: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

28 IBM

ResultSet

• This class provides method to access data generated by query in table

• setX is main method

• getRow()method use to count total rows

Page 29: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

29 IBM

ResultSet• Scrollable resultset will move backward or forward

• first()• last()• next()• absolute (int position) • relative(+- int position) Returntype of all methods are boolean

To use methods other than next following constants are used ResultSet.TYPE-Scroll_SENSITIVE ResultSet.CONCUR_UPDATABLE

Page 30: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

30 IBM

PreparedStatement• Query precompiled• ? Given on the place of value• PreparedStatement

objname=connectionobjname.prepareStatement(String query)

• Value of ? Set using setX method• setX(? Position, Value)• e.g. setInt(1,10)

Page 31: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

31 IBM

CallableStatement• Call stored procedures stored in jdbc

application program• It use three parameter:IN- Input data to procedureOUT- Data return by procedureINOUT- Input as well as output data

Page 32: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

32 IBM

MetaData• Infromation describe Database or

Resultset • Types: DatabaseMetaDataDatabaseMetaData objname=

Connectionobjname.getMetaData()ResultSetMetaDataResultSetMetaData objname=

ResultSetobjname.getMetaData()

Page 33: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

33 IBM

DatabaseMetaData methods

• getDatabaseProductVersion()• getURL()• getDatabaseProductName()• getDriverName()• getUserName()

Page 34: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

34IBM

ResultSetMetaData methods

• getColumnCount()• getColumnName(int position)• getColumnType(int position) rt: int• getTableName(int columnposition)• getColumnDisplaySize(int columnposition)• getColumnTypeName(int position)

Page 35: Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming

35IBM

Exception

• SQL Exception: if query wrong then exception generated

• SQL Warning: Provide information about database access warning

• If data lost due to truncation then TruncationException is thrown