数据库中返回多条记录,写成list形式,并且将list转换成json形式

其他的很简单,只把重要的几个方法写下来

list代码

public static List 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 rtn=new ArrayList();
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 list) throws JSONException,SQLException{
JSONObject cabinbill_json = new JSONObject();
try {
for (Iterator 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 list=new ArrayList();
list=CabinBill.getResultList( ls_assistant_no);
System.out.println(list.size());
String jsonstr="[";
for(int i=0;i


你可能感兴趣的:(android学习笔记,数据库)