2CS1010601-AdvJava_UNIT-5-J2EE_Database
2CS1010601-AdvJava_UNIT-5-J2EE_Database
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.
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.
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.
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 ]
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.
[ 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
[ 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 ]
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 ]
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.
[ 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 ]
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.
[ Page 10 of 10 ]