简体   繁体   English

如何使用JDBC将InputStream插入CLOB?

[英]How to insert an InputStream in to a CLOB using JDBC?

I am writing a method that receives a Comma Separated text file as a InputStream through a web service. 我正在编写一个方法,通过Web服务接收逗号分隔文本文件作为InputStream。 I created my table using the following SQL script: 我使用以下SQL脚本创建了我的表:

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 am using the following code to generate the CSV. 在我的单元测试中,我使用以下代码生成CSV。 I cannot create a file b/c of admin rights on the machine the test may be running on: 我无法在运行测试的机器上创建管理员权限的文件b / c:

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

My method currently looks like this: 我的方法目前看起来像这样:

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 execute the method to test it I get an exception that reads "Message payload is of type: jetty.HttpParser" when setAsciiStream is called. 当我执行方法来测试它时,我得到一个异常,当调用setAsciiStream时,它会读取“消息有效负载的类型:jetty.HttpParser”。

Any idea how I can convert the stream so that it can be added to the SQL for the CLOB? 知道我如何转换流,以便它可以添加到CLOB的SQL?

你能尝试PreparedStatement InputStreamReadersetCharacterStream方法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM