使用2个QueryParams返回需要2个不同变量的对象

时间:2014-12-05 14:25:57

标签: java html mysql

我正在编写一个使用MySQL,Javascript和HTML代码的特殊权限表单程序,它们都响应。我正在使用java中的单例模式访问和外观代码以及响应单例模式代码的服务代码来完成所有这些操作。

我正在尝试通过变量,courseDept&检索表单(AKA 1结果)。 courseNm。

这是我用来完成所有这些的代码片段:

FormDataFacade.java代码段:

@Path("/specialPermissions/sp")
@GET
@Produces("text/plain")
public Response getSpecialPermissionFormByDeptAndRoomNm(@QueryParam("courseDept") String theDept, @QueryParam("courseNm") String theNm) 
throws NamingException, SQLException, ClassNotFoundException
{
//Referenciation to FormDataFacade class.
FormDataFacade iFacade = FormDataFacade.getInstance();

int intNm = 0;
try {
intNm = Integer.parseInt(theNm);
}catch (NumberFormatException FAIL) {
intNm = 1;
}

//Aiming for forms with matching departments & room numbers by calling FormDataFacade
//method, getSpecialPermissionFormByDeptAndRoomNm.
SpecialPermissionForms[] orgaForm = iFacade.getSpecialPermissionFormByDeptAndRoomNm(theDept, intNm);

//Json String Representation...
if (orgaForm != null)
{
 Gson neoArcadia = new Gson();
 String result = neoArcadia.toJson(orgaForm);

 //Json String added to response message body...
 ResponseBuilder rb = Response.ok(result, MediaType.TEXT_PLAIN);
 rb.status(200); //HTTP Status code has been set!
 return rb.build(); //Creating & Returning Response.
}
else
{ //In case of finding no form data for the procedure...
 return Response.status(700).build();
}
}

FormDataServices.java代码段:

public SpecialPermissionForms[] getSpecialPermissionFormByDeptAndRoomNm(String theDept, int theNm) throws SQLException, ClassNotFoundException
{
 Connection con = zeon.getConnection();
 PreparedStatement pstmt = con.prepareStatement("SELECT formID, studentName, courseDept, courseNm, semester, year, reasonCode FROM spforms WHERE courseDept = ? & courseNm = ?");
 pstmt.setString(1, theDept);
 pstmt.setInt(2, theNm);
 ResultSet rs = pstmt.executeQuery();

 SpecialPermissionForms[] neoForm = new SpecialPermissionForms[50];
 int current = 0;

  while (rs.next())
  {
   int formID3 = rs.getInt("formID");
   String studentName3 = rs.getString("studentName");
   String courseDept3 = rs.getString("courseDept");
   String courseNm3 = rs.getString("courseNm");
   String semester3 = rs.getString("semester");
   int year3 = rs.getInt("year");
   String reasonCode3 = rs.getString("reasonCode");
   SpecialPermissionForms neo = new SpecialPermissionForms(formID3, studentName3, courseDept3, courseNm3, semester3, year3, reasonCode3);
   neoForm[current] = neo;
   current++;
  }

  if (current > 0)
  {
   neoForm = Arrays.copyOf(neoForm, current);
   return neoForm;
  }

  else
  {
   return null;
  }
}

两段java代码响应的Forms.html代码段:

$("#btnOneName").click(function() {
alert("clicked");
var inputId1=document.getElementById("t_specialFormCourseDept").value;
var inputId2=document.getElementById("t_specialFormCourseNm").value;
var theData = "courseDept=" + inputId1 + "&" + "courseNm=" + inputId2;
alert("Sending: " + theData);
var theUrl = "http://localhost:8080/onlineforms/services/enrollment/specialPermissions/sp?courseDept=&courseNm="+ theData;
$.ajax( {
url: theUrl,
type: "GET",
dataType: "text",
success: function(result) {
alert("success");
var neoForm = JSON.parse(result);
alert(neoForm);
var output="<h3>Current Form Lists:</h3>";
output += "<ul>";
for (var current = 0; current < neoForm.length; current++)
{
output += "<li>" + neoForm[current].formID + ": " + neoForm[current].studentName + " (" + neoForm[current].courseDept + " - " + neoForm[current].courseNm + ") " + 
        " (" + neoForm[current].semester +  " - " + neoForm[current].year + ") " + " - " + neoForm[current].reasonCode + "</li>";
}
output += "</ul>";
alert(output);
$("#p_retrieveOneName").html(output); 
},
error:function(xhr) {
alert("error");
$("#p_retrieveOneName").html("Error:"+xhr.status+" "+xhr.statusText);}
} );
});
});

现在,当我在成功编译之后在我的web服务中测试此代码时,它确实有效,但它会检索所有内容,包括我搜索的特定结果 - 我只想返回我专门搜索的结果和没有其他的。在这些代码片段中,我到底做错了什么? 我们非常欢迎任何正确方向的建议或步骤。

0 个答案:

没有答案