0% found this document useful (0 votes)
4 views10 pages

2CS1010601-AdvJava_UNIT-5-J2EE_Database

The document provides an overview of the J2EE platform, detailing its architecture which includes the Client Tier, Middle Tier, and Enterprise Information Tier. It also explains the role of containers in managing application components, and compares ODBC and JDBC drivers for database connectivity. Additionally, it outlines the steps for accessing databases using JDBC and describes the different types of JDBC statements: Statement, PreparedStatement, and CallableStatement.

Uploaded by

naumanac1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

2CS1010601-AdvJava_UNIT-5-J2EE_Database

The document provides an overview of the J2EE platform, detailing its architecture which includes the Client Tier, Middle Tier, and Enterprise Information Tier. It also explains the role of containers in managing application components, and compares ODBC and JDBC drivers for database connectivity. Additionally, it outlines the steps for accessing databases using JDBC and describes the different types of JDBC statements: Statement, PreparedStatement, and CallableStatement.

Uploaded by

naumanac1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Shri C. J.

Patel College of Computer Studies, Visnagar


2CS1010601 : WEB APPLICATION DEVELOPMENT USING ADVANCE JAVA
[ UNIT – V : J2EE Platform & Database Programming ]

Introduction to
J2EE Platform
and Architecture
✓ The Java 2 Platform, Enterprise Edition (J2EE) provides a standard for
developing multitier, enterprise applications.
✓ J2EE Architecture is made up of three tiers :
1. Client Tier
2. Middle tier (Web Tier + Enterprise Java Bean (EJB) Tier)
3. Enterprise Information Tier

1. Client Tier :
• It consists of programs or applications that interact with the user for
requests and responses.
• Usually, they are located on a different machine from the server.
• A client can be a web browser, standalone application, or server on a
different machine.
• Clients can be classified as Web Clients and Application Clients.
• Web components, such as Servlets and Java Server Pages (JSPs), or
standalone Java applications provide a dynamic interface to the middle
tier.

