Advertisement

CRUD in JSP with example

CRUD in JSP with example
CRUD in JSP with example

CRUD in JSP with example

CRUD in JSP with example – CRUD stands for Create, Read, Update, and Delete, which are the basic operations for working with a persistent storage system like a database. JSP (JavaServer Pages) is a technology that allows you to create dynamic web pages using Java code embedded in HTML. Here is an example of a simple CRUD application using JSP:

    1. Create (Insert) Operation:

To create a new record in the database, you need a form to accept user input. Here is an example of a JSP form for adding a new user:

<%@ page language=”java” contentType=”text/html; charset=UTF-8″
pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8″>
<title>Add User</title>
</head>
<body>
<h1>Add User</h1>
<form action=”createUser.jsp” method=”post”>
Name: <input type=”text” name=”name”><br>
Email: <input type=”email” name=”email”><br>
Password: <input type=”password” name=”password”><br>
<input type=”submit” value=”Add”>
</form>
</body>
</html>

 

When the user submits the form, the data is sent to a JSP file called createUser.jsp, which will handle the insertion of the new record into the database. Here is an example of the code for createUser.jsp:

<%@ page language=”java” contentType=”text/html; charset=UTF-8″
pageEncoding=”UTF-8″%>
<%@ page import=”java.sql.*” %>
<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8″>
<title>Create User</title>
</head>
<body>
<%
String name = request.getParameter(“name”);
String email = request.getParameter(“email”);
String password = request.getParameter(“password”);

// validate input
if (name == null || name.trim().equals(“”) ||
email == null || email.trim().equals(“”) ||
password == null || password.trim().equals(“”)) {
out.println(“All fields are required”);
} else {
try {
// connect to database
Class.forName(“com.mysql.jdbc.Driver”);
Connection con = DriverManager.getConnection(“jdbc:mysql://localhost:3306/mydb”, “root”, “password”);

// insert record
PreparedStatement ps = con.prepareStatement(“INSERT INTO users (name, email, password) VALUES (?, ?, ?)”);
ps.setString(1, name);
ps.setString(2, email);
ps.setString(3, password);
ps.executeUpdate();

// redirect to list page
response.sendRedirect(“listUsers.jsp”);
} catch (Exception e) {
out.println(“Error: ” + e.getMessage());
}
}
%>
</body>
</html>

 

This code retrieves the user input from the request object, validates it, connects to the database, inserts the new record, and then redirects the user to a page that displays the list of all users.

    1. Read (Select) Operation:

To retrieve records from the database, you need a JSP page that displays the data in a table. Here is an example of a JSP page for listing all users:

<%@ page language=”java” contentType=”text/html; charset=UTF-8″
pageEncoding=”UTF-8″%>
<%@ page import=”java.sql.*” %>
<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8″>
<title>List Users</title>
</head>
<body>
<h1>List Users</h1>
<table border=”1″>
<tr>
<th>ID</

 

Download Any Jobs Application Form For 500 +
Download Jobs Application Form For Every Jobs
Resume Templates Top 20 in MS Word – CV Format

 

Disclaimer Confirm everything before applying for a job or giving an advance to a similar officer. We are not responsible for any damage or loss.

About Ghulam Murtaza

Ghulam Murtaza from okara Punjab Pakistan. I have Book Depot & Photo State Shop and provide today's latest career opportunities in private and Govt departments. View all Government jobs collected from daily Pakistani newspapers to apply online. Murtazaweb.com is a recruitment website for employment ads.

View all posts by Ghulam Murtaza →

Leave a Reply

Your email address will not be published. Required fields are marked *