MCS-051 : ADVANCED INTERNET TECHNOLOGIES | 1. (a) Write a web application using JSP and JDBC that takes a bank account number as input and displays the account balance.
MCS-051 : ADVANCED INTERNET TECHNOLOGIES | 1. (a) Write a web application using JSP and JDBC that takes a bank account number as input and displays the account balance. Assume that the balance after every transaction is available in the database
MCA (Revised)
Term-End Examination
June, 2021
MCS-051 : ADVANCED INTERNET TECHNOLOGIES 
 1. (a) Write a web application using JSP and JDBC that takes a bank account number as input and displays the account balance. Assume that the balance after every transaction is available in the database.                                                                                                                                                 7 
To watch this video click on the following Picture 
Design Home Page (index.html)
CODE FOR INDEX.HTML
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body bgcolor="lightyellow">
    <center>
        <div>
            <h1>Check Current Balance</h1>
            <form method="post" action="check_balance.jsp">
                <table border="1" bgcolor="lightgreen">
                    <tr>
                        <td><label>Enter Account Number</label></td><td><input type="text" name="ano"></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"><input type="submit" value="Submit"></td>
                    </tr>
                </table>
            </form>
        </div>
            <hr>
            <div>
            <h1>Update Balance</h1>
            <form method="post" action="update_account.jsp">
              <table border="1" bgcolor="lightblue">
                  <tr>
                        <td><label>Enter Account Number</label></td><td><input type="text" name="ano"></td></tr>
                    <tr>
                        <td><label>Transaction Amount</label></td><td><input type="text" name="amt"></td>
                    </tr>
                                        <td colspan="2" align="center"><input type="Submit" value="Submit"></td>
                    </tr>
                </table>    
            </form>
        </div>
    </center>
    </body>
</html>
    
        Check Current Balance
Update Balance
Design CHECK_BALANCE Page (check_balance.jsp)
CODE FOR CHECK_BALANCE.JSP
<%--
Document : check_balance
Created on : 10 Mar, 2022, 11:12:49 AM
Author : Nitin
--%>
<%@page import="java.sql.DriverManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body bgcolor="DodgerBlue">
<center><h1>Your Current Balance</h1></center>
<%
try{
String an=request.getParameter("ano");
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/dtm", "root","");
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select * from tr where ano='"+an+"'");
out.println("<table border='1' width='50%' bgcolor='lightblue' align='center'>");
out.println("<tr><th>Account Number</th><th>Customer Name</th><th>Balance</th><th>Transactio date</th></tr>");
while(rs.next())
out.println("<tr align='center'><td>"+rs.getString(1)+"</td><td>"+rs.getString(2)+"</td><td>"+rs.getString(3)+"</td><td>"+rs.getString(4)+"</td></tr>");
out.println("</table>");
conn.close();
            }catch(Exception e)
{
out.println(e);
}
%>
</body>
</html>
Design UPDATE_ACCOUNT Page (update_account.jsp)
CODE FOR UPDATE_ACCOUNT.JSP
<%--
Document : update_acount
Created on : 10 Mar, 2022, 1:16:51 PM
Author : Nitin
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body bgcolor="lightseagreen">
<center>
<h1>Your Account has been updated!</h1>
<%
try{
String ano=request.getParameter("ano");
double amt=Double.parseDouble(request.getParameter("amt"));
out.println("<table border='1'><tr><th colspan='2'>Account Summary</th></tr><tr><td>");
out.println("Account number</td><td>"+ano+"</td></tr><tr><td>");
out.println("Transaction Amount </td><td> "+amt+"</td></tr></table>");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/dtm","root","");
Statement st=con.createStatement();
String q1="select bal from tr where ano='"+ano+"'";
ResultSet rs=st.executeQuery(q1);
while(rs.next())
out.println("</br></br><h3>Your Balance Before This transaction : "+rs.getString(1)+"</h3>");
String query="update tr set bal=bal+"+amt+"where ano='"+ano+"'";
st.executeUpdate(query);
String q2="select bal from tr where ano='"+ano+"'";
rs=st.executeQuery(q2);
while(rs.next())
out.println("</br></br><h3>Now Your Available Balance : "+rs.getString(1)+"</h3>");
}
catch(Exception e)
{
out.println(e);
}
out.println();
%>
</center>
</body>
</html>
 MySQL Queries for Above Web Application
MySQL Related
-------------------------------------
mysql> SET time_zone = '+05:30';
Query OK, 0 rows affected (0.01 sec)
-------------------------------------
mysql> create database dtm;
Query OK, 1 row affected (0.02 sec)
mysql> use dtm;
Database changed
mysql> create table tr
    -> (ano varchar(10), cname varchar(50), Bal numeric(12,2), tdt timestamp);
Query OK, 0 rows affected (0.10 sec)
mysql> insert into tr
    -> values('a101', 'Ramesh', 10545.35,now());
