📄 vetservlet.java
字号:
package com.hellofdeath.vet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class VetServlet extends HttpServlet {
private static final long serialVersionUID = 3336070555975125762L;
public void init() throws ServletException {
}
public VetServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GBK");
request.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
session=request.getSession();
vd=new Vector<VetDao>();//必须在这里初始化,否则再次查询会加上原有的元素
name = request.getParameter("vetName");// 获取医师的姓名
spec = request.getParameter("vetSpec");// 获取技能的名称
if (name.length() == 0 && spec.length() > 0) {// 判断医师和技能的输入 是否有为空的情况
this.querySpec();
} else if (name.length() > 0 && spec.length() == 0) {
this.queryName();
} else if (name.length() > 0 && spec.length() > 0) {
this.queryBoth();
}
if (!existed) {
out.println("<p align=center>抱歉,您要求的查询没有得到结果.请单击" +
"<a href=\"/PetClinique/vet/vetSearch.jsp\">返回</a>继续查询");
} else {
//将工医师的信息存储到 request的vet_spec 属性中
//并转发到显示页面
session.setAttribute("vet_spec", vd);
response.sendRedirect("/PetClinic/vet/vetSearch_name.jsp");
}
out.flush();
out.close();
}
public void queryName() { // 只输入了医师的名字
strSql = "select a.name as 'specialty',a.id as'spec_id',c.id as 'vet_id',c.name as 'vet' from specialties a,vet_specialties b,vets c "
+ "where a.id=b.specialty_id and b.vet_id=c.id and b.vet_id "
+ "in(select id from vets where name like '%" + name + "%')";
this.addVector();
}
public void querySpec() {// 只输入了技能的名字
strSql = "select a.name as 'specialty',a.id as'spec_id',c.id as 'vet_id',c.name as 'vet' from specialties a,vet_specialties b,vets c "
+ "where a.id=b.specialty_id and b.vet_id=c.id and b.specialty_id "
+ "in(select id from specialties where name like '%"
+ spec
+ "%')";
this.addVector();
}
public void queryBoth() {// 输入医师和技能的名字
strSql = "select a.name as 'specialty',a.id as'spec_id',c.id as 'vet_id',c.name as 'vet' from specialties a,vet_specialties b,vets c "
+ "where a.id=b.specialty_id and b.vet_id=c.id and b.specialty_id "
+ "in(select id from specialties where name like '%"
+ spec
+ "%')"
+ " and b.vet_id in(select id from vets where name like '%"
+ name + "%')";
this.addVector();
}
public void addVector() {// 将信息加入向量
rs = dbu.getResultSet(strSql);
try {
while (rs.next()) {
existed = true;
VetDao vet = new VetDao();//必须在这里初始化,否则后添加的元素会覆盖前面的元素的值
vet.setSpecId(rs.getInt("spec_id"));
vet.setSpecName(rs.getString("specialty"));
vet.setVetId(rs.getInt("vet_id"));
vet.setVetName(rs.getString("vet"));
vd.add(vet);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
existed = false;
}
dbu.close();// 关闭数据库的连接
}
private HttpSession session=null;
private boolean existed = false;
private Vector<VetDao> vd;
private String name = null;
private String spec = null;
private String strSql = null;
private com.hellofdeath.tool.DBUtils dbu = new com.hellofdeath.tool.DBUtils();
private ResultSet rs = null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -