AJAX - Database Connection 
without reloading the page
Ajax is the mastermind to display data directly from database without reloading the page. How to achieve this without calling a Servlets Action or Struts Action without reloading or refreshing the page how to display the data from table. Google showing suggestions without reloading the page.

Here am going to explain how to display data using Database.
Am going to use combination of JavaScript, AJAX and MySql Database.

Steps to connect Database using AJAX

Step 1:
  • Create a Database and Table with fields.
Ex: Database table contains Empid int(20), EmpName varchar(200)

Step 2:
Use a Servlets action or JSP or PHP to write database logic. Am going to use Servlet Action.

Configure Web.xml 
-------------------------
<servlet>
<servlet-name>ajaxtutorial</servlet-name>
<servlet-class>com.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ajaxtutorial</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>

Test.java

public class Test extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
  {
     String val = request.getParameter("id");
     String stor=null;
     Connection con = null;
     PreparedStatement pst =null;
     ResultSet rs=null;
     response.setContentType("text/xml");
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     pst = con.prepareStatement("select Empid, EmpName from employee where Empid=?");
     pst.setString(1,val);
     if(rs.next())
     {
      stor =rs.getString(1);
      response.getWriter().write(stor);
     }
  con.close();
  }catch(SQLException e)
   {
    
   }
}
}

Step 3:
 In your JSP page or PHP page write this JavaScript function

<script type="text/javascript">

function ajaxDB()
{
var boxtype=document.getElementById("boxtype");
var url = "/test?id="+boxtype;
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();

}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
var x= document.getElementById("hello");
x.innerHTML=xmlhttp.responseText;
}
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
</script>
<input type="text" id="boxtype"></input>
<div id="hello"></div>
»»  Read More
Read more!
Steps to Connect AJAX Database using Struts 
 As i have explained clearly connecting AJAX with Servlets in previous chapter. 
Now i going to explain how to connect Database using Struts and AJAX.

Steps to Connect Database using Struts and Ajax
Step 1: Configuring  web.xml and struts-config.xml

Web.xml :
<web-app>
<display-name>AjaxTutorialonline</display-name> 
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

Step 2: Configuring Struts for Ajax Database Connection
Struts-config.xml

   <form-beans>
      <form-bean name="Ajaxtutorialonline" type="com.ajax.ExampleForm" />
   </form-beans>
   <action-mappings>
   <action path="/ajax" name="Ajaxtutorialonline" scope="request" type="ajax.tutorial.AjaxAction"/>
   </action-mappings>

Step 3:  
ExampleForm.java

package com.ajax;
import org.apache.struts.action.ActionForm;
public class ProfileForm extends ActionForm{
    private String empid=null;
    private String employee=null;
//Generate getter setter method
public String getEmpid() {
        return empid;
    }
  public void setEmpid (String empid) {
        this.empid= empid;
    }
public String getEmployee() {
        return employee;
    }
    public void setEmployee(String employee) {
        this.employee = employee;
    }

Step 4: Writing Action 
AjaxAction.java

package ajax.tutorial;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class AjaxAction extends Action
{
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res)
{
 String val = req.getParameter("id");
String stor=null;
     Connection con = null;
     PreparedStatement pst =null;
     ResultSet rs=null;
     response.setContentType("text/xml");
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     pst = con.prepareStatement("select Empid, EmpName where Empid=?");
        pst.setString(1,val);
     if(rs.next())
     {
      stor =rs.getString(1);
      response.getWriter().write(stor);
     }
  con.close();
  }catch(SQLException e)
   {
    
   }
}


Step 5: JavaScript AJAX
<script type="text/javascript">

function ajaxDB()
{
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();

}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
var x= document.getElementById("hello");
x.innerHTML=xmlhttp.responseText;
}
xmlhttp.open("GET","/test.do",true);
xmlhttp.send();
}
</script>

<div id="hello"></div>


»»  Read More
Read more!