具体代码如下:
package com.query;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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;
public class QueryServlet extends HttpServlet {
private static final long serialVersionUID = -4196021128099327767L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/web";
String username = "root";
String password = "721123";
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>搜索结果显示页面</title></head>");
out.println("<body>");
out.println("<a href='"+ request.getContextPath()+ "/servlet/UpdateClassifyTypeServlet' title='新增接口信息'>【新增接口信息】</a> <a href='"+ request.getContextPath()+ "/servlet/QueryClassifyTypeServlet'>【返回搜索页面】</a>
");
");
out.println("<foot size='8'><strong>搜索结果信息显示:</strong></foot>
");
");
out.println("</body>");
out.println("</html>");
out.println("<center><table width='1162px' border='1' style='word-break: break-all;overflow:hidden;'>");
out.println("<tr bgcolor='#808080'><td width='50px' border='1' bordercolor='000000' align='center'><strong>序号</strong></td>");
out.println("<td width='150px' border='1' bordercolor='000000' align='center'><strong>标题</strong></td>");
out.println("<td width='100px' border='1' bordercolor='000000' align='center'><strong>分类</strong></td>");
out.println("<td width='100px' border='1' bordercolor='000000' align='center'><strong>类型</strong></td>");
out.println("<td width='400px' border='1' bordercolor='000000' align='center'><strong>URL</strong></td>");
out.println("<td width='200px' border='1' bordercolor='000000' align='center'><strong>备注</strong></td>");
out.println("<td width='100px' border='1' bordercolor='000000' align='center'><strong>操作</strong></td>");
out.println("<td width='65px' border='1' bordercolor='000000' align='center'><strong>创建时间</strong></td></tr>");
boolean hasParam = false;
String classify = request.getParameter("classify");
String type = request.getParameter("type");
String title = request.getParameter("title");
String sql = "select * from ADDINTERFACEINFO";
List<String> params = new ArrayList<String>();
if(classify!=null&&!"".equals(classify.trim())){
if(hasParam){
sql += "and classify like ?";
}else{
sql += " where classify like ?";
hasParam = true;
}
params.add(classify);
}
if(type!=null&&!"".equals(type.trim())){
if(hasParam){
sql += "and type like ?";
}else{
sql += " where type like ?";
hasParam = true;
}
params.add(type);
}
if(title!=null&&!"".equals(title.trim())){
if(hasParam){
sql += "and title like ?";
}else{
sql += " where title like ?";
hasParam = true;
}
params.add(title);
}
sql += " order by id desc";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username,password);
PreparedStatement pstmt = conn.prepareStatement(sql);
for(int i = 0; i < params.size(); i++) {
pstmt.setString(i+1, "%"+params.get(i)+"%"); //模糊查询
}
ResultSet rs = (ResultSet) pstmt.executeQuery();
boolean hasData = false;
while (rs.next()) {
printRow(out, rs);
hasData = true;
}
if(!hasData){
out.println("<td border='0' colspan='8' align='center'>没有搜索到相关数据。</td>");
}
rs.close();
pstmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
out.println("</table></center>");
}
public void printRow(PrintWriter out, ResultSet rs) throws SQLException {
out.println("<tr><td width='50px' bordercolor='000000'>"+ rs.getInt("id") + "</td>");
out.println("<td width='150px' bordercolor='000000'>"+ rs.getString("title") + "</td>");
out.println("<td width='100px' bordercolor='000000'>"+ rs.getString("classify") + "</td>");
out.println("<td width='100px' bordercolor='000000'>"+ rs.getString("type") + "</td>");
out.println("<td width='400px' bordercolor='000000'>"+ rs.getString("urladdress") + "</td>");
out.println("<td width='200px' bordercolor='000000'>"+ rs.getString("content") + "</td>");
out.println("<td width='65px' bordercolor='000000'><a href='"+ rs.getString("urladdress")+ "' target='_blank'>查看</a> <a href='/Interfaceinfo/servlet/UpdateInterfaceInfoServlet?id="+ rs.getInt("id") + "' target='_blank'>修改</a></td>");
out.println("<td width='100px' bordercolor='000000'>"+ rs.getString("time") + "</td></tr>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}