Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your best bet is to <strong>not</strong> store the data in Java's memory in any way, but just write the obtained data to the response <strong>immediately</strong> as the data comes in. You also need to configure the MySQL JDBC driver to serve the resultset row-by-row by <a href="http://java.sun.com/javase/6/docs/api/java/sql/Statement.html#setFetchSize%28int%29" rel="noreferrer"><code>Statement#setFetchSize()</code></a> as per the <a href="http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-implementation-notes.html" rel="noreferrer">MySQL JDBC driver documentation</a>, otherwise it will cache the whole thing in memory.</p> <p>Assuming you're familiar with Servlets, here's a kickoff example which takes that all into account:</p> <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/plain"); response.setHeader("Content-Disposition", "attachment;filename=numbers.txt"); // Force download popup. Connection connection = null; Statement statement = null; ResultSet resultSet = null; Writer writer = response.getWriter(); try { connection = database.getConnection(); statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); statement.setFetchSize(Integer.MIN_VALUE); resultSet = statement.executeQuery("SELECT number FROM phonenumbers"); while (resultSet.next()) { writer.write(resultSet.getString("number")); if (!resultSet.isLast()) { writer.write(","); } } } catch (SQLException e) { throw new ServletException("Query failed!", e); } finally { if (resultSet != null) try { resultSet.close; } catch (SQLException logOrIgnore) {} if (statement != null) try { statement.close; } catch (SQLException logOrIgnore) {} if (connection != null) try { connection.close; } catch (SQLException logOrIgnore) {} } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload