You are on page 1of 19

PARUL INSTITUTE OF ENGINEERING AND TECH

5th SEMSTER CSE-BDA


ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

FACULTY OF ENGINEERING AND TECHNOLOGY


BACHELOR OF TECHNOLOGY

ENTERPRISE PROGRAMMING LABORATORY


(203105357)

SEMISTER 5
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

LABORATORY MANUAL

1
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

CERTIFICATE

This is to certify that Mr. KANKATALA VENU GOPAL KARTHIK

with enrolment no. 210303125045 has successfully completed his

laboratory in the ENTERPRISE PROGRAMMING (203105373)

from the department of COMPUTER SCIENCE & ENGINEERING

during the academic year 2023-2024

Date of Submission: ......................... Staff In charge: ...........................

Head Of Department: .........................................

2
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

INDEX

3
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

EXPERIMENT-1

AIM : Database Connectivity with JDBC


Write a program to retrieve the data and managing DML operation using JDBC.

a basic Java program that demonstrates how to use JDBC (Java Database Connectivity) to
retrieve data from a database and perform DML (Data Manipulation Language) operations
like inserting, updating, and deleting data. Please note that you'll need to have a database set
up and the appropriate JDBC driver for your database type.
In this example, I'll demonstrate using MySQL as the database. Make sure to replace the
placeholders (<placeholders>) with your actual database connection details.

import java.sql.*;
public class JDBCDemo {
// JDBC database URL, username, and password
static final String DB_URL = "jdbc:mysql://localhost:3306/your_database";
static final String USER = "your_username";
static final String PASS = "your_password";

public static void main(String[] args) {


Connection conn = null;
Statement stmt = null;
try {
// Register JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Create a statement
stmt = conn.createStatement();
// Retrieving data
String selectQuery = "SELECT id, name, age FROM your_table";
ResultSet resultSet = stmt.executeQuery(selectQuery);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
}
// Performing DML operations
String insertQuery = "INSERT INTO your_table (name, age) VALUES ('New Person',
25)";
int rowsInserted = stmt.executeUpdate(insertQuery);
System.out.println(rowsInserted + " rows inserted.");

4
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

String updateQuery = "UPDATE your_table SET age = 30 WHERE name = 'New


Person'";
int rowsUpdated = stmt.executeUpdate(updateQuery);
System.out.println(rowsUpdated + " rows updated.");
String deleteQuery = "DELETE FROM your_table WHERE age = 30";
int rowsDeleted = stmt.executeUpdate(deleteQuery);
System.out.println(rowsDeleted + " rows deleted.");
// Clean-up
resultSet.close();
stmt.close();
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
} catch (SQLException se) {
se.printStackTrace();
}
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
}

Remember to replace placeholders like your_database, your_username, your_password, and


adjust the SQL queries according to your database structure. Also, make sure you have the
appropriate JDBC driver library in your project's classpath. This example provides a basic
framework; in real-world applications, you might want to use connection pooling and
consider error handling more robustly.

5
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

EXPERIMENT-2

AIM : Introduction to Enterprise Programming Setting up the development environment


(JDK,Eclipse/IntelliJ….etc.,).

Setting up the development environment for enterprise programming involves configuring


essential tools like the Java Development Kit (JDK) and Integrated Development
Environments (IDEs) such as Eclipse or IntelliJ IDEA. This process ensures that you have the
necessary tools to efficiently create, debug, and manage large-scale enterprise applications.
Here's a detailed guide to setting up the development environment:

Java Development Kit (JDK) Installation:


The JDK is the core software package required for Java development. It includes the Java
Runtime Environment (JRE), compilers, and tools needed to develop Java applications.
Follow these steps to install the JDK:

Visit the official Oracle JDK download page or OpenJDK website.


Download the JDK installer suitable for your operating system (Windows, macOS, Linux).
Run the installer and follow the installation prompts to complete the setup.
Integrated Development Environment (IDE) Installation:
IDEs provide a comprehensive development environment with features like code editing,
debugging, version control, and more. Eclipse and IntelliJ IDEA are popular choices:

a. Eclipse:

