通过Http将所有数据库行发送到客户端的最佳方法

时间:2015-02-06 13:57:53

标签: java android http

你好,希望你们度过一个愉快的下午,我已经用java编写了几个服务器,他们处理了httpExchanges,有时我需要将整个数据库行发送到客户端,现在我这样做:

  class HousingsListHandler implements HttpHandler{

    @Override
    public void handle(final HttpExchange HE) throws IOException {

        Thread thread =  new Thread(new Runnable() {
                final HttpExchange t = HE;

                    @Override
                    public void run() {

                        Connection conn = getaConnection();
                        PreparedStatement stmt = null;
                        ResultSet tableExistence = null;

                        try {
                            stmt = conn.prepareStatement("SELECT * FROM "+TABLE_NAME);

                        tableExistence = stmt.executeQuery();

                        int count = 0;

                        do {

                            List<String> list = new ArrayList<>();
                            list.add(_HOUSE_ID,tableExistence.getString(COL_HOUSE_ID));
                            list.add(_ADDRESS,tableExistence.getString(COL_ADDRESS));
                            list.add(_LMCP,tableExistence.getString(COL_LMCP));
                            list.add(_CITY,tableExistence.getString(COL_CITY));
                            list.add(_USERNAME,tableExistence.getString(COL_USERNAME));
                            list.add(_DETAILS,tableExistence.getString(COL_DETAILS));
                            list.add(_PRICE,tableExistence.getString(COL_PRICE));
                            list.add(_FILE_NAME,tableExistence.getString(COL_FILE_NAME));
                            list.add(_DATE,tableExistence.getString(COL_DATE));
                            list.add(_IS_RAHN,tableExistence.getString(COL_IS_RAHN));
                            list.add(_IS_EJARE,tableExistence.getString(COL_IS_EJARE));
                            list.add(_IS_RAHND_EJARE,tableExistence.getString(COL_IS_RAHN_AND_EJARE));
                            list.add(_IS_FOROUSH,tableExistence.getString(COL_IS_FOROUSH));

                            t.getResponseHeaders().put("housing"+count,list);
                            count++;

                        } while (tableExistence.next());

                        t.getResponseHeaders().add("size",String.valueOf(count));

                        t.sendResponseHeaders(200,t.getResponseHeaders().size());

                        cleanUp(stmt, tableExistence, t);
                        CloseConnection(conn);

                        } catch (SQLException | IOException e) {
                            display(e.toString());
                            cleanUp(stmt, tableExistence, t);
                            CloseConnection(conn);
                            return;
                        }

                    }});

            thread.start();

      } 
    }

我担心的是因为我期待有1-5百万用户,而我的表可能有超过15k行,这仍然是最好和最有效的方法吗?任何经验都会非常感激,非常感谢

0 个答案:

没有答案