Upload
miles-haynes
View
213
Download
0
Embed Size (px)
DESCRIPTION
Advantage Simple in Design Easy to maintain and modify Communication is fast Disadvantages: Inflexible Limited Client Less secure Cost ineffective
Citation preview
Ch. No Name Marks
01 AWT 24
02 Networking 18
03 JDBC 20
04 Swing 18
05 Servlet 20
Advance Java Programming
2 IBM
Two-Tier Architecture
• Client connects directly to server
• e.g. HTTP
Advantage• Simple in Design• Easy to maintain and modify• Communication is fast
• Disadvantages:• Inflexible• Limited Client• Less secure• Cost ineffective
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
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
Disadvantages
• High Maintenance• High Complexity• Lower network efficiency
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
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
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
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.
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
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.
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
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
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.
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
17 IBM
Disadvantage of Type-IV Driver• At client side, a separate driver is needed for
each database
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
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”)
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)
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()
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.
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)
24 IBM
Connection termination
• Connection are closed using close method.
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”)
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”)
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”)
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
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
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)
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
32 IBM
MetaData• Infromation describe Database or
Resultset • Types: DatabaseMetaDataDatabaseMetaData objname=
Connectionobjname.getMetaData()ResultSetMetaDataResultSetMetaData objname=
ResultSetobjname.getMetaData()
33 IBM
DatabaseMetaData methods
• getDatabaseProductVersion()• getURL()• getDatabaseProductName()• getDriverName()• getUserName()
34IBM
ResultSetMetaData methods
• getColumnCount()• getColumnName(int position)• getColumnType(int position) rt: int• getTableName(int columnposition)• getColumnDisplaySize(int columnposition)• getColumnTypeName(int position)
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