Monday, March 8, 2021

JSP program showing account details by taking account number as input.

 //File 'Bank.jsp' inside the 'Web pages' folder to display account details

<%-- 

    Document   : Bankjsp

    Created on : 19 Mar, 2020, 12:04:04 PM

    Author     : ashu

--%>

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

<%@ page import="javax.servlet.http.*,javax.servlet.*" %>

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

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

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

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

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

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

<%@ page language="java"%>

<%@ page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

    <head>

        <title>Real Bank JSP Page</title>

        <script>

            function printDiv(divName) {

                var printContents = document.getElementById(divName).innerHTML;

                var originalContents = document.body.innerHTML;


                 document.body.innerHTML = printContents;


                 window.print();


                document.body.innerHTML = originalContents;

        }

        </script>

    </head>

    <body>

    <div id="printableArea">                //For the 'print' button 

    <center>

        <h1>Welcome to Real Bank!</h1>

        <h3>Customers with balance greater than Rs.5000/- in SB a/c </h3>

        

        <%

            try {

            String connectionURL = "jdbc:mysql://localhost/bank";

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

            Connection con = DriverManager.getConnection(connectionURL, "root", "ashutosh");

            if(con.isClosed()){

                 out.println("Successfully connected to " + "MySQL server using TCP/IP...");

            }

            Statement stmt = con.createStatement();

            ResultSet rs = stmt.executeQuery("select customerName, customerAddress, accountNum from customers where accountType='SB' and balance >= 5000.00;");

            %>

            <table border=1>

            <tr><th>Name</th><th>Address</th><th>Account Number</th></tr>

            <%

            while(rs.next()){

            %>

            <tr>

            <td><%=rs.getString(1)%></td>

            <td><%=rs.getString(2)%></td>

            <td><%=rs.getInt(3)%></td>

            </tr>

        <%   

        }

        rs.close();

        stmt.close();

        con.close();

        }catch(Exception ex){

            out.println("Unable to connect to database"+ex);

        }  

        %>

        </table>

        <br><br>   

        <%

            out.println("<input type= 'button' onclick=printDiv('printableArea') value='Print'>");

        %>

        <br><br>

        <%

            

            out.println("<input type= 'button' onclick=location.href='index.html' value='back'>");

        %>

        

    </center>

    </div>

    </body>

</html>

--------------------------------------------------------------------------------------------------------
//The 'Accountjsp.jsp' file to fetch account details for a given account number

<%-- 
    Document   : Accountjsp
    Created on : 9 Mar, 2021, 8:31:38 AM
    Author     : ashu
--%>

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ page import="java.math.*"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.SQLException"%>
<%@ page import="java.sql.Statement"%>
<%@ page language="java"%>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Account Details</title>
        <script>
            function printDiv(divName) {
                var printContents = document.getElementById(divName).innerHTML;
                var originalContents = document.body.innerHTML;

                 document.body.innerHTML = printContents;

                 window.print();

                document.body.innerHTML = originalContents;
        }
        
        
        </script>
    </head>
    <body>
    <div id="printableArea">
    <center>
        <h1>Welcome to Real Bank!</h1>
 
        
        <%
            try {
            String connectionURL = "jdbc:mysql://localhost/bank";
            Class.forName("com.mysql.jdbc.Driver").newInstance(); 
            Connection con = DriverManager.getConnection(connectionURL, "root", "ashutosh");
            if(con.isClosed()){
                 out.println("Successfully connected to " + "MySQL server using TCP/IP...");
            }
            Statement stmt = con.createStatement();
            
            String account_number = request.getParameter("acNum");
            //out.println(account_number);
            ResultSet rs = stmt.executeQuery("select customerName, customerAddress, accountNum, balance, accountType from customers where accountNum = " + account_number + ";");
            %>
            <table border=1>
            <tr><th>Name</th><th>Address</th><th>Account Number</th><th>Balance</th><th>Account Type</th></tr>
            <%
            while(rs.next()){
            %>
            <tr>
            <td><%=rs.getString(1)%></td>
            <td><%=rs.getString(2)%></td>
            <td><%=rs.getInt(3)%></td>
            <td><%=rs.getString(4)%></td>
            <td><%=rs.getString(5)%></td>
            </tr>
        <%   
        }
        rs.close();
        stmt.close();
        con.close();
        }catch(Exception ex){
            out.println("Unable to connect to database"+ex);
        }  
        %>
        </table>
        <br><br>   
        <%
            out.println("<input type= 'button' onclick=printDiv('printableArea') value='Print'>");
        %>
        <br><br>
        <%
            
            out.println("<input type= 'button' onclick=location.href='index.html' value='back'>");
        %>
        
    </center>
    </div>
    </body>
</html>


---------------------------------------------------------------------------------------------------------

//Corresponding HTML page '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>Real Bank</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
    <center><h1>Welcome to Real Bank!</h1>
        <h2>You can bank with us for best returns and services</h2>
    <div>Details of customers having greater than equal to Rs.5000/- in savings account</div>
    <br><br>
    <input type= "button" onClick="location.href='Bank.jsp'" value="Click to view details">
    <br><br>
    <div>Details of customer having savings account number: <input type= "text" id = "acNum" name = "acNum" maxlength = "10" size = "10"></div>
    <br><br>
    
<!-- Excellent way to send textbox value from HTML file to a JSP file -->
    <input type= "button" onClick='location.href = "Accountjsp.jsp?acNum="  + document.getElementById("acNum").value;'  value ="Account details">
    
    </center>
        
    </body>
</html>

-------------------------------------------------------------------------------------------------------------------
//Project directory structure




----------------------------------------------------------------------------------------------------------------------
//Output

//index page at url = http://localhost:8080/jspJDBC/
//Syntax for url of index file = http://localhost:<port_number>/<project_name>/





----------------------------------------------------------------------------------------------------------------

// The 'Bank.jsp' page opens on clicking the button 'Click to View Details' at the index (Home) page




-----------------------------------------------------------------------------------------------------------------------

//Result of clicking the 'Print' button


The actual pronted PDF file


----------------------------------------------------------------------------------------------------------------
Output for 'Account Details' button





Output of print button of this page





No comments:

Post a Comment

इश्क में ग़ैरत-ए-जज़्बात ने रोने ना दिया - सुदर्शन फ़ाकिर

 इश्क में ग़ैरत-ए-जज़्बात ने रोने ना दिया वरना क्या बात थी किस बात ने रोने ना दिया आप कहते थे कि रोने से ना बदलेंगे नसीब उमर भर आप की इस बात...