//Update Table in the database
//index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Updating User Details</title>
<style>
div {
background-color: lightgreen;
color: Red;
padding: 20px;
margin: 20px;
border: 2px yellow;
}
</style>
</head>
<body>
<div>
<h1>Updating User Details</h1>
<form action="UpdateDetails" method="post">
<label>Username:</label>
<input type="text" name="username" required>
<br>
<h3>Give updated details of the above user</h3>
<label>Password:</label>
<input type="password" name="password" required>
<br>
<label>Email:</label>
<input type="email" name="email" required>
<br>
<input type="submit" name="submit" value="Update">
</form>
</div>
</body>
</html>
//UpdateServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/UpdateDetails")
public class UpdateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public UpdateServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection con = null;
PreparedStatement st = null;
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
try {
// Load the Driver Class
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("Driver loaded");
// Create the connection
con = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE", "system", "1234");
System.out.println("Connection established");
String user = request.getParameter("username");
String pwd = request.getParameter("password");
String email = request.getParameter("email");
// Update statement (ensure the table and column names match your Oracle schema)
st = con.prepareStatement("UPDATE studentdata SET password=?, email=? WHERE username=?");
st.setString(1, pwd);
st.setString(2, email);
st.setString(3, user);
int r = st.executeUpdate();
if (r > 0) {
pw.println("<h1>Hi " + user + ", your password and email have been updated successfully.</h1>");
} else {
pw.println("<h1>User not found or details are the same.</h1>");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
pw.println("<h1>Error loading database driver: " + e.getMessage() + "</h1>");
} catch (SQLException e) {
e.printStackTrace();
pw.println("<h1>Error updating details: " + e.getMessage() + "</h1>");
} finally {
try {
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter