$.ajax() メソッドを使用して複数の選択ボックスを動的に設定しようとしています....ここに私のhtmlコードがあります......
<select id="imageSize" name="ImageSizeId"></select>
<select id="circulation" name="circulationId"></select>
これが私のサーブレットコードです.....
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/json;charset=utf-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
JSONObject usesfactors = new JSONObject();
JSONArray jArraysize = new JSONArray();
JSONArray jArraycirculation = new JSONArray();
Statement stmtsize,stmtcirculation;
ResultSet rssize,rscirculation;
int category_id = Integer.parseInt(request.getParameter("CategoryId"));
int format_id = Integer.parseInt(request.getParameter("FormatId"));
String size,display,des;
int sizeid;
try {
if(category_id == 2 && format_id == 201){
// Get image size factors
String sql1="SELECT SizeId,Size FROM Size WHERE Category_id = "+category_id+" AND Format_id = "+format_id+" ";
PreparedStatement ps1 = conn.prepareStatement(sql1);
rssize = ps1.executeQuery() ;
if( rssize!=null){
System.out.println("Not Null");// its printing even if resultset rssize has no records.......why?
while(rssize.next())
{
System.out.println("inside resultset");
JSONObject jobj = new JSONObject();
sizeid = rssize.getInt("SizeId");
size=rssize.getString("Size");
System.out.println(size);
jobj.put("SizeId", sizeid);
jobj.put("Size", size);
jArraysize.add(jobj);
}
usesfactors.put("Size", jArraysize);
rssize.close();
}
else{
System.out.println("Does not have Size factors");
}
// Get image circulation factors
String sql2="SELECT circulationId,circulation FROM Circulation WHERE Category_id = "+category_id+" AND Format_id = "+format_id+" ";
PreparedStatement ps2 = conn.prepareStatement(sql2);
rscirculation = ps2.executeQuery() ;
if(rscirculation!=null){
while(rscirculation.next())
{
JSONObject jobj = new JSONObject();
display = rscirculation.getString("DisplayName");
des=rscirculation.getString("Description");
jobj.put("DisplayName", display);
jobj.put("Description", des);
jArraycirculation.add(jobj);
}
usesfactors.put("Circulation", jArraycirculation);
rscirculation.close();
}
else{
System.out.println("Does not have Circulation factors");
}
out.println(usesfactors);
}
私は空のjson結果を取得しています....?何が間違っていますか? {"Size":[],"Circulation":[]} この stmt "usesfactors.put("Size", jArraysize);" を実行したくありません 結果セットrssizeがnullの場合、結果セットrssizeにレコードがない場合でも、このstmtが実行されます....要するに、結果セットrssizeにレコードがある場合にのみ、jsonオブジェクト(usesfactors)にjson配列(jArraysize)を入れたいです。