csnd吃相太难看送妈去吧 2019-05-04 15:25 采纳率: 0%
浏览 429

如何将my eclipse查询MySQL的结果传给客户端。

下面是查询数据库的代码数据库
public boolean Seekaim(SeekUser seekid){

String word="select * from seeker where userID like '%"+seekid.getSeek_name()+"%'";//生成一条sql语句
try {

    Statement statement=connection.createStatement();//创建一个Statement对象
    ResultSet resultSet=statement.executeQuery(word);//查询数据可通过Statement接口的executeQuery()方法执行SQL语句
    //executeQuery()方法用于执行SELECT查询语句,将放回一个ResultSet型的结果集。通过遍历查询结果集的内容,才可获取SQL语句执行的查询结果。
    //定义三个string类型的量
    String id,name;
    while(resultSet.next()){//遍历结果集   
        id=resultSet.getString("userID");
        System.out.println("查询的用户是"+id);
        name=resultSet.getString("userName");
        System.out.println("查询用户的昵称是"+name);
         }

    /*
    1:Result接口类似于一个临时表,用来暂时存放数据库查询操作所获得的结果集。
    2:PreparedStatement接口中的excuteQuery()方法,在此PreparedStatement对象执行sql查询语句,返回结果为查询结果集Result对象
    3:next()将指针向下移一行
    4:ResultSet对象的getXXX()方法可获取查询结果集中数据。由于ResultSet中保存的数据是表的形式,因此可通过使用getXXX()方法指定列的序号与列的名称。
       id=rs.getInt("id");//获得id
       username=rs.getString(2);//
       password=rs.getString("password");//
       age=rs.getInt(4);//
       sex=rs.getInt(5);//
                  仔细体会就可以明白,getXXX(参数);参数既可以是列的名称还可以是第几列的数字。
     */
    if (resultSet.next())//resultSet.next();如果为真代表查询结果有值,为假代表查询结果没有值
    {
        return true;
    }
} catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

return false;

}


已经查到了想要的数据,现在想把数据返回给安卓客户端我要怎么写呀,顺便贴上servlet的代码

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

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


public class SeekServlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public SeekServlet() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * 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 {
        //获取request中的参数(登陆)括号里面是安卓客户端里面构建url时写入的参数,保持一致,绿色的那个
        /*
        *********************************搜索用户******************************************************************
         */
        //获取request中的参数
        String seek_id = request.getParameter("edit_id");
        //接收客户端消息处理
        SeekUser seek3=new SeekUser();
        seek3.setSeek_name(seek_id);
        //向数据库查询对象
        MyDB_user mUser3=new MyDB_user();
        mUser3.Seekaim(seek3);
        //返回查询结果


        //创建一个DataOutputStream对象
        DataOutputStream dos1=new DataOutputStream(response.getOutputStream());

        if(mUser3.Seekaim(seek3))
        {

        }

    }

    /**
     * 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 {

         // 输出流  
        PrintWriter out = response.getWriter();
        // 设置编码形式  
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        // 获取传入的数据  
        String id = request.getParameter("id");
        System.out.println("UserInfoServlet: id = " + id);
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

查询的结果已经可以遍历出来了但是不知道怎么传回给安卓端。Seekaim方法是在servlet中调用的有没有大佬来拯救一下小菜鸡

  • 写回答

1条回答 默认 最新

  • qq_22847605 2019-05-05 09:38
    关注

    放在作用域或者请求转发的url后面中啊,作用域有session,上下文applicationContext,request,response等。例如:如果是request转发到另一个页面的,就放在request作用或上下文中,到时在界面上用相应的作用域或者cl表达式取就行了。放在作用域这种适用于将结果传到另一个页面,而不是请求页面,如果是页面是ajax请求的话,得返回json

    评论

报告相同问题?

悬赏问题

  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接