import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class Inquire extends HttpServlet
{ public void init(ServletConfig config) throws ServletException
{super.init(config);
}
//通过Post方法按名字查询记录:
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{ PrintWriter out=response.getWriter();
response.setContentType("text/html;charset=gb2312");//设置响应的MIME类型。
out.println("<HTML>");
out.println("<BODY>");
//获取提交的姓名:
String name=request.getParameter("name");
name = new String(name.getBytes("iso-8859-1"),"gb2312");
String number,xingming;
Connection con=null;
Statement sql=null;
ResultSet rs=null;
int math,english,physics;
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
try
{ con=DriverManager.getConnection("jdbc:odbc:sun","sa","111111");
sql=con.createStatement();
String condition="SELECT * FROM student WHERE 姓名 = "+"'"+name+"'";
rs=sql.executeQuery(condition);
out.print("<Table Border>");
out.print("<TR>");
out.print("<TH width=100>"+"Number");
out.print("<TH width=100>"+"Name");
out.print("<TH width=50>"+"Math");
out.print("<TH width=50>"+"English");
out.print("<TH width=50>"+"Phsics");
out.print("</TR>");
while(rs.next())
{ out.print("<TR>");
number=rs.getString(1);
out.print("<TD >"+number+"</TD>");
xingming=rs.getString(2);
out.print("<TD >"+xingming+"</TD>");
math=rs.getInt("数学成绩");
out.print("<TD >"+math+"</TD>");
english=rs.getInt("英语成绩");
out.print("<TD >"+english+"</TD>");
physics=rs.getInt("物理成绩");
out.print("<TD >"+physics+"</TD>");
out.print("</TR>") ;
}
out.print("</Table>");
con.close();
}
catch(SQLException e)
{
}
out.println("</BODY>");
out.println("</HTML>");
}
//通过Get方法按成绩查询记录:
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{ PrintWriter out=response.getWriter();
response.setContentType("text/html;charset=GB2312");//设置响应的MIME类型。
out.println("<HTML>");
out.println("<BODY>");
//获取提交的分数的最大值和最小值:
String englishmax=request.getParameter("englishmax");
String englishmin=request.getParameter("englishmin");
String mathmax=request.getParameter("mathmax");
String mathmin=request.getParameter("mathmin");
String number,xingming;
Connection con=null;
Statement sql=null;
ResultSet rs=null;
int math,english,physics;
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
try
{ con=DriverManager.getConnection("jdbc:odbc:sun","sa","111111");
sql=con.createStatement();
String eCondition="英语成绩 <= "+englishmax+" AND "+"英语成绩 >= "+englishmin;
String mCondition="数学成绩 <= "+mathmax+" AND "+"数学成绩 >= "+mathmin;
String condition="SELECT * FROM student WHERE "+mCondition+" and "+eCondition;
rs=sql.executeQuery(condition);
out.print("<Table Border>");
out.print("<TR>");
out.print("<TH width=100>"+"Number");
out.print("<TH width=100>"+"Name");
out.print("<TH width=50>"+"Math");
out.print("<TH width=50>"+"English");
out.print("<TH width=50>"+"Phsics");
out.print("</TR>");
while(rs.next())
{ out.print("<TR>");
number=rs.getString(1);
out.print("<TD >"+number+"</TD>");
xingming=rs.getString(2);
out.print("<TD >"+xingming+"</TD>");
math=rs.getInt("数学成绩");
out.print("<TD >"+math+"</TD>");
english=rs.getInt("英语成绩");
out.print("<TD >"+english+"</TD>");
physics=rs.getInt("物理成绩");
out.print("<TD >"+physics+"</TD>");
out.print("</TR>") ;
}
out.print("</Table>");
con.close();
}
catch(SQLException e)
{
}
out.println("</BODY>");
out.println("</HTML>");
}
}
评论8