今天完成了多条件查询的后台代码。
package servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import dao.SelectDao; import bean.Table; @WebServlet("/SelectServlet") public class SelectServlet extends HttpServlet { private static final long serialVersionUID = 1L; public SelectServlet() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String[] yhf; yhf=request.getParameterValues("yhf"); String[] name; name=request.getParameterValues("name"); String[] value; value=request.getParameterValues("value"); String[] jingmo; jingmo=request.getParameterValues("jingmo"); List<Table> table = SelectDao.getTable(yhf, name, value, jingmo, yhf.length); request.setAttribute("table", table); request.getRequestDispatcher("admin/alltable.jsp").forward(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import bean.Table; import util.DBUtil; public class SelectDao { public static List<Table> getTable(String yhf[],String name[],String value[],String jingmo[],int length) { System.out.println(Arrays.toString(yhf)); System.out.println(Arrays.toString(name)); System.out.println(Arrays.toString(value)); System.out.println(Arrays.toString(jingmo)); String sql = "select * from t_table where "; if(yhf[0].equals("binghan")) { if(jingmo[0].equals("jingque")) { sql +=name[0]+" = '"+value[0]+"'"; } else if(jingmo[0].equals("mohu")) { sql = sql + name[0]+" like '%"+value[0]+"%'"; } System.out.println("1:"+sql); } if(yhf[0].equals("buhan")) { if(jingmo[0].equals("jingque")) { sql = sql + "not " + name[0]+" = '"+value[0]+"'"; } else if(jingmo[0].equals("mohu")) { sql = sql + "not " + name[0]+" like '%"+value[0]+"%'"; } System.out.println("2:"+sql); } for(int i=1;i<length;i++) { if(yhf[i].equals("binghan")) { if(jingmo[i].equals("jingque")) { sql = sql + " and "+name[i]+" = '"+value[i]+"'"; } else if(jingmo[i].equals("mohu")) { sql = sql + " and "+name[i]+" like '%"+value[i]+"%'"; } else if(jingmo[i].equals("null")) { break; } System.out.println("3:"+sql); } else if(yhf[i].equals("huohan")) { if(jingmo[i].equals("jingque")) { sql = sql + " or "+name[i]+" = '"+value[i]+"'"; } else if(jingmo[i].equals("mohu")) { sql = sql + " or "+name[i]+" like '%"+value[i]+"%'"; } else if(jingmo[i].equals("null")) { break; } System.out.println("4:"+sql); } else if(yhf[i].equals("buhan")) { if(jingmo[i].equals("jingque")) { sql = sql + " not "+name[i]+" = '"+value[i]+"'"; } else if(jingmo[i].equals("mohu")) { sql = sql + " not "+name[i]+" like '%"+value[i]+"%'"; } else if(jingmo[i].equals("null")) { break; } System.out.println("5:"+sql); } } System.out.println("6:"+sql); List<Table> list = new ArrayList<>(); Connection conn = DBUtil.getConnection(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { Table table = new Table(); int wjid = rs.getInt("wjid"); String jsxqmc = rs.getString("jsxqmc"); String jgqc = rs.getString("jgqc"); String gkglbm = rs.getString("gkglbm"); String szdy = rs.getString("szdy"); String frdb = rs.getString("frdb"); String lxr = rs.getString("lxr"); String jgsx = rs.getString("jgsx"); int ntrjeze = rs.getInt("ntrjeze"); String jsxqjjfs = rs.getString("jsxqjjfs"); String kjhdlx = rs.getString("kjhdlx"); String xkfl = rs.getString("xkfl"); String xqjsssly = rs.getString("xqjsssly"); String xqjsyyhy = rs.getString("xqjsyyhy"); String glcs = rs.getString("glcs"); String xssh = rs.getString("xssh"); String bmsh = rs.getString("bmsh"); table.setWjid(wjid); table.setJsxqmc(jsxqmc); table.setJsxqjjfs(jsxqjjfs); table.setKjhdlx(kjhdlx); table.setGkglbm(gkglbm); table.setJgqc(jgqc); table.setLxr(lxr); table.setSzdy(szdy); table.setFrdb(frdb); table.setJgsx(jgsx); table.setNtrjeze(ntrjeze); table.setXkfl(xkfl); table.setXqjsssly(xqjsssly); table.setXqjsyyhy(xqjsyyhy); table.setGlcs(glcs); table.setXssh(xssh); table.setBmsh(bmsh); list.add(table); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } }
servlet层将前台收到的"yhf" "name" "value" "jingmo"的值传到dao层。
String sql = "select * from t_table where "; if(yhf[0].equals("binghan")) { if(jingmo[0].equals("jingque")) { sql +=name[0]+" = '"+value[0]+"'"; } else if(jingmo[0].equals("mohu")) { sql = sql + name[0]+" like '%"+value[0]+"%'"; } System.out.println("1:"+sql); } if(yhf[0].equals("buhan")) { if(jingmo[0].equals("jingque")) { sql = sql + "not " + name[0]+" = '"+value[0]+"'"; } else if(jingmo[0].equals("mohu")) { sql = sql + "not " + name[0]+" like '%"+value[0]+"%'"; } System.out.println("2:"+sql); } for(int i=1;i<length;i++) { if(yhf[i].equals("binghan")) { if(jingmo[i].equals("jingque")) { sql = sql + " and "+name[i]+" = '"+value[i]+"'"; } else if(jingmo[i].equals("mohu")) { sql = sql + " and "+name[i]+" like '%"+value[i]+"%'"; } else if(jingmo[i].equals("null")) { break; } System.out.println("3:"+sql); } else if(yhf[i].equals("huohan")) { if(jingmo[i].equals("jingque")) { sql = sql + " or "+name[i]+" = '"+value[i]+"'"; } else if(jingmo[i].equals("mohu")) { sql = sql + " or "+name[i]+" like '%"+value[i]+"%'"; } else if(jingmo[i].equals("null")) { break; } System.out.println("4:"+sql); } else if(yhf[i].equals("buhan")) { if(jingmo[i].equals("jingque")) { sql = sql + " not "+name[i]+" = '"+value[i]+"'"; } else if(jingmo[i].equals("mohu")) { sql = sql + " not "+name[i]+" like '%"+value[i]+"%'"; } else if(jingmo[i].equals("null")) { break; } System.out.println("5:"+sql); } }
dao层将接受到的数组的数据通过if语句拼装成一个完整的sql语句。
例如:
select * from t_table where jgqc = '石家庄铁道大学' or lxr like '%安振%'