[ Page 1 of 10 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : WEB APPLICATION DEVELOPMENT USING ADVANCE JAVA
[ UNIT – V : J2EE Platform & Database Programming ]

2. Middle Tier :
• It is used for defining logical functioning units & consists of web
components and EJB components.
• It usually contains enterprise beans and web services that distribute
business logic for the applications.

3. The Enterprise Information Tier :


• That is used for storage purposes in the form of relational database &
consists of containers, components, and services.

Container Types

✓ Containers are the interface between a component and the low-level, platform-
specific functionality that supports the component.
✓ Before it can be executed, a web, enterprise bean, or application client
component must be assembled into a Java EE module and deployed into its
container.
✓ The deployment process installs Java EE application components in the Java EE
containers.
✓ The container also manages non configurable services, such as enterprise bean
and servlet lifecycles, database connection resource pooling, data persistence,
and access to the Java EE platform APIs

[ Page 2 of 10 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : WEB APPLICATION DEVELOPMENT USING ADVANCE JAVA
[ UNIT – V : J2EE Platform & Database Programming ]

1. Java EE server:
▪ The runtime portion of a Java EE product.
▪ A Java EE server provides EJB and web containers.

2. Enterprise JavaBeans (EJB) container:


▪ It manages the execution of enterprise beans for Java EE
applications.
▪ Enterprise beans and their container run on the Java EE server.
▪ An EJB container is a server platform used for controlling the
execution of Enterprise Bean, and provides various services for
Enterprise Bean.
▪ The EJB container provides APIs for EJB.

3. Web container:
▪ It is a server platform used for executing servlets and JSPs.
▪ It receives access from the Web client, and provides services
according to a request.

4. Application client container:


▪ It manages the execution of application client components.
▪ Application clients and their container run on the client.

5. Applet container:
▪ It manages the execution of applets.
▪ It consists of a web browser and Java Plug-in running on the client
together.

[ Page 3 of 10 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : WEB APPLICATION DEVELOPMENT USING ADVANCE JAVA
[ UNIT – V : J2EE Platform & Database Programming ]

ODBC and JDBC Drivers


✓ Open Database Connectivity (ODBC) and Java Database Connectivity (JDBC)
are standards for drivers that enable programmers to get connected with a
database.
✓ Both ODBC and JDBC drivers act as intermediaries between applications and
databases, translating requests into a format that the database understands.

Sr. Key ODBC JDBC


No.

1 Stands For ODBC stands for Open JDBC Stands for Java
Database Connectivity, it is Database Connectivity
compatible with all types of i.e only compatible with
languages such as C, C++, Java, java language.
etc.

2 Introduction ODBC was introduced by JDBC was introduced by


Microsoft prior to JDBC in SUN Micro Systems
1992. after ODBC in 1997.

3 Platform ODBC is platform dependent JDBC is platform-


dependency as we can use ODBC only for independent and can be
windows platform. used for any platform.

4 Type ODBC can be considered as a JDBC is a purely object-


type of procedural as most of oriented type driver.
these drivers are developed in
native languages like C and
C++ which are the procedural
types of language.

5 Performance The performance of ODBC is The performance of


faster as compared to JDBC JDBC is slower than
as data imports and exports ODBC but its platform
are faster and memory- independence allowing
intensive. to work with any
operating system
(including Mac and
Linux), driver version, or
bitness (32-bit or 64-bit).

[ Page 4 of 10 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : WEB APPLICATION DEVELOPMENT USING ADVANCE JAVA
[ UNIT – V : J2EE Platform & Database Programming ]

Accessing Databases
with JDBC
• Steps to Connect Java Application with Database in Java:
1. Import the Packages.
2. Load/Register the drivers using the forName() method
3. Establish a connection using the Connection class object
4. Create a statement
5. Execute the query
6. Retrieve result
7. Close the connections

1. Import the packages:


• import java.sql.*;
• This includes uploading all the packages containing the JDBC classes,
interfaces, and subclasses used during the database programming.

[ Page 5 of 10 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : WEB APPLICATION DEVELOPMENT USING ADVANCE JAVA
[ UNIT – V : J2EE Platform & Database Programming ]

2. Load/ register the drivers using the forName() method


• Before connecting to the database, we’ll need to load/register the drivers
once per database.
• This is done to create a communication channel with the database. Loading a
driver can be done in two ways:
a. Class.forName()
b. DriverManager.registerDriver()

3. Establish a connection using the Connection class object


• After loading the driver, establish connections using getConnection():
Connection con = DriverManager.getConnection(url, userName, password);
▪ getConnection(URL, username, password): This uses three
parameters URL, a password, and a username
▪ URL: This has only one parameter - URL. The URL has both a username
and password. There are several JDBC connection strings for different
relational databases and some are listed below:
a. IBM DB2 database:
jdbc:db2://HOSTNAME:PORT/DATABASE_NAME
b. Oracle database:
jdbc:oracle:thin:@HOST_NAME:PORT:SERVICE_NAME
c. My SQL database:
jdbc:mysql://HOST_NAME:PORT/DATABASE_NAME

4. Create a statement:
• Once a connection is established you can interact with the database.
• The JDBC Statement, CallableStatement, and PreparedStatement
interfaces define the methods that enable you to send SQL commands and
receive data from your database.
• Use of JDBC Statement is as follows:
Statement st = con.createStatement( );
▪ Statement: is used for executing a static SQL statement with no
parameter. An example is:
Statement st = con.createStatement();
This statement returns the ResultSet object.
▪ PreparedStatement: It is used for executing a precompiled SQL
statement that have parameters. It improves the application's
performance because it has more features and compiles the query only
[ Page 6 of 10 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : WEB APPLICATION DEVELOPMENT USING ADVANCE JAVA
[ UNIT – V : J2EE Platform & Database Programming ]

once. PreparedStatement is faster than Statement. It becomes more


visible when we reuse the PreparedStatement for executing multiple
queries.
▪ CallableStatement: It is an interface which is used to execute SQL
stored procedures, cursors, and functions. It is simply created by
calling the prepare all method of the connection object.

5. Execute the query:


• The most important part is executing the query.
• The query here is an SQL Query. We can have multiple types of queries:
▪ The query for updating/inserting a table in a database.
▪ The query for retrieving data.
• This uses a type statement object to build and submit SQL statements to a
database. It has following methods:
▪ executeQuery(String sql) : to execute queries of retrieving values
from the database
▪ executeUpdate(String sql) : to execute queries of updating/inserting.

6. Retrieve result:
• When queries are executed using the executeQuery() method, it produces
results stored in the ResultSet object.
• The ResultSet object is then used to access the retrieved data from the
database.

7. Close the connection:


• The JDBC connection can now be closed after all is done.
• By closing the connection, objects of Statement and ResultSet will be closed
automatically.
• The close() method of the Connection interface is used to close the
connection.
con.close();

[ Page 7 of 10 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : WEB APPLICATION DEVELOPMENT USING ADVANCE JAVA
[ UNIT – V : J2EE Platform & Database Programming ]

JDBC Statement Types


✓ In JDBC (Java Database Connectivity) there are 3-types of statements that
we can use to interact with the DBMS (Database Management System).

1. Statement
2. PreparedStatement
3. CallableStatement

1. Statement
• Statement interface is used to execute normal SQL queries.
• It can be used to execute a static SQL query.
• You can’t pass the parameters to SQL query at run time.
• It is when we know that we will not need to execute the SQL query multiple
times.
• This interface is preferred over other two interfaces if you are executing a
particular SQL query only once.
• The performance of this interface is also very less compared to other two
interfaces.
• It is suitable for the execution of the DDL (Data Definition Language)
statements, such as CREATE, ALTER, DROP.
• For example:
Statement stmt = con.createStatement();
stmt.executeUpdate("CREATE TABLE STUDENT(ID NUMBER NOT
NULL, NAME VARCHAR)");

[ Page 8 of 10 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : WEB APPLICATION DEVELOPMENT USING ADVANCE JAVA
[ UNIT – V : J2EE Platform & Database Programming ]

2. PreparedStatment
• It extends the Statement interface.
• It is precompiled SQL statement that can be executed multiple times with
different parameters.
• It is used to execute dynamic or parameterized SQL queries.
• You can pass the parameters to SQL query at run time using this interface.
• It gives better performance than Statement interface.
• Because, PreparedStatement are precompiled and the query plan is created
only once irrespective of how many times you are executing that query. This
will save lots of time.
• For example:
PreparedStatement pstmt = con.prepareStatement("update STUDENT
set NAME = ? where ID = ?");
pstmt.setString(1, "MyName"); //Assigns "MyName" to first place
pstmt.setInt(2, 111); //Assigns "111" to second place holder
pstmt.executeUpdate();

3. CallableStatement
• CallableStatement extends PreparedStatement interface.
• CallableStatement is used to execute the stored procedures.
• Usng CallableStatement, you can pass 3 types of parameters (IN, OUT and IN
OUT) to stored procedures.
• IN – used to pass the values to stored procedure.
• OUT – used to hold the result returned by the stored procedure.
• IN OUT – acts as both IN and OUT parameter.
• Before calling the stored procedure, you must register OUT parameters
using registerOutParameter() method of CallableStatement.
• The performance of this interface is higher than the other two interfaces.
Because, it calls the stored procedures which are already compiled and stored
in the database server.
• For example:
CREATE PROCEDURE MULTIPLY(OUT RESULT INT, IN a INT, IN b INT) SET
RESULT = a * b;
CallableStatement cstmt = con.prepareCall("{call MULTIPLY(?, ?, ?)}");
cstmt.registerOutParameter(1, Types.INTEGER); //to pass IN parameters
cstmt.setInt(2, 4);
cstmt.setInt(3, 8);

[ Page 9 of 10 ]
Shri C. J. Patel College of Computer Studies, Visnagar
2CS1010601 : WEB APPLICATION DEVELOPMENT USING ADVANCE JAVA
[ UNIT – V : J2EE Platform & Database Programming ]

cstmt.execute();
int result = cstmt.getInt(1);
• In general, Statement is simple and basic but not very efficient,
PreparedStatement is optimized for performance and supports
parameterization, and CallableStatement is used to call stored procedures.

➢ Statement vs. PreparedStatement vs. CallableStatement :

Statement Prepared Statement Callable Statement


It is used to execute It is used to execute It is used to call the stored
normal SQL queries. parameterized or procedures.
dynamic SQL queries.
It is preferred when a It is preferred when a It is preferred when the
particular SQL query particular query is to be stored procedures are to be
is to be executed only executed multiple times. executed.
once.
You cannot pass the You can pass the You can pass 3 types of
parameters to SQL parameters to SQL query parameters using this
query using this at run time using this interface. They are – IN, OUT
interface. interface. and IN OUT.
This interface is It is used for any It is used to execute stored
mainly used for DDL kind of SQL procedures and functions.
statements like queries which are to be
CREATE, ALTER, executed multiple times.
DROP etc.
The performance of The performance of this The performance of this
this interface is very interface is better than interface is high
low. the Statement interface
(when used for multiple
execution of same
query).

[ Page 10 of 10 ]

You might also like