Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Streaming to HTTP Response through ResultSet?

751830Apr 11 2011 — edited Apr 13 2011
I have a cursor which returns a large amount of data (potentially millions of rows, each with 10-20 columns). I would like to efficiently create a CSV file from these results by streaming it to the user upon their demand. In other words, I'm not saving a file to the server, I'm piping the results directly to the Response object of my web/client structure.

Currently, I have a resultset, and a BufferedOutputStream attached to the Response object. But I don't know how to properly get the resultset into an InputStream such that I can write it to the output stream in efficient chunks. My code looks like this so far (work in progress):
		String filename = "testfile.txt";
			
		response.setHeader("cache-Control", "no-store");
	        response.setHeader("pragma", "no-cache");
	        response.setDateHeader("Expires", 0);
	        response.setContentType("application/download");
	        response.setHeader("Content-Disposition","attachment;filename=\""+filename + "\"");
	
	        // Getting output stream.
	        BufferedOutputStream os = new BufferedOutputStream(response.getOutputStream());                    
	
	        // Buffer for read operations.
	        byte[] buffer = new byte[4072];
	
	        // now start the database operations to get the resultset
	        
	        Connection conn = null;
		CallableStatement stmt = null; 
		ResultSet rs = null; 
			
		String query = "{?= call get_all_data(?,?)}";
			
		conn = DBUtil.getConnection();
		conn.setReadOnly(true);
		stmt=conn.prepareCall(query);

		stmt.registerOutParameter(1,java.sql.Types.INTEGER);	// return value
		stmt.registerOutParameter(2,oracle.jdbc.OracleTypes.CURSOR);	// CURSOR
		stmt.registerOutParameter(3,java.sql.Types.VARCHAR);	// return message
			
		stmt.execute();               
			
		rs = (ResultSet)stmt.getObject(2);
			
		// here's where I'm unclear

		// how do I get the resultset to the outputstream in specific sized chunks?  
                // where would I apply the CSV formatting to the columns?

		while (rs.next()) {
                        ?????
			os.write(?????);
		}
			
		rs.close();
		stmt.close();

                os.flush();
Edited by: xaeryan on Apr 11, 2011 12:07 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 11 2011
Added on Apr 11 2011
9 comments
1,146 views