Wednesday 1 February 2012

JDBC-AJAX-Chained List Using JSON

Today(1-2-2012), I developed a chained list demo using json.js library

and raw AJAX. For this demo I required

1.combo1 -to show state names 2.List1 -to populate cities according

to the selected state. 3.List2 -the selected city in List1

to be added to list2

To store these state and cities I needed a database table. So I created a table with two fields 1.state,2.city

Then, I used 3 buttons,a)for listing the cities in list1

b) to add city to List2

c) to Remove city from List2

When user clicks “List the cities” button, the AJAX will call “jsondemo.jsp” page which gets the selected state as an input .

Using this input we form the sql query as

“select *from table1 where state=’”+selectedState+”’”

This will return the Resultset. In this ResultSet, we need the second column only which contains cities name in accordance with given state.

Then we have to iterate this String to form JSONString. Then return this String to ajax code.

(Reference: Ajax On Java -Steven douglas olson,Orielly publishers(page 49))

For populating the cities in List1 we need to access database with the help of JSP. for other remaining two tasks we can use simple javascript code.

I tested this code in IE. I got correct result. I tried this same code in Firefox and Chrome. But, It didn’t work. Though the object is initialized correctly, data could not be populated in Lists. I must find out!