Thursday, 10 April 2025

Write a JSP program to accept the details of Account (ANo, Type, Bal) and store it into database and display it in tabular form (Use Prepared Statement interface).




account.html



<html>

<head>

    

</head>

<body>


       <form action="account.jsp" method="post">

        <label>Account Number:</label> 

        <input type="number" name="ano" required><br>


        <label>Account Type:</label> 

        <input type="text" name="type" required><br>


        <label>Balance:</label> 

        <input type="number"  name="bal" required><br>


        <button type="submit">Submit</button>

    </form>


</body>

</html>


account.jsp


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

<!DOCTYPE html>

<html>

<head>

    <title>Account Details</title>

</head>

<body>


<%

    Class.forName("com.mysql.jdbc.Driver");

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mca", "root", "");

    PreparedStatement pstmt;

    ResultSet rs;


    if (request.getMethod().equalsIgnoreCase("POST")) {

        int ano = Integer.parseInt(request.getParameter("ano"));

        String type = request.getParameter("type");

        int bal = Integer.parseInt(request.getParameter("bal"));


        String sql = "INSERT INTO account (ANo, Type, Bal) VALUES (?, ?, ?)";

        pstmt = con.prepareStatement(sql);

        pstmt.setInt(1, ano);

        pstmt.setString(2, type);

        pstmt.setInt(3, bal);

        pstmt.executeUpdate();

    }


    // Fetch all account records

    String fetchSQL = "SELECT * FROM account";

    pstmt = con.prepareStatement(fetchSQL);

    rs = pstmt.executeQuery();

%>


<h2>Account Records</h2>

<table border="1">

    <tr>

        <th>Account Number</th>

        <th>Type</th>

        <th>Balance</th>

    </tr>

    <%

        while (rs.next()) {

    %>

    <tr>

        <td><%= rs.getInt("ANo") %></td>

        <td><%= rs.getString("Type") %></td>

        <td><%= rs.getInt("Bal") %></td>

    </tr>

    <%

        }

    %>

</table>


<br>

<a href="account.html">Add Another Account</a>


<%

    rs.close();

    pstmt.close();

    con.close();

%>


</body>

</html>