packagecom.forbidden.airtransport.servlet;importjava.io.DataOutputStream;importjava.io.IOException;importjava.io.OutputStream;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Timestamp;importjava.text.SimpleDateFormat;importjava.util.Date;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.forbidden.airtransport.util.RegCode;/*货主找车Servlet
* @author rosen jiang
* @since 2005-12*/publicclassGoodsFindAutoextendsHttpServlet {/*** 构造函数*/publicGoodsFindAuto() {super();
}/*** 销毁方法*/publicvoiddestroy() {super.destroy();
}/*** 货主找车功能
*
*@paramrequest HttpServletRequest
*@paramresponse HttpServletResponse
*@throwsServletException
*@throwsIOException*/publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {//电话区号实际城市名互转类RegCode rc=newRegCode();//用户名,用于记载查询日志。String userName=request.getParameter("userName");//页数intpage=0;
String strPage=request.getParameter("page");//每页条数intperPage=0;
String strPerPage=request.getParameter("perPage");//车辆出发地String autoFrom=request.getParameter("autoFrom");//车辆目的地String autoTarget=request.getParameter("autoTarget");//发布日期String pubDate=request.getParameter("pubDate");//查询日志String writeLog="insert into search_log (userName,flag,f_rom,target,serDate)"+"values (?,?,?,?,?)";//计算总记录数String countRow="select count(*) from auto_back where autoFrom=? and autoTarget=?"+"and TO_DAYS(regDate)=TO_DAYS(?)";//查询结果String finSql="select * from auto_back where autoFrom=? and autoTarget=?"+"and TO_DAYS(regDate)=TO_DAYS(?) limit ?,?";//连接信息Connection conn=null;
PreparedStatement stm=null;
ResultSet rs=null;//构建输出流response.setContentType("application/octet-stream");
OutputStream ops=response.getOutputStream();
DataOutputStream dos=newDataOutputStream(ops);try{if(userName==null||strPage==null||strPerPage==null||autoFrom==null||autoTarget==null||pubDate==null){
dos.writeUTF("非法请求!");
}else{
page=Integer.parseInt(strPage);
perPage=Integer.parseInt(strPerPage);//电话区号转换到实际地址autoFrom=rc.convent(autoFrom);
autoTarget=rc.convent(autoTarget);//获取连接conn=DriverManager.getConnection("proxool.automy");//记录请求信息stm=conn.prepareStatement(writeLog);
stm.setString(1,userName);
stm.setString(2,"2");
stm.setString(3,autoFrom);
stm.setString(4,autoTarget);
Date sDate=newDate();
stm.setTimestamp(5,newTimestamp(sDate.getTime()));
stm.executeUpdate();//计算结果集总数stm=conn.prepareStatement(countRow);
Date date=newDate(Long.parseLong(pubDate));
SimpleDateFormat formatter=newSimpleDateFormat("yyyy-MM-dd");
String Fdate=formatter.format(date);
stm.setString(1,autoFrom);
stm.setString(2,autoTarget);
stm.setString(3,Fdate);
rs=stm.executeQuery();
rs.next();introw=rs.getInt(1);if(row==0){
dos.writeUTF("未找到你要匹配的数据。");
}else{//进行查询stm=conn.prepareStatement(finSql);introws=0;if(page==1){
rows=0;
}else{
rows=perPage*((page-1)/4);
}
stm.setString(1,autoFrom);
stm.setString(2,autoTarget);
stm.setString(3,Fdate);
stm.setInt(4,rows);
stm.setInt(5,perPage);
rs=stm.executeQuery();
dos.writeUTF("");//设置前两个字节为空dos.writeUTF(row+"");//记录总数if(row%perPage!=0){
dos.writeUTF(Integer.toString(1+(row/5)));//总页数}else{
dos.writeUTF(Integer.toString((row/5)));//总页数}
dos.writeUTF(Integer.toString(page));//当前页while(rs.next()){//创建业务数据输出流String resString=rs.getString(3)+"&"+rs.getString(4)+"&"+rs.getString(6)+"&"+rs.getString(5)+"&"+rs.getString(9)+"&"+rs.getString(8)+"&"+rs.getString(10);
dos.writeUTF(resString);
}
}
}//数据长度response.setContentLength(dos.size());
}catch(Exception e) {
e.printStackTrace();
dos.writeUTF("服务器异常!"+e.toString());
}finally{try{
rs.close();
stm.close();
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
dos.flush();
dos.close();
ops.close();
}
}/*** post方法
*
*@paramrequest HttpServletRequest
*@paramresponse HttpServletResponse
*@throwsServletException
*@throwsIOException*/publicvoiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {
}/*** 初始化方法
*
*@throwsServletException*/publicvoidinit()throwsServletException {
}
}