0% found this document useful (0 votes)
9 views2 pages

Updel

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

Updel

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

delete.

jsp

<%@ page import="java.sql.*, java.util.*" %>


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%
String id = request.getParameter("id");

String url = "jdbc:mysql://localhost:3306/BCA";


String username = "root";
String password = "root";
Connection conn = null;
PreparedStatement stmt = null;

try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);

String deleteQuery = "DELETE FROM students WHERE id = ?";


stmt = conn.prepareStatement(deleteQuery);
stmt.setString(1, id);

int rowsDeleted = stmt.executeUpdate();

if (rowsDeleted > 0) {
out.println("Record deleted successfully!");
response.sendRedirect("retrieve.jsp"); // Redirect back to the list
page
}
} catch (Exception e) {
out.println("Error: " + e.getMessage());
} finally {
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
%>

update.jsp

<%@ page import="java.sql.*, java.util.*" %>


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%
String id = request.getParameter("id");
String firstName = request.getParameter("first_name");
String lastName = request.getParameter("last_name");
String city = request.getParameter("city_name");
String email = request.getParameter("email");

String url = "jdbc:mysql://localhost:3306/BCA";


String username = "root";
String password = "root";
Connection conn = null;
PreparedStatement stmt = null;

try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);

String updateQuery = "UPDATE students SET first_name = ?, last_name = ?,


city_name = ?, email = ? WHERE id = ?";
stmt = conn.prepareStatement(updateQuery);
stmt.setString(1, firstName);
stmt.setString(2, lastName);
stmt.setString(3, city);
stmt.setString(4, email);
stmt.setString(5, id);

int rowsUpdated = stmt.executeUpdate();

if (rowsUpdated > 0) {
out.println("Record updated successfully!");
response.sendRedirect("retrieve.jsp"); // Redirect back to the list
page
}
} catch (Exception e) {
out.println("Error: " + e.getMessage());
} finally {
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
%>

You might also like