Visit the Eclipse Downloads page (https://www.eclipse.org/downloads/).


Download the Eclipse IDE for Java Developers package.
Extract the downloaded archive to a directory of your choice.
Run the Eclipse executable (eclipse.exe on Windows) to start the IDE.
b. IntelliJ IDEA:

Visit the IntelliJ IDEA Download page (https://www.jetbrains.com/idea/download/).


Download the Community (free) or Ultimate (paid) edition based on your preference.
Run the installer and follow the installation prompts.
Configuring the IDE:

a. Eclipse:

When you first launch Eclipse, you'll be asked to select a workspace directory. This is where
your projects will be stored.
You can customize the IDE's appearance, plugins, and settings through the Preferences menu.
b. IntelliJ IDEA:

On launching IntelliJ IDEA, you'll need to set up your preferences and keymap.

6
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

Create or import a project and define its settings like SDK (JDK), dependencies, and version
control.
Project Setup:
Create a new project or import an existing one:

a. Creating a New Project:

In Eclipse, use the File > New > Java Project option.
In IntelliJ IDEA, select File > New > Project and choose a Java project template.
b. Importing an Existing Project:

In Eclipse, use the File > Import option and select General > Existing Projects into
Workspace.
In IntelliJ IDEA, select File > New > Project from Existing Sources and navigate to your
project's root directory.
Adding External Libraries:
Enterprise applications often rely on external libraries and frameworks. You can add these
dependencies to your project:

In Eclipse, right-click your project, choose Build Path > Configure Build Path, and add
external JARs or libraries.
In IntelliJ IDEA, open your project's settings (File > Project Structure), navigate to Libraries,
and add the necessary JARs or dependencies.
Version Control Integration:
It's essential to use version control systems like Git to manage your codebase:

Both Eclipse and IntelliJ IDEA have built-in Git integration. You can clone repositories,
commit changes, and manage branches directly from the IDE.
Coding and Development:
With your IDE and project set up, you can start coding enterprise applications. Leverage the
IDE's features for code completion, debugging, testing, and more.

Building and Running:


You can build and run your applications within the IDE:

Eclipse and IntelliJ IDEA provide buttons to build and run your code. You can also configure
run configurations to specify how your application should run.
Deployment and Testing:
Depending on your enterprise application, you might need to deploy it to a server or test
various deployment scenarios.

Continuous Integration (CI) and Deployment:


For enterprise projects, setting up CI/CD pipelines using tools like Jenkins, Travis CI, or
GitHub Actions can automate build, test, and deployment processes.

7
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

In summary, setting up the development environment for enterprise programming involves


installing the JDK, choosing an IDE (Eclipse or IntelliJ IDEA), configuring the IDE, setting
up projects, managing dependencies, integrating version control, coding, building, running,
testing, and potentially deploying through CI/CD pipelines. This environment provides the
foundation for efficient and effective enterprise software development.

8
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

EXPERIMENT-3

AIM : Building a web application with java EE create a registration form and showing data
In another servlet

Building a web application using Java EE involves creating servlets, JSP (JavaServer Pages),
and using the features of the Java EE platform. In this example, we'll create a simple
registration form using servlets and then display the registered data in another servlet.

1. Set Up the Project:

Create a new dynamic web project in your IDE (Eclipse, IntelliJ, etc.).
Make sure you have the Java EE libraries (servlet-api.jar) in your project's classpath.
2. Create Registration Form (index.jsp):

Create a JSP file named index.jsp in the WebContent directory. This will be the registration
form.

<!DOCTYPE html>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<h2>Registration Form</h2>
<form action="RegisterServlet" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
<input type="submit" value="Register">
</form>
</body>
</html>
3. Create Registration Servlet (RegisterServlet.java):

Create a servlet named RegisterServlet that handles the registration process. It will save the
registered data in a list (for simplicity).

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

9
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

import javax.servlet.http.HttpServletResponse;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private List<User> userList = new ArrayList<>();

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
String name = request.getParameter("name");
String email = request.getParameter("email");

User user = new User(name, email);


userList.add(user);

response.sendRedirect("index.jsp");
}
}
4. Create User Class (User.java):

Create a simple Java class to represent a user.

public class User {


private String name;
private String email;

public User(String name, String email) {


this.name = name;
this.email = email;
}

// Getters and setters


}
5. Create Display Servlet (DisplayServlet.java):

Create a servlet named DisplayServlet that displays the registered data.

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

10
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

@WebServlet("/DisplayServlet")
public class DisplayServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html><body>");
out.println("<h2>Registered Users</h2>");
for (User user : RegisterServlet.userList) {
out.println("Name: " + user.getName() + "<br>");
out.println("Email: " + user.getEmail() + "<br><br>");
}
out.println("</body></html>");
}
}
6. Create Web Deployment Descriptor (web.xml):

In the WEB-INF directory, create a web.xml file to map servlets to their URLs.

<web-app>
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>RegisterServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>DisplayServlet</servlet-name>
<servlet-class>DisplayServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/RegisterServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DisplayServlet</servlet-name>
<url-pattern>/DisplayServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
7. Run the Application:

11
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

Run your application on a suitable server (like Apache Tomcat).


Access the registration form at http://localhost:8080/your-web-app-name/.
After submitting the form, you can view the registered data by accessing
http://localhost:8080/your-web-app-name/DisplayServlet.
Please replace your-web-app-name with the actual name of your deployed web application.
This example demonstrates a basic registration form and data display using Java EE
technologies. In real-world scenarios, you would typically use a database to store and retrieve
data instead of using in-memory lists.

12
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

EXPERIMENT-4

AIM : Introduction to Design Patterns


Implementing a simple example using a factory design pattern

Design patterns are reusable solutions to common problems that occur in software design and
development. They provide a structured way to solve recurring design problems while
promoting flexibility, maintainability, and scalability. One of the widely used design patterns
is the Factory Design Pattern, which falls under the creational category and is used for object
creation.

Factory Design Pattern:

The Factory Design Pattern is used to create objects without specifying the exact class of
object that will be created. It defines an interface (or an abstract class) for creating objects,
and concrete implementations of this interface are responsible for instantiating specific types
of objects.

Example: Creating Shapes using Factory Design Pattern:

In this example, we'll create a simple implementation of the Factory Design Pattern to create
different shapes.

Create Shape Interface:

public interface Shape {


void draw();
}
Create Concrete Shape Classes:

public class Circle implements Shape {


public void draw() {
System.out.println("Drawing a Circle");
}
}

public class Square implements Shape {


public void draw() {
System.out.println("Drawing a Square");
}
}

public class Rectangle implements Shape {


public void draw() {
System.out.println("Drawing a Rectangle");

13
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

}
Create Shape Factory:

public class ShapeFactory {


public Shape createShape(String type) {
if (type.equalsIgnoreCase("circle")) {
return new Circle();
} else if (type.equalsIgnoreCase("square")) {
return new Square();
} else if (type.equalsIgnoreCase("rectangle")) {
return new Rectangle();
} else {
throw new IllegalArgumentException("Invalid shape type: " + type);
}
}
}
Using the Factory to Create Shapes:

public class Main {


public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();

Shape circle = shapeFactory.createShape("circle");


circle.draw();

Shape square = shapeFactory.createShape("square");


square.draw();

Shape rectangle = shapeFactory.createShape("rectangle");


rectangle.draw();
}
}
In this example, the Factory Design Pattern helps decouple the object creation process from
the client code. The ShapeFactory class encapsulates the creation logic, and the client code
can request shapes without needing to know the details of how the shapes are created.

Keep in mind that this is a simple example, and in real-world scenarios, factories might
involve more complex logic and considerations, such as managing object lifecycle,
dependencies, and configurations.

Design patterns like the Factory Design Pattern are powerful tools to enhance code
modularity, reusability, and maintainability by providing standardized solutions to common
design challenges.

14
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

EXPERIMENT-5
AIM : Unit testing in Java Enterprise Applications
Writing unit tests for java classes and components

Design patterns are reusable solutions to common problems that occur in software design and
development. They provide a structured way to solve recurring design problems while
promoting flexibility, maintainability, and scalability. One of the widely used design patterns
is the Factory Design Pattern, which falls under the creational category and is used for object
creation.

Factory Design Pattern:

The Factory Design Pattern is used to create objects without specifying the exact class of
object that will be created. It defines an interface (or an abstract class) for creating objects,
and concrete implementations of this interface are responsible for instantiating specific types
of objects.

Example: Creating Shapes using Factory Design Pattern:

In this example, we'll create a simple implementation of the Factory Design Pattern to create
different shapes.

Create Shape Interface:

public interface Shape {


void draw();
}
Create Concrete Shape Classes:
java
Copy code
public class Circle implements Shape {
public void draw() {
System.out.println("Drawing a Circle");
}
}

public class Square implements Shape {


public void draw() {
System.out.println("Drawing a Square");
}
}

public class Rectangle implements Shape {


public void draw() {
System.out.println("Drawing a Rectangle");

15
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

}
}
Create Shape Factory:

public class ShapeFactory {


public Shape createShape(String type) {
if (type.equalsIgnoreCase("circle")) {
return new Circle();
} else if (type.equalsIgnoreCase("square")) {
return new Square();
} else if (type.equalsIgnoreCase("rectangle")) {
return new Rectangle();
} else {
throw new IllegalArgumentException("Invalid shape type: " + type);
}
}
}
Using the Factory to Create Shapes:

public class Main {


public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();

Shape circle = shapeFactory.createShape("circle");


circle.draw();

Shape square = shapeFactory.createShape("square");


square.draw();

Shape rectangle = shapeFactory.createShape("rectangle");


rectangle.draw();
}
}
In this example, the Factory Design Pattern helps decouple the object creation process from
the client code. The ShapeFactory class encapsulates the creation logic, and the client code
can request shapes without needing to know the details of how the shapes are created.

Keep in mind that this is a simple example, and in real-world scenarios, factories might
involve more complex logic and considerations, such as managing object lifecycle,
dependencies, and configurations.

Design patterns like the Factory Design Pattern are powerful tools to enhance code
modularity, reusability, and maintainability by providing standardized solutions to common
design challenges.

16
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

User
Unit testing in Java Enterprise Applications
Writing unit tests for java classes and components

Unit testing is an essential practice in Java Enterprise Applications to ensure the correctness
and reliability of individual components, classes, and methods. Writing unit tests helps catch
bugs early, facilitates code maintenance, and supports refactoring. In this example, we'll
cover writing unit tests using the JUnit framework, which is widely used for Java unit testing.

1. Set Up the Testing Environment:

First, make sure you have JUnit added to your project's dependencies. You can typically add
JUnit via Maven, Gradle, or download the JAR files manually.

2. Write Unit Tests:

Suppose you have a simple class representing a basic mathematical operation. We'll write unit
tests for this class.

MathOperation.java:

public class MathOperation {


public int add(int a, int b) {
return a + b;
}

public int subtract(int a, int b) {


return a - b;
}
}
MathOperationTest.java (JUnit Test Class):

import static org.junit.Assert.assertEquals;


import org.junit.Before;
import org.junit.Test;

public class MathOperationTest {

private MathOperation mathOperation;

@Before
public void setUp() {
mathOperation = new MathOperation();
}

17
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

@Test
public void testAdd() {
int result = mathOperation.add(3, 4);
assertEquals(7, result);
}

@Test
public void testSubtract() {
int result = mathOperation.subtract(7, 3);
assertEquals(4, result);
}
}
In this example, we're using JUnit to write test methods. Let's break down the key
components:

@Before: This annotation indicates that the annotated method should be run before each test
method. It's used to set up any necessary resources before testing.
@Test: This annotation indicates that the annotated method is a test method. JUnit will
execute methods with this annotation to perform tests.
assertEquals: This is an assertion method provided by JUnit. It checks whether the actual
value matches the expected value. If not, the test will fail.
3. Running Unit Tests:

After writing your unit tests, you can run them using a build tool (like Maven or Gradle) or
directly from your IDE.

4. Test Suites and Categories:

JUnit allows you to group related tests into test suites or categories. This is helpful for
organizing and running specific sets of tests.

5. Mocking and Dependency Injection:

In enterprise applications, components often have dependencies. When writing unit tests, you
might use mocking frameworks like Mockito to simulate behavior of dependent objects.
Dependency injection is also a common technique to inject dependencies into your classes
during testing.

6. Continuous Integration:

For robust testing in enterprise applications, consider integrating your unit tests into a
continuous integration (CI) process. This ensures that tests are automatically executed
whenever code changes are made.

7. Testing Other Components:

18
ERP NO: 210303125045
PARUL INSTITUTE OF ENGINEERING AND TECH
5th SEMSTER CSE-BDA
ENTERPRISE PROGRAMMING LABORATORY
SUB CODE: 203105375

In Java Enterprise Applications, you'll have more complex components like EJBs, servlets,
and database interactions. Writing unit tests for these components might involve using
frameworks that provide mocks and stubs for simulating complex interactions.

Remember that unit testing is just one layer of testing. In enterprise applications, integration
testing, functional testing, and performance testing are also crucial to ensure the overall
quality and reliability of your software.

19
ERP NO: 210303125045

You might also like