package servlet;
import java.io.IOException;
//import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DisplayServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection con;
String[][] str = new String[25][9];
Integer count=new Integer(0); //定义count初始化
String start=request.getParameter("start");// 接收起始站start到start
String end=request.getParameter("end"); //接收到达站end 到end
start=new String (start.getBytes("ISO8859-1"),"UTF-8");
end=new String (end.getBytes("ISO8859-1"),"UTF-8");
System.out.println(start);
System.out.println(end);
// 链接数据库
try {
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=db", "sa","sa");
Statement s;
s = con.createStatement();
ResultSet rs;
rs = s.executeQuery("select count(*) as count1 from lieche where Start like '%"+start+"%'"+"and Endd like '%"+end+"%'");
// 吧满足条件的起始站和到达站的 个数用count1表示输出
rs.next();
System.out.println(rs.getInt("count1"));
count=Integer.valueOf(rs.getInt("count1"));
System.out.println(count.intValue());
rs=s.executeQuery("select * from lieche where Start like '%"+start+"%'"+"and Endd like '%"+end+"%'");//查询满足条件的
int i = 0;
while (rs.next()) { //把数据库的信息放在数组里输出
str[i][0] = rs.getString(1);
str[i][1] = rs.getString(2);
str[i][2] = rs.getString(3);
str[i][3] = rs.getString(4);
str[i][4] = rs.getString(5);
str[i][5] = rs.getString(6);
str[i][6] = rs.getString(7);
str[i][7] = rs.getString(8);
str[i][8] = rs.getString(9);
i++;
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("infor", str); //把 str值用infor表示
request.setAttribute("col",count); //把 count值 用 col表示
request.getRequestDispatcher("../result.jsp").forward(request, response); // 调转页面到result.jsp
}
public void init() throws ServletException {
}
}