Softwares
Thursday, December 30, 2010
0
comments
following softwares uploading soon
mysql
sqlyog
mysqlconnector.jar file
mysql
sqlyog
mysqlconnector.jar file
Labels:
ajax
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name> GetStates </servlet-name> <servlet-class> GetStates </servlet-class> </servlet> <servlet-mapping> <servlet-name> GetStates </servlet-name> <url-pattern> /GetStates </url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file> index.jsp </welcome-file> </welcome-file-list> </web-app>
package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* * @author Jagadeesh
* if you like this example please follow my blog and
* submit your comments
*/
public class DBClass {
Connection con = null;
public Connection createConnection() throws
ClassNotFoundException,SQLException
{
//loading the driver. to use this driver you need to include
//mysql connector.jar files in tomcat server lib
//install mysql and sql yog and copy jar files and import
//database from sqlyog uisng tools-->import option
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection
("jdbc:mysql://localhost:3306/countrydb", "root", "root");
return connection;
}
public String getStates(String country)throws
SQLException, ClassNotFoundException
{
//creating connection by calling above method
Connection con = createConnection();
//using preparedstatement we are executing sql query(getting states)
PreparedStatement pstmt = con.prepareStatement
("select state from countries where country=?");
pstmt.setString(1, country);
ResultSet rs = pstmt.executeQuery();
String states="";
while(rs.next())
{
//getting states from database
states=rs.getString(1);
}
//this method returns string of states
return states;
}
public String getDistricts(String getstate)throws
SQLException, ClassNotFoundException
{
//creating connection by calling above method
Connection con = createConnection();
//using preparedstatement we are executing sql query(getting districts)
PreparedStatement pstmt = con.prepareStatement
("select Districtsnames from districts where state=?");
pstmt.setString(1, getstate);
ResultSet rs = pstmt.executeQuery();
String districts="";
while(rs.next())
{
districts=rs.getString(1);
}
//this method return districts as string.we will call this method
//in servlet
return districts;
}
}
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import database.DBClass;
/**
* @author Jagadeesh
* if you like this example please submit your comments and suggestions
* if you like this example please submit your comments and suggestions
* if you have any doubts you can post comments in blog comment form
*/
public class GetStates extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//creating object for database class
DBClass dbobj = new DBClass();
String result="";
String action = request.getParameter(("action"));
String selectedcountry = request.getParameter("country");
String selectedstate = request.getParameter(("state"));
try {
if(action.equals("getStates"))
{
//calling database method getstates and pass country
// as arguement
String state = dbobj.getStates(selectedcountry);
//dividing states using ","
String states[]= state.split(",");
for(int i=0;i<states.length;i++)
{
//adding states for selectbox options
result+="<option value="+states[i]+">"+states[i]+"</option>";
}
//sending result to jsp page.what ever we are
//inserting in out.println it will go back to browser(jsp page)
//as response
out.println(result);
}
if(action.equals("getDistricts"))
{
//calling database method getstates and pass selectedstate
// as arguement
String districts = dbobj.getDistricts(selectedstate);
//dividing districts using ","
String getdistricts[]= districts.split(",");
for(int i=0;i<getdistricts.length;i++)
{
//adding districts for selectbox options
result+="<option value="+getdistricts[i]+">"+getdistricts[i]+"</option>";
}
//sending result to jsp page.what ever we are
//inserting in out.println it will go back to browser(jsp page)
//as response
out.println(result);
}
}
catch (Exception e)
{
out.print(e);
}
finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
<%--
@author Jagadeesh
* if you like this example please submit your comments and suggestions
* if you like this example please submit your comments and suggestions
* if you have any doubts you can post comments in blog comment form
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<script>
function GetXmlHttpObject()
{
//creating xmlhttprequestobject.common method for any ajax application
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
//to get states
function getStates(country)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
else
{
//sending selected country to servlet
var url="GetStates?action=getStates&country="+country;
//creating callback method.here countrychanged is callback method
xmlHttp.onreadystatechange=countryChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
}
//call back fuction
function countryChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
//getting response from server(Servlet)
var showstates = xmlHttp.responseText;
//displaying response in select box by using that id
document.getElementById("state").innerHTML=showstates;
//to get district for selected state getting selectedstate
  // from selectbox using that selectboxid
var state = document.getElementById("state").value;
// to get the districts for selected state
getDistricts(state);
}
}
// to get districts
function getDistricts(state)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
else
{
//above we got selected state for selected country
//now we are getting districts for selected state
var url="GetStates?action=getDistricts&state="+state;
//creating callback method.here stateChanged is callback method
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
//getting response from server(Servlet) of available districts
var showdistricts = xmlHttp.responseText;
//displaying district in selectbox
document.getElementById("districts").innerHTML=showdistricts;
}
}
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form method="post" name="register" id="register">
<SELECT NAME="country" id="country"onchange="getStates(this.value)">
<OPTION VALUE="select">select
<OPTION VALUE="india">India
<OPTION VALUE="US">US
<OPTION VALUE="UK">UK
</SELECT>
<!-- empty select box to display states-->
<SELECT NAME="states" id="state"style="width:100px"
onchange="getDistricts(this.value)">
</SELECT>
<!-- empty select box to display districts-->
<SELECT NAME="districts" id="districts"style="width:100px" >
</SELECT>
</form>
</body>
</html>