代码
package com.sici.servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
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 javax.servlet.http.HttpSession;
import com.sici.javabean.biao;
/**
* Servlet implementation class Select2Servlet
*/
@WebServlet("/Select2Servlet")
public class Select2Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Select2Servlet() {
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
HttpSession session = request.getSession();
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//1代表模糊,2代表并或
String JGQC=new String();
String GKGLSX=new String();
String SZDY=new String();
String FRDB=new String();
String LXR=new String();
String JGSX=new String();
String JSXQMC=new String();
String minzu1=new String();
String sex1=new String();
String time1=new String();
String mianmao1=new String();
String leibie1=new String();
String name2=new String();
String minzu2=new String();
String sex2=new String();
String time2=new String();
String mianmao2=new String();
String leibie2=new String();
// 注意数组从1开始
//t:输入的值 ,s模糊, m并且 ,c:机构全称,
String t[]=new String[15];
String s[]=new String[15];
String m[]=new String[15];
String c[]=new String[15];
for(int i=1;i<=14;i++)
{
t[i]=request.getParameter("t"+i);
s[i]=request.getParameter("s"+i);
c[i]=request.getParameter("c"+i);
m[i]=request.getParameter("m"+i);
}
for(int i=1;i<=14;i++)
{
if(c[i]==null)
c[i]="";
}
// 注意数组从1开始
//zh[][1]为姓名集,zh[][2]为精确或模糊,zh[][3]为并或非
String zh[][]=new String [15][4];
for(int i=1;i<=14;i++)
{
if(c[i].equals("JGQC"))
{
zh[1][0]="JGMC";zh[1][1]=t[i];zh[1][2]=s[i];zh[1][3]=m[i];
}
if(c[i].equals("GKGLSX"))
{
zh[2][0]="GKGLBM";zh[2][1]=t[i];zh[2][2]=s[i];zh[2][3]=m[i];
}
if(c[i].equals("SZDY"))
{
zh[3][0]="SZDY";zh[3][1]=t[i];zh[3][2]=s[i];zh[3][3]=m[i];
}
if(c[i].equals("FRDB"))
{
zh[4][0]="FRDB";zh[4][1]=t[i];zh[4][2]=s[i];zh[4][3]=m[i];
}
if(c[i].equals("LXR"))
{
zh[5][0]="LXR";zh[5][1]=t[i];zh[5][2]=s[i];zh[5][3]=m[i];
}
if(c[i].equals("JGSX"))
{
zh[6][0]="JGSX";zh[6][1]=t[i];zh[6][2]=s[i];zh[6][3]=m[i];
}
if(c[i].equals("JSXQMC"))
{
zh[7][0]="JSXQMC";zh[7][1]=t[i];zh[7][2]=s[i];zh[7][3]=m[i];
}
if(c[i].equals("GJZ"))
{
zh[8][0]="GJZ";zh[8][1]=t[i];zh[8][2]=s[i];zh[8][3]=m[i];
}
if(c[i].equals("NTRZJZE"))
{
zh[9][0]="NTRZJZE";zh[9][1]=t[i];zh[9][2]=s[i];zh[9][3]=m[i];
}
if(c[i].equals("JSXQJJFS"))
{
zh[10][0]="JSXQHZMS";zh[10][1]=t[i];zh[10][2]=s[i];zh[10][3]=m[i];
}
if(c[i].equals("KJHDLX"))
{
zh[11][0]="KJHDLX";zh[11][1]=t[i];zh[11][2]=s[i];zh[11][3]=m[i];
}
if(c[i].equals("XKFL"))
{
zh[12][0]="XKFL";zh[12][1]=t[i];zh[12][2]=s[i];zh[12][3]=m[i];
}
if(c[i].equals("XQJSSSLY"))
{
zh[13][0]="XQJSSSLY";zh[13][1]=t[i];zh[13][2]=s[i];zh[13][3]=m[i];
}
if(c[i].equals("XQJSYYHY"))
{
zh[14][0]="XQJSYYHY";zh[14][1]=t[i];zh[14][2]=s[i];zh[14][3]=m[i];
}
}
for(int i=1;i<=14;i++)
{
if(zh[i][1]==null)
zh[i][1]="";
}
boolean flag=false;
//给出parmas
ArrayList<String> params = new ArrayList<String>();
StringBuffer sb=new StringBuffer();
sb.append("select * from t_dcwjxx where 1 = 1 ");
for(int i=1;i<=14;i++)
{
if(!zh[i][1].equals("")&&zh[i][1]!=null)
{
if(zh[i][3].equals("b"))
{
sb.append(" and "+zh[i][0]+" ");
}else if(zh[i][3].equals("h"))
{
sb.append(" or "+zh[i][0]+" ");
}else
{
sb.append(" and "+zh[i][0]+" ");
sb.append(" <> ? ");params.add(zh[i][1]);
continue;
}
if(zh[i][2].equals("jq"))
{sb.append(" = ? ");params.add(zh[i][1]);}
else
{sb.append(" like ? ");params.add("%" +zh[i][1]+ "%"); }
}
}
System.out.println(sb.toString());
System.out.println(params);
String url = "jdbc:mysql://localhost:3306/db_diaoyan?&useSSL=false&serverTimezone=UTC&useUnicode=yes&characterEncoding=utf8";
Connection conn = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url, "root", "root");
} catch (ClassNotFoundException e) {
response.getWriter().print("加载驱动失败");
} catch (SQLException e) {
response.getWriter().print("连接数据库失败");
}
ArrayList list = new ArrayList();
try {
String sql =sb.toString();
ps = conn.prepareStatement(sql);
for(int i=1;i<=params.size();i++)
{
ps.setString(i, params.get(i-1));
System.out.println(params.get(i-1));
}
ResultSet rs = ps.executeQuery();
while(rs.next())
{
int wJID = rs.getInt("WJID");
int sFSH = rs.getInt("SFSH");
String jGMC = rs.getString("JGMC");
int qSXQNF = rs.getInt("QSXQNF");
int jZXQNF = rs.getInt("JZXQNF");
biao use = new biao(wJID, sFSH, jGMC, qSXQNF, jZXQNF);
list.add(use);
}
} catch (SQLException e) {
response.getWriter().print("修改失败");
}
try {
if (ps != null) {
ps.close();
ps = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception e) {
response.getWriter().print("关闭失败");
}
session.setAttribute("list",list);
request.getRequestDispatcher( "find2.jsp").forward(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}