How to insert an InputStream into a CLOB using JDBC?

I am writing a method that receives a comma-delimited text file as an InputStream through a web service. I created my table using the following SQL script:

CREATE SEQUENCE FILE_NUMBER_SEQ
INCREMENT BY 1
START WITH 1
MINVALUE 1
NOMAXVALUE;

CREATE TABLE FILES
(
    ID        NUMBER  NOT NULL ,
    FILE      CLOB NOT NULL
);

In my unit test, I use the following code to create a CSV. I can not create the b / c file of administrator rights on the computer on which the test can run:

    String simulatedCSVFile = new String("col1,col2\\ndata1,data2");
    InputStream stream = new ByteArrayInputStream(
                                simulatedCSVFile.getBytes("UTF-8"));

Now my method is as follows:

public void uploadCSVFile(InputStream stream) {
    try {

        Connection conn = null;
        PreparedStatement preparedStmt = null;

        conn = db.getDataSource().getConnection();
        conn.setAutoCommit(false);

        String sql = "INSERT INTO FILES(ID,FILE) VALUES(FILE_NUMBER_SEQ.NEXTVAL,?)";
        preparedStmt = conn.prepareStatement(sql);
        preparedStmt.setAsciiStream(1, stream);
        int count = preparedStmt.executeUpdate();
    }
}

When I run the method to test it, I get an exception that says: "The message payload is of type: jetty.HttpParser" when calling setAsciiStream.

Any idea how I can convert the stream so that it can be added to SQL for CLOB?

+3
source share
1 answer

InputStreamReader setCharacterStream PreparedStatement?

+1

Source: https://habr.com/ru/post/1784299/


All Articles