Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8165137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:28:08+00:00 2026-06-06T19:28:08+00:00

I need a Java function that returns the results of a SQL SELECT query

  • 0

I need a Java function that returns the results of a SQL SELECT query as an InputStream parameter for another system that sends the result over a network.

However, the InputStream must be of a String with custom delimiters (i.e. often, but not always, CSV).

While I can easily create a function to retrieve the result, create a delimited String, and finally convert that String to an InputStream, the SQL result will often be much too large to process in memory. Also, processing the entire result set before returning the result will incur an unwanted wait time.

How can I return an InputStream to iterate over the SQL result and send the processed (delimited) data as it is returned from the database?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:28:09+00:00Added an answer on June 6, 2026 at 7:28 pm

    Posting (not tested) code snippet, which should give you basic idea:

    /**
     * Implementors of this interface should only convert current row to byte array and return it.
     * 
     * @author yura
     */
    public interface RowToByteArrayConverter {
        byte[] rowToByteArray(ResultSet resultSet);
    }
    
    public class ResultSetAsInputStream extends InputStream {
    
        private final RowToByteArrayConverter converter;
        private final PreparedStatement statement;
        private final ResultSet resultSet;
    
        private byte[] buffer;
        private int position;
    
        public ResultSetAsInputStream(final RowToByteArrayConverter converter, final Connection connection, final String sql, final Object... parameters) throws SQLException {
            this.converter = converter;
            statement = createStatement(connection, sql, parameters);
            resultSet = statement.executeQuery();
        }
    
        private static PreparedStatement createStatement(final Connection connection, final String sql, final Object[] parameters) {
            // PreparedStatement should be created here from passed connection, sql and parameters
            return null;
        }
    
        @Override
        public int read() throws IOException {
            try {
                if(buffer == null) {
                    // first call of read method
                    if(!resultSet.next()) {
                        return -1; // no rows - empty input stream
                    } else {
                        buffer = converter.rowToByteArray(resultSet);
                        position = 0;
                        return buffer[position++] & (0xff);
                    }
                } else {
                    // not first call of read method
                    if(position < buffer.length) {
                        // buffer already has some data in, which hasn't been read yet - returning it
                        return buffer[position++] & (0xff);
                    } else {
                        // all data from buffer was read - checking whether there is next row and re-filling buffer
                        if(!resultSet.next()) {
                            return -1; // the buffer was read to the end and there is no rows - end of input stream
                        } else {
                            // there is next row - converting it to byte array and re-filling buffer
                            buffer = converter.rowToByteArray(resultSet);
                            position = 0;
                            return buffer[position++] & (0xff);
                        }
                    }
                }
            } catch(final SQLException ex) {
                throw new IOException(ex);
            }
        }
    
    
    
        @Override
        public void close() throws IOException {
            try {
                statement.close();
            } catch(final SQLException ex) {
                throw new IOException(ex);
            }
        }
    }
    

    This is very straight-forward implementation and it can be improved in following ways:

    • code duplication between if and else in read method can be removed – it was posted just for clarification
    • instead of re-creating byte array buffer for each row (new byte[] is costly operation), more sophisticated logic can be implemented to use byte array buffer which is initialised only once and then re-filled. One then should change RowToByteArrayConverter.rowToByteArray method’s signature to int fillByteArrayFromRow(ResultSet rs, byte[] array) which should return number of bytes filled and fill passed byte array.

    Because byte array contains signed bytes it can contain -1 (which is actually 255 as unsigned byte) and thus indicate incorrect end of stream, so & (0xff) is used to convert signed byte to unsigned bytes as integer values. For details refer to How does Java convert int into byte?.

    Please also note that if network transfer speed is slow, this may keep open result sets for
    a long time, thus posing problems for the database.

    Hope this helps …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a java script function that converts the document object of the current
There is a good function that I need, which is implemented in the Java
I've got this java function that extracts strings from Inputstreams and returns a List.
I need this for calling a C function from Java class (JNI) and I
I need to implement a function which returns a TDictionary, without specifying the exact
I've got a BST AVL, in Java, that I need to prove is balanced
I need a function to generate random integers. (assume Java long type for now,
Sadly, I have a legacy PHP4 system that I continuously need to add features
I'm working on a Java function that should be called infinitely . This function
I need to draw a chart in Jasper iReport that can sum the results

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.