0

Im using JSP--AJAX--SERVLET .Need to fetch value from database .Im getting value from JSP without AJAX in next page but purpose to need ajax is to fetch value on same page . Here is the code: index.jsp:

  <%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.*" %>
<% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); %>
<%@page import="java.io.FileInputStream"%>
<jsp:useBean id="NeftRtgsCount" scope="page" class="in.ac.idrbt.NeftRtgsCount" />
<html>  
<title>sfms count</title>
<head>
<body > 
 <form  >
<script language="javascript" type="text/javascript" src="js/datetimepicker.js"></script>
<br/>
 <h4>From date:</h4>
<input type="text" size="30"  id="demo1" name ='date1' /><a href="javascript:NewCal('demo1','ddmmmyyyy',true,24)"><img src="css/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a> 
<h4>To date:</h4>
<input type="text"  size="30"  id="demo2" name ='date2'/><a href="javascript:NewCal('demo2','ddmmmyyyy',true,24)"><img src="css/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a> 
<br/>
<br/>
 <h4> Type: </h4><select name="type">
  <option value="NEFT">NEFT</option>
  <option value="RTGS">RTGS</option>
  <option value="NEFT&RTGS">NEFT&RTGS</option>
 </br>
 </br> 
</select> <br/><script>
 $(document).ready(function() {
         $("#Getcount").click(function() {
             servletCall();
         });

     });
     function servletCall() {
         $.post(
             "NeftRtgsCount", 
             {data : {
                date1 : $('#date1').val(),
                date2 : $('#date2').val(),
                type : $('#type').val()
        }}, 
             function(result) {
             $('#div').html('Count : <strong>' + result + '</strong>'); //message you want to show
         });
     };
     </script>
 <button id="Getcount">Getcount</button>
     <div id="div"></div>

<li>

<br/>
<br/>

</head>
   </form>

</body>  
</html> 

NeftRtgsCount.java

    package in.ac.idrbt;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class NeftRtgsCount
 */
public class NeftRtgsCount extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
  //  public NeftRtgsCount1() {
   //     super();
        // TODO Auto-generated constructor stub
   // }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.getWriter().append("Served at: ").append(request.getContextPath());
        PrintWriter pw=response.getWriter();
        response.setContentType("text/html");

        String FROM_DATE = request.getParameter("date1").toUpperCase().trim();
        String TO_DATE = request.getParameter("date2").toUpperCase().trim();
        String TYPE =request.getParameter("type");


        Connection con;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        con = DriverManager.getConnection("jdbc:oracle:thin:@172.16.105.48:1521:sfms","sfmsbr","sfms");
        if((TYPE).equals("NEFT"))
        {
        ps = con.prepareStatement("Select sum(NEFT_OUT) TOTAL_NEFT_OUT from (select count(*) NEFT_OUT from MHB_MSG_HDR_BR_AR where MHB_MSG_TYP in ('2184', '2185') and MHB_OWNR_ADDRS='IFTO0000001' and MHB_SEND_DATE between TO_DATE('"+FROM_DATE+"', 'DD-MON-YYYY HH24:MI:SS') and TO_DATE('"+TO_DATE+"', 'DD-MON-YYYY HH24:MI:SS') union all select count(*) NEFT_OUT from MHB_MSG_HDR_BR where MHB_MSG_TYP in ('2184', '2185') and MHB_OWNR_ADDRS='IFTO0000001' and MHB_SEND_DATE between TO_DATE('"+FROM_DATE+"', 'DD-MON-YYYY HH24:MI:SS') and TO_DATE('"+TO_DATE+"', 'DD-MON-YYYY HH24:MI:SS'))");
        rs = ps.executeQuery();


        while (rs.next())
        {

        //String NeftCount=null;
        pw.println( "NEFT COUNT :"+rs.getInt(1)+"");

        } 
        }
        else if((TYPE).equals("RTGS")){
            ps = con.prepareStatement("Select sum(RTGS_OUT) TOTAL_RTGS_OUT from(select count(*) RTGS_OUT from MHB_MSG_HDR_BR_AR where MHB_MSG_TYP in ('5010','5011', '5012', '5013') and MHB_OWNR_ADDRS='IFTO0000001' and MHB_SEND_DATE between TO_DATE('"+FROM_DATE+"', 'DD-MON-YYYY HH24:MI:SS') and TO_DATE('"+TO_DATE+"', 'DD-MON-YYYY HH24:MI:SS') union all select count(*) RTGS_OUT from MHB_MSG_HDR_BR where MHB_MSG_TYP in ('5010','5011', '5012', '5013') and MHB_OWNR_ADDRS='IFTO0000001' and MHB_SEND_DATE between TO_DATE('"+FROM_DATE+"', 'DD-MON-YYYY HH24:MI:SS') and TO_DATE('"+TO_DATE+"', 'DD-MON-YYYY HH24:MI:SS'))");
            rs = ps.executeQuery();


            while (rs.next())
            {

            //String NeftCount=null;
            pw.println( "RTGS COUNT :"+rs.getInt(1)+"");

            }
        }
        }
        catch (SQLException e)
        {
            System.out.println(e);
        }
        catch(ClassNotFoundException ex)
        {
            System.out.println("ex");

        }
        catch (NullPointerException nullPointer)
        {
            System.out.println(nullPointer);
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    doGet(request, response);
    }


    }

After clicking Getcount .Url changes to

http://localhost:8181/NeftRtgsCount/index.jsp?date1=1-Feb-2016+10%3A52%3A14&date2=1-Feb-2017+10%3A52%3A14&type=NEFT and then nothing displays .

aswini
  • 19
  • 3
  • Select sum(NEFT_OUT) TOTAL_NEFT_OUT from ( select count(*) NEFT_OUT from MHB_MSG_HDR_BR_AR where MHB_MSG_TYP in ('2184', '2185') and MHB_OWNR_ADDRS='IFTO0000001' and MHB_SEND_DATE between TO_DATE('"+FROM_DATE+"', 'DD-MON-YYYY HH24:MI:SS') and TO_DATE('"+TO_DATE+"', 'DD-MON-YYYY HH24:MI:SS') union all select count(*) NEFT_OUT from MHB_MSG_HDR_BR where MHB_MSG_TYP in ('2184', '2185') and MHB_OWNR_ADDRS='IFTO0000001' and MHB_SEND_DATE between TO_DATE('"+FROM_DATE+"', 'DD-MON-YYYY HH24:MI:SS') and TO_DATE('"+TO_DATE+"', 'DD-MON-YYYY HH24:MI:SS') ) – Sagar Gangwal Feb 03 '17 at 05:59
  • Have you checked that above query return single row of Data? If it's not then as shown in code printwriter object print nothing to response.Or you may check that if response is coming there by simply putting this line after creation of PrintWriter Object pw.println( "Call this method"); – Sagar Gangwal Feb 03 '17 at 06:07
  • The same query worked without ajax ..The result is a single row(count) .If you see in NeftRtgsCount.java i have given `pw.println( "NEFT COUNT :"+rs.getInt(1)+""); ` But not even displaying result in console .I suspect the values are not going to servlet from ajax . – aswini Feb 03 '17 at 07:18

0 Answers0