You can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Ajax Onchange Fetch The Data From The Database

Ajax » On Oct 20, 2011 By Sivateja

Let us see how to fetch the data from database onchange of drop down, actually this is the real time scenario, am using jsp  you can integrate with any type of frame works ( in .java files ), concept is same.

Files Required

  • index.html
  • db_fetch.jsp
  • web.xml





Note: you must have classes12.jar, ojdbc14.jar in the lib folder of you application



Click Here To Check The Output

By ( Java/J2EE Developer & Web Designer )
Founder of You can contact me at sivateja [at]


18 Responses to “Ajax Onchange Fetch The Data From The Database”
  1. Emil Schoeck says:

    I like this post, enjoyed this one appreciate it for posting .

  2. Mohammed Vaseem says:


    When am executing this program am getting error in javascript at line number 21 saying as xmlhttp.responseXML is null. Am using mysql database. I got to know this from the error console of the mozilla browser.

    I did not understand purpose of
    1. function loadXMLDoc1()

    Please provide explaination.

  3. Java4s says:

    @Mohammed Vaseem

    – Sorry friend, i overlooked that function while executing [ function loadXMLDoc1() removed now ].

    – document.getElementById(“a”).innerHTML=some.getElementsByTagName(“empno”)[0].childNodes[0].nodeValue;

    Here document.getElementById(“a”).innerHTML means in place of span id=”a”, we are printing ‘empno‘ which is in the jsp page [ that is XML syntax ]

  4. Mohammed Vaseem says:

    Again am getting the same error at the same line. Please help.

  5. Java4s says:

    @Mohammed Vaseem

    I have tested, every thing is working fine from my end.

    Check once and make your jsp file is able to fetch the values from the database and test whether it is able to print the values by removing xml tags [ out.println(rs.getInt(1)) ], your jsp file must work.

  6. Mohammed Vaseem says:

    Thank you for your extreme help.

    I did mistake in the line 8. I Directly copy pasted the program. After giving correct path i got output.

  7. Java4s says:

    @Mohammed Vaseem

    Gosh, any ways good to here that you got it :-)

  8. Bikash says:

    i like your website very much.its very easy to understand..

    while testing ajax program,its not working,i can’t understand this line
    var urls=””+keys
    –>how Index.html will link to db_fetch.jsp
    thank you

  9. Java4s says:


    See line number 27, we have open() method in xmlhttp object right, once program flow reaches to this line, Ajax object will sends the request to the url in ‘urls’ variable.

    There we used to write business logic and all.

  10. Jovin says:

    Hai Bikash,

    You just simply use the path of ‘db_fetch.jsp’ of your project.It will work.
    what i mean is that, if Your db_fetchjsp is in the same folder, just use
    var url=”db_fetch.jsp?ok=”+key
    hope it will work..

  11. amit says:

    with due respect —

    sir ! Although i have understood the full code , but still i am having trouble in executing the same. you have written
    ” var some=xmlhttp.responseXML.documentElement; “, but the response is coming from an jsp file then why “responseXML” (number-1) and i have put an alert after this line and that alert is also not working.

    so i request you to please guide me accordingly as soon as possible…

  12. Java4s says:


    Though we are getting the response from jsp, we must specify the type of response(responseXML) there.

  13. kasi says:

    Hello sir i am getting error when i execute using“GET”,”db_fetch.jsp”,true); when i am using responseText its working but i cant able to get response using responseXML……plz help.

  14. sudhanshu says:

    I got this error

    SEVERE: Servlet.service() for servlet [jsp] in context with path [/AjaxFetch] threw exception [java.lang.NumberFormatException: For input string: “”] with root cause
    java.lang.NumberFormatException: For input string: “”
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)

    please help

  15. Wht to do if we have datatype of column is varchar….?

    because my empno is of varchar…

  16. it is working but i have problem when i have a ‘&’ character in a string .it does not give result for eg;
    if i have “me & you” it does not reaqd this.

  17. trinadh says:

    i’m not getting the output even though i did everything as it shown above.

    i am not getting in jsp
    even i mention additional out.println’s

  18. dilip says:

    sir i tried it but im not getting anything when im selecting a value


By posting your answer, you agree to our comments policy.
So, What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs

Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services

  Web Services [ SOAP ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
© 2010 - 2015 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.