其他的很简单,只把重要的几个方法写下来
list代码
public static List<CabinBill> getResultList(String ls_assistant_no) throws SQLException{ String sql="select * from Table_Job_Bill where AssistantNO='"+ls_assistant_no+"'"; DBConnection dbconn = new DBConnection(); ResultSet rs = dbconn.executequery(sql); System.out.println(sql); List<CabinBill> rtn=new ArrayList<CabinBill>(); while(rs.next()){ CabinBill cabinbill=new CabinBill(); cabinbill.setJobID(rs.getString("JobID")); cabinbill.setAssistantNO(rs.getString("AssistantNO")); cabinbill.setFeeName(rs.getString("FeeName")); cabinbill.setPE(rs.getString("PE")); rtn.add(cabinbill); } return rtn; }
json代码
返回一条记录的时候
public static JSONObject cabinbillToJSON(CabinBill cabinbill) throws JSONException,SQLException{ JSONObject cabinbill_json = new JSONObject(); if((cabinbill!=null)&&(cabinbill.getJobID()!=null)){ cabinbill_json.put("JobID", cabinbill.getJobID()); cabinbill_json.put("AssistantNO", cabinbill.getAssistantNO()); cabinbill_json.put("FeeName", cabinbill.getFeeName()); cabinbill_json.put("PE", cabinbill.getPE()); }else{ cabinbill_json.put("Err","根据给定条件没有找到查询结果!"); } return cabinbill_json; }
返回多条记录的时候
public static JSONObject cabinbillToJSON1(List<CabinBill> list) throws JSONException,SQLException{ JSONObject cabinbill_json = new JSONObject(); try { for (Iterator<CabinBill> iterator = list.iterator(); iterator.hasNext();) { CabinBill cabinbill= iterator.next(); cabinbill_json.put("JobID", cabinbill.getJobID()); cabinbill_json.put("AssistantNO", cabinbill.getAssistantNO()); cabinbill_json.put("FeeName", cabinbill.getFeeName()); cabinbill_json.put("PE", cabinbill.getPE()); } } catch (JSONException e) { e.printStackTrace(); } System.out.println(cabinbill_json.length()); return cabinbill_json; }
在servlet中的代码
import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONException; import org.json.JSONObject; import com.server.model.CabinBill; /** * Servlet implementation class CabinBillQuery */ public class CabinBillQuery extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public CabinBillQuery() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ @SuppressWarnings("static-access") protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setCharacterEncoding("UTF-8"); JSONObject cabinbill_json = null; try{ String ls_assistant_no=new String(request.getParameter("AssistantNO").getBytes("iso-8859-1"),"UTF-8"); List<CabinBill> list=new ArrayList<CabinBill>(); list=CabinBill.getResultList( ls_assistant_no); System.out.println(list.size()); String jsonstr="["; for(int i=0;i<list.size();i++){ cabinbill_json=list.get(i).cabinbillToJSON(list.get(i)); if(i<list.size()-1){jsonstr=jsonstr+cabinbill_json.toString()+",";} if(i==list.size()-1){jsonstr=jsonstr+cabinbill_json.toString();} //response.getWriter().println(cabinreserve_json.toString()+"\r\n"); } jsonstr=jsonstr+"]"; response.getWriter().println(jsonstr); System.out.println(response.getBufferSize()); return ; } catch (SQLException e1) { e1.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } } }