2 qq 35198865 qq_35198865 于 2016.09.20 20:01 提问

servlet 怎么把数据库查到的东西传到HTML中

下面是我写的,不知道该怎么改了。。

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class Servlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        // 获取客户端发送的请求类型:GET/POST
    doPost(request, response);
        }
protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String responseBody = "Current request method is <B>:" + request.getMethod()
            + "</B> <br>" + new Date();
    // 将所有HTML响应数据通过网络回发到客户端。
    // 获取发送响应的PrintWriter对象
    PrintWriter out = response.getWriter();
    // 装在需要响应到客户端的响应数据
    out.println(responseBody);


try{
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");   //注册JDBC驱动
          Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=Stu_Man","sa","13361775956");  //链接数据库
          System.out.println("连接成功!"); //如果连接成功,控制台输出“连接成功!” 

          //查询数据
          String str2= "select Course.couid,counam, coutimes, couroom, courtea from Course,Student,Score where stunam ='张三' and  Student.stuid=Score.stuid and Score.couid=Course.couid";
          Statement stmt = con.createStatement();
          ResultSet rs = stmt.executeQuery(str2);
          response.setContentType("text/html;charset=utf-8");


          while (rs.next()) 
          {
         String a = rs.getString("couid");
         String b = rs.getString("counam");
         String c = rs.getString("coutimes");
         String d = rs.getString("couroom");
         String e = rs.getString("courtea");

         out.print("<td><input type=\"checkbox\" name=\"id\" value=\""+a+"\"></td>");
         out.print("<td>"+b+"</td>");
         out.print("<td>"+c+"</td>");
         out.print("<td>"+d+"</td>");
         out.print("<td>"+e+"</td>");
         out.print("</tr>");
                       }   
          out.print("</table></center>");
          out.println("  </BODY>");
          out.println("</HTML>");
          out.flush();
          out.close();

          //插入数据 
          String str1 = "insert  into Student values(107,'王九','男','95-05-02','技术4')";
          Statement stm = con.createStatement();
          int res= stm.executeUpdate(str1);
          if (res > 0) { 
           System.out.println("数据已成功插入!");
          }
          //删除数据
         PreparedStatement p = null;
            try {
         String str3 = "delete from Student where stunam ='王' ";
         p = con.prepareStatement(str3);
         int row = p.executeUpdate();
         System.out.println("成功删除了"+row+"行数据!");
                }
            catch (SQLException sqlE)
          {
         sqlE.printStackTrace();
          } 
            if (rs != null) {
               con.close();  //释放资源
            }
   } 
 catch (SQLException e) 
{

// e.printStackTrace();
System.out.println("数据执行错误!");
}

       catch(Exception e)
       {
          System.out.println("连接失败!");
       }  
    // 发送响应
    out.close();

}

}

5个回答

leiwuyu1
leiwuyu1   2016.09.20 22:03
已采纳

我的思路这样的 ,你把从数据库里得到的东西 放到一个map或者其他的也行 ,然后放到request,到页面里 再从request里拿出来,

guwei4037
guwei4037   Ds   Rxr 2016.09.20 20:09

在servlet的doget或者dopost方法中,将要显示的值放入request作用域中,request.setAttribute("list",list),然后跳转到jsp页面,在jsp页面request.getAttribute("list")

qq_35198865
qq_35198865 我就去这样用的不错
7 个月之前 回复
oSanYeCao1234567
oSanYeCao1234567   Ds   Rxr 2016.09.20 22:21

request.setAttribute("data",data),然后跳转到jsp页面,显示对应的属性值即可

Marksinoberg
Marksinoberg   Ds   Rxr 2016.09.21 13:22

通过域对象来存储,然后jsp页面使用el表达式即可获取相应名称对应的值了。

quasimodo_es
quasimodo_es   2016.09.21 16:07

servlet可以自己了解一下原理;
如果搞项目,找个spring mvc 小例子看看就行了,

Csdn user default icon
上传中...
上传图片
插入图片