不好意思,粘贴错误,以下是正确代码:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;public class TestDB extends HttpServlet{
    public void doGet(HttpServletRequest request,
      HttpServletResponse response)
  throws ServletException,IOException{
 response.setContentType("text/html");
 PrintWriter out = response.getWriter();
 showResult(request,out);
}private void showResult(HttpServletRequest request,PrintWriter out){
     String sname = request.getParameter("sname");
   try
  {
Class.forName("org.gjt.mm.mysql.Driver");
String dburl = "jdbc:mysql://localhost/test";
Connection c = DriverManager.getConnection(dburl, "", "");
Statement stat = c.createStatement();
ResultSet result = stat.executeQuery("select Sno from student where Sname=sname"); 
   String Sno = result.getString(1);
   result.next();
   out.println("<html>");
out.println("<head><title>Result</title></head>");
out.println("<body>");
out.println("<h1>");
out.println("The Student's ID is:");
out.println("</h1>");
out.println("<h1>" );
out.println(Sno);
out.println("</h1>");
out.println("</body></html>");
   result.close();
  }
catch(SQLException e)
  {
System.out.println(e);
  }
catch(ClassNotFoundException e)
  {
    System.out.println(e);
  }
      catch(Exception e)
      {
         System.out.println(e);}   
}
}