Results 1 to 1 of 1
  1. #1
    sandy1000's Avatar
    sandy1000 is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Smile use of ajax and jsp to extract values from a table

    here 2 programs are used for explanation
    one jsp program to display information.
    One jsp program to withdraw information from database
    first I created a database named shanu using ms access

    In the database i created a tableknown as student

    it contains 3 fields
    student_id auto integer
    student name text
    student_address text

    After creation of the database i create a user DSN named shanu

    now i write the jsp program to design the screen
    The name of the file is ajax_db_example.jsp

    <html>
    <head>
    <script type="text/javascript">
    function showCustomer(str){
    if (str==""){
    document.getElementById("txtHint").innerHTML="";
    return;
    }


    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET","getstudent.jsp?q="+str,true);
    xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
    var xml=xmlhttp.responseXML;
    var tags=xml.getElementsByTagName("tr");

    for(var i=0;i<tags.length;i++){
    document.getElementById("st_id").innerHTML=tags[i].childNodes[0].childNodes[0].nodeValue;
    document.getElementById("st_name").innerHTML=tags[i].childNodes[1].childNodes[0].nodeValue;
    document.getElementById("st_add").innerHTML=tags[i].childNodes[2].childNodes[0].nodeValue;

    }
    //document.getElementById("txtHint").innerHTML=xmlht tp.responseText;
    //document.getElementById("names").value =xmlhttp.responseText;
    }
    }
    xmlhttp.send();

    }
    </script>
    </head>
    <body>

    <form name="form1" action="" method="post">
    <select name="customers" onChange="showCustomer(this.value)">
    <option value="">Select a customer:</option>
    <option value="2">Shanu</option>
    <option value="3">Neelu</option>
    <option value="4">Mahendra</option>
    </select>

    <br />
    <div id="st_id">Customer id will be listed here...</div>
    <div id="st_name">Customer name will be listed here...</div>
    <div id="st_add">Customer address will be listed here...</div>
    </form>
    </body>
    </html>
    [/COLOR]Now I write a jsp file to withdraw values from the database shanu . File Name
    getstudent.jsp
    [/COLOR]


    <%@page import = "java.io.PrintWriter" %>
    <%@page import="java.sql.*"%>
    <%
    String names = request.getParameter("q");
    //String tables = "<table><tr><td>hiii</td></tr></table>";
    //out.print(names);
    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String url = "jdbc:odbc:shanu";
    Connection con = DriverManager.getConnection(url);
    Statement st=con.createStatement();
    ResultSet rs= st.executeQuery("select * from student where student_id="+ names );
    //out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
    String xml="<table>";
    while(rs.next()){
    xml+="<tr>";
    xml+="<td>";
    xml+=(String)rs.getString(1);
    xml+="</td><td>";
    xml+=(String)rs.getString(2);
    xml+="</td><td>";
    xml+=(String)rs.getString(3);
    xml+="</td>";
    xml+="</tr>";
    }
    xml+="</table>";
    out.print(xml);
    con.close();
    PrintWriter pw=response.getWriter();
    response.setContentType("text/xml");
    pw.write(xml);
    pw.flush();
    pw.close();
    }
    catch(Exception e){}
    %>
    Last edited by sandy1000; 12-27-2010 at 12:38 PM.

Similar Threads

  1. Struts 2 persistance for Table values
    By aaruviswa in forum Web Frameworks
    Replies: 1
    Last Post: 12-30-2010, 05:12 AM
  2. JSP/Ajax/Post giving null values
    By Mange in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-22-2010, 11:02 PM
  3. how to extract cmyk values in a pdf file
    By jkrishnanvenkat in forum New To Java
    Replies: 0
    Last Post: 01-22-2010, 03:02 PM
  4. Replies: 1
    Last Post: 01-08-2010, 07:19 AM
  5. Replies: 0
    Last Post: 02-28-2009, 12:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •