Java 多条件查询

                                       Java 多条件查询
开发工具与关键技术:java
作者:彭浩达
撰写时间:2019年 7月 4日

public void Rescome(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
int drugid = Integer.parseInt(request.getParameter(“drugid”));
String drugname = request.getParameter(“drugname”);

//PurDrugMed drugMed = null;
List drugMeds = crkservice.seltectdrug(drugid);

	request.setAttribute("drugmed", drugMeds);
	request.getRequestDispatcher("/jsp/Rescome.jsp").forward(request, response);
}

private String selectdrug = "select * from drug;
@Override
public List seltectdrug(int drugid,String drugname) {
// TODO Auto-generated method stub
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
List drugMeds = null;
PurDrugMed drugMed = null;

	try {
		con = DBUtil.getConnection();
		if(drugid != 0){

//当drugid 不为0时候在 selectdrug 后拼接 and drugid= + drugid
StringBuffer sqlString = new StringBuffer(selectdrug);
String str = " and drugid=" + drugid;
sqlString.append(str);
ps = con.prepareStatement(sqlString.toString());
}else if(drugname != null){
//当drugname 不为空时候在 selectdrug 后拼接 and drugname like + ‘% + durgname + %’
StringBuffer sqlString = new StringBuffer(selectdrug);
String str = " and drugname like " + “’%” + durgname+ “%’”;
sqlString.append(str);
}else{
ps = con.prepareStatement(selectdrug);
//都为空的时候就不拼接
}

		rs = ps.executeQuery();
		drugMeds = new ArrayList();
		while(rs.next()){
			drugMed = new PurDrugMed();
			drugMed.setDrugname(rs.getString("drugname"));
			drugMed.setSpecification(rs.getString("specification"));
			drugMed.setQuantity(rs.getBigDecimal("quantity"));
			drugMed.setRetailprice(rs.getBigDecimal("retailprice"));
			drugMed.setUnit(rs.getString("unit"));
			drugMed.setCountryzi(rs.getString("countryzi"));
			drugMed.setBatchnumber(rs.getString("batchnumber"));
			drugMed.setNumber(rs.getString("number"));
			drugMed.setMedicalname(rs.getString("medicalname"));
			drugMed.setCostprice(rs.getBigDecimal("costprice"));	drugMed.setLingshouz(rs.getBigDecimal("quantity").multiply(rs.getBigDecimal("retailprice")));
			drugMed.setChengbeiz(rs.getBigDecimal("quantity").multiply(rs.getBigDecimal("costprice")));
			drugMeds.add(drugMed);
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return drugMeds;
}

你可能感兴趣的:(shui)