Write a JSP program to accept the details of Account (ANo, Type, Bal) and store it into database and display it in tabular form.
JSP Form Page – accountForm.jsp
<!DOCTYPE html>
<html>
<head>
<title>Account Details Form</title>
</head>
<body>
<h2>Enter Account Details</h2>
<form action="accountInsert.jsp" method="post">
Account No: <input type="number" name="ANo" required><br><br>
Account Type: <input type="text" name="Type" required><br><br>
Balance: <input type="number" name="Bal" step="0.01" required><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Display Page – accountInsert.jsp
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>Account Details</title>
</head>
<body>
<h2>Account Details</h2>
<%
// Read form data
int ANo = Integer.parseInt(request.getParameter("ANo"));
String Type = request.getParameter("Type");
double Bal = Double.parseDouble(request.getParameter("Bal"));
// Database connection parameters
String url = "jdbc:mysql://localhost:3306/BankDB";
String user = "root";
String password = ""; // Set your MySQL password
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// Load MySQL JDBC Driver
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection(url, user, password);
// Insert account details
String insertQuery = "INSERT INTO Account (ANo, Type, Bal) VALUES (?, ?, ?)";
ps = con.prepareStatement(insertQuery);
ps.setInt(1, ANo);
ps.setString(2, Type);
ps.setDouble(3, Bal);
ps.executeUpdate();
// Retrieve all accounts
String selectQuery = "SELECT * FROM Account";
ps = con.prepareStatement(selectQuery);
rs = ps.executeQuery();
// Display in table
%>
<table border="1" cellpadding="5" cellspacing="0">
<tr>
<th>Account No</th>
<th>Type</th>
<th>Balance</th>
</tr>
<%
while(rs.next()){
%>
<tr>
<td><%= rs.getInt("ANo") %></td>
<td><%= rs.getString("Type") %></td>
<td><%= rs.getDouble("Bal") %></td>
</tr>
<%
}
%>
</table>
<%
} catch(Exception e) {
out.println("Error: " + e.getMessage());
} finally {
try { if(rs != null) rs.close(); } catch(Exception e) {}
try { if(ps != null) ps.close(); } catch(Exception e) {}
try { if(con != null) con.close(); } catch(Exception e) {}
}
%>
</body>
</html>