-1

I want to just show my data from database on div notifications using Ajax but it doesn't work. I am using Maven and Spring, Hibernate and in this code I am using only JDBC, just to see if it's working or not. But it doesn't give any output.

function getNotifications() {
  var xmlHttp;
  try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e) {
    try {
      xmlHttp = new XMLHttpRequest();
    } catch (e) {
      alert("your browser cant support");
    }
  }
  xmlHttp.onreadystatechange = function () {
    if (xmlHttp.readyState == 4) {
      var x = (xmlHttp.responseText);
      document.getElementById("notifications").innerHTML = x;
      /* document.write(xmlHttp.responseText); */
    }
  }
  var url = "auto.jsp";
  url = url + "?q=" + str;
  xmlHttp.open("get", url, true);
  xmlHttp.send(url);
}        

auto.jsp

<%@ page import="java.sql.*" %>
<% String s="G";
System.out.println("helloooooooooo1111111");
String result=""; 
ArrayList li = new ArrayList();
try {
  Class.forName("com.mysql.jdbc.Driver");
  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2402","root","admin");

  System.out.println("helloooooooooo1111111");
  Statement st = con.createStatement();
  String sql = "select * from message ";
  System.out.println("helloooooooooo22222266666");
  st.execute(sql);

  ResultSet rs = st.getResultSet();

  System.out.println("helloooooooooo222222");
  int i = 1;
  while(rs.next()){
    li.add(rs.getString(i));
    i = i++;
  }
}
catch(Exception ex){
  out.println(ex);
}
%>

<% 
Iterator itr = li.iterator();

while (itr.hasNext()) {
  out.println(itr.next());  
}

%>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Nav Kumar
  • 141
  • 3
  • 5
  • 12
  • 2
    "Doesn't work" is not much of an error description. – Werner Kvalem Vesterås Sep 20 '13 at 11:30
  • error means when i put alert it this script it doest show the message in block if(xmlHttp.readyState==4){ var x = (xmlHttp.responseText); document.getElementById("notifications").innerHTML= x; /* document.write(xmlHttp.responseText); */ } – Nav Kumar Sep 20 '13 at 11:36
  • i am using ajax first time soo i dont know how it work but i know this code may be much enough – Nav Kumar Sep 20 '13 at 11:37
  • 2
    @kryger: library/framework/API names like "Maven", "Spring" and "Hibernate" are NOT code. Please do not format them as code in suggested edits! – BalusC Sep 20 '13 at 13:32

1 Answers1

0

At first look problem seems to be in two statements. You are reassigning value instead of appending.
You have url variable

var url = "auto.jsp";
url = url + "?q=" + str;  

Change to

var url = "auto.jsp";
url += "?q=" + str;   

In auto.jsp inside

while(rs.next()){
 li.add(rs.getString(i));
 i++; //here simple i++ will work fine.
}   

Edit

Not related:

  1. how to avoid scriptlets in jsp
  2. preincrement and postincrement in java
Community
  • 1
  • 1
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90