Professional Documents
Culture Documents
Unit-4 Imp WT
Unit-4 Imp WT
Unit-4 Imp WT
UNIT -4
Q1. Illustrate Java Bean. Explain characteristics of java Bean. Give example.
Ans:
JavaBean :
JavaBeans are used to encapsulate many objects into a single object (the bean), so
they can be passed around as a single unit.
Characteristics of JavaBean
Properties:
Getters and setters follow a naming convention: for a property named property, the
getter method would be getProperty() and the setter method would be setProperty(Type
property).
No-Argument Constructor:
Serializable:
Event Handling:
Example of a JavaBean
import java.io.Serializable;
// No-argument constructor
public Person() {
return name;
this.name = name;
return age;
this.age = age;
Q2. Define JDBC. Explain the Drivers used in JDBC. Create a JDBC Program for
Update and display the record of employees using prepared statement.
Ans:
Definition:
JDBC allows Java applications to interact with databases to retrieve, insert, update, and
delete data.
JDBC Drivers
JDBC drivers are implementations that handle the communications between the Java
applications and the database.
Example: sun.jdbc.odbc.JdbcOdbcDriver
This driver converts JDBC calls into native calls of the database API.
Example: oracle.jdbc.driver.OracleDriver
Example: com.internetcds.jdbc.tds.Driver
This driver converts JDBC calls directly into the database-specific protocol.
Example: com.mysql.cj.jdbc.Driver
JDBC Program to Update and Display Employee Records using Prepared Statement
Below is an example of a JDBC program that updates and displays employee records
using PreparedStatement.
Prerequisites:
Ensure you have the JDBC driver for your database (e.g., MySQL JDBC driver).Set up your
database and table.
name VARCHAR(100),
position VARCHAR(100),
salary DECIMAL(10, 2)
);
Java Code:
import java.sql.*;
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
pstmt = conn.prepareStatement(updateSQL);
pstmt = conn.prepareStatement(selectSQL);
rs = pstmt.executeQuery();
// Step 5: Extract data from result set
while (rs.next()) {
int id = rs.getInt("id");
// Display values
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
se.printStackTrace();
Q3. What do you mean by Session Bean? Explain its types using suitable example.
Ans:
In the context of Enterprise JavaBeans (EJB), a Session Bean is a type of EJB that
represents a single client inside the EJB container.
Session Beans are used to implement business logic in a Java EE application and are
managed by the EJB container, providing services such as transaction management,
security, concurrency, and more.
A Stateless Session Bean (SLSB) does not maintain conversational state with the
client across method invocations.
It is typically used for implementing stateless services or business logic that doesn't
require maintaining client-specific data between method calls.
import javax.ejb.Stateless;
@Stateless
return a - b;
A Stateful Session Bean (SFSB) maintains conversational state with the client across
multiple method invocations.
This means that the bean instance preserves the data related to a specific client
session, allowing the client to maintain a stateful interaction with the bean.
SFSBs are suitable for scenarios where the client needs to maintain a
conversational state, such as shopping cart in an e-commerce application.
import javax.ejb.Stateful;
@Stateful
items.add(item);
items.clear();
State Management:
Concurrency:
SFSB: Each instance is dedicated to a single client session, managing state exclusively
for that client.
Lifecycle:
SFSB: Created when a client requests it and destroyed when the client session ends or
explicitly removed.