Query OK, 1 row affected (0.15 sec)
mysql> select * from tr;
+------+--------+----------+---------------------+
| ano  | cname  | Bal      | tdt                 |
+------+--------+----------+---------------------+
| a101 | Ramesh | 10545.35 | 2022-03-09 20:48:31 |
+------+--------+----------+---------------------+
1 row in set (0.02 sec)
mysql> update tr
    -> set bal=bal-500
    -> where ano='a101';
Query OK, 1 row affected (0.10 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from tr;
+------+--------+----------+---------------------+
| ano  | cname  | Bal      | tdt                 |
+------+--------+----------+---------------------+
| a101 | Ramesh | 10045.35 | 2022-03-09 20:51:27 |
+------+--------+----------+---------------------+
1 row in set (0.00 sec)
mysql> insert into tr
    -> values('a102', 'Suresh', 14735.65,'2022-02-09 15:45:30');
Query OK, 1 row affected (0.02 sec)
mysql> select * from tr;
+------+--------+----------+---------------------+
| ano  | cname  | Bal      | tdt                 |
+------+--------+----------+---------------------+
| a101 | Ramesh | 10045.35 | 2022-03-09 20:51:27 |
| a102 | Suresh | 14735.65 | 2022-02-09 15:45:30 |
+------+--------+----------+---------------------+
2 rows in set (0.00 sec)
mysql> update tr
    -> set bal=bal-500
    -> where ano='a102';
Query OK, 1 row affected (0.10 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from tr;
+------+--------+----------+---------------------+
| ano  | cname  | Bal      | tdt                 |
+------+--------+----------+---------------------+
| a101 | Ramesh | 10045.35 | 2022-03-09 20:51:27 |
| a102 | Suresh | 14235.65 | 2022-03-09 20:54:37 |
+------+--------+----------+---------------------+
2 rows in set (0.00 sec)
mysql> 
Other MySQL Queries
mysql> use dtm;
Database changed
mysql> show tables;
mysql> create table ms
    -> (ano varchar(12), cname varchar(50), tdate timestamp,
    -> primary key(ano,tdate)
    -> );
Query OK, 0 rows affected (1.54 sec)
mysql> alter table ms
    -> add column bal numeric(12,2);
Query OK, 0 rows affected (3.50 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> describe ms;
+-------+---------------+------+-----+-------------------+-----------------------------+
| Field | Type          | Null | Key | Default           | Extra                       |
+-------+---------------+------+-----+-------------------+-----------------------------+
| ano   | varchar(12)   | NO   | PRI |                   |                             |
| cname | varchar(50)   | YES  |     | NULL              |                             |
| tdate | timestamp     | NO   | PRI | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| bal   | decimal(12,2) | YES  |     | NULL              |                             |
+-------+---------------+------+-----+-------------------+-----------------------------+
4 rows in set (1.19 sec)
mysql> insert into ms
    -> values('a101','Gaurav','05-01-22 15:45:20',10500.65);
Query OK, 1 row affected (0.04 sec)
mysql> select * from ms;
+------+--------+---------------------+----------+
| ano  | cname  | tdate               | bal      |
+------+--------+---------------------+----------+
| a101 | Gaurav | 2005-01-22 15:45:20 | 10500.65 |
+------+--------+---------------------+----------+
1 row in set (0.00 sec)
mysql> insert into ms
    -> values('a101','Gaurav',now(),-500);
Query OK, 1 row affected (0.13 sec)
mysql> select * from ms;
+------+--------+---------------------+----------+
| ano  | cname  | tdate               | bal      |
+------+--------+---------------------+----------+
| a101 | Gaurav | 2005-01-22 15:45:20 | 10500.65 |
| a101 | Gaurav | 2022-03-10 12:46:31 |  -500.00 |
+------+--------+---------------------+----------+
2 rows in set (0.00 sec)
mysql> create table cb
    -> (ano varchar(12), abal numeric(12,2));
Query OK, 0 rows affected (0.45 sec)
mysql> insert into cb
    -> values('a101', (select distinct sum(bal) from ms where ano='a101'));
mysql> select * from cb;
+------+----------+
| ano  | abal     |
+------+----------+
| a101 | 10000.65 |
+------+----------+
1 row in set (0.00 sec)
mysql> insert into ms
    -> values('a102','Vikas','2022-02-01 10:20:25',5000),('a102','Vikas',now(),-300);
Query OK, 2 rows affected (0.22 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> insert into cb
    -> values('a102', (select distinct sum(bal) from ms where ano='a102'));
mysql> select * from cb;
+------+----------+
| ano  | abal     |
+------+----------+
| a101 | 10000.65 |
| a102 |  4700.00 |
+------+----------+
2 rows in set (0.00 sec)
mysql>  
You can also visit my YouTube Channel Gaurav Pali MCA