I am trying to get values from database using servlet
in which i am using JSON
object to send the data to my JSP
in my JSP
, jQuery
handles the receiving data.i am able to get data but just the last value of the database not all the values here is all my code any help is Thankful.
jQuery:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#country_id").change(function() {
var xyz = $("option:selected").val();
var Myselect = $('#state_ref');
$.getJSON("../Retrive_country?stateadd_1=none",
{countryREF : xyz } ,function(data){
$('#state_ref').empty();
$.each(data, function(index, state){
$("<option></option>")
.attr("value", state.stateId).text(state.stateName)
.appendTo('#state_ref');
});
});//end get
});
});
</script>
Servlet :
String sql1 = "SELECT * FROM state WHERE country_ref="+countryref+"
PreparedStatement pst1 = db.getConnection().prepareStatement(sql1);
ResultSet j = pst1.executeQuery();
JSONObject obj = new JSONObject();
pw.println("[");
while (j.next()) {
state_id = j.getString(1);
state = j.getString(2);
country_ref = j.getString(3);
obj.put("stateId", state_id);
obj.put("stateName", state);
}
pw.println(obj);
pw.println("]");
JSP:
<div class="span2 clear">
<select name="country_id" id="country_id">
<option>-select-</option>
<option id="blabbb">america</option>
<option id="blabbb">UK</option>
<option id="blabbb">Africa</option>
</select></div>
<div class="span2 clear">
<select name="state_ref" id="state_ref">
<option ></option>
</select></div>
when i try to console.log(data);
here is the result
here i am getting only one value(last value of column) instead of getting all the values from database.