【问题标题】:Wild card for java.sql.Types with Spring JdbcTemplate带有 Spring JdbcTemplate 的 java.sql.Types 的通配符
【发布时间】:2017-05-05 17:08:26
【问题描述】:

我想在 oracle 数据库中的表中插入一个 blob。但我不知道其他列的数据类型。我想以 Object 类型传递其他列的值。

我知道我可以使用以下代码插入 blob 值。

jdbcTemplate.update(
            "INSERT INTO LOB_ (BLOB_) VALUES (?)",
            new Object[]{new SqlLobValue(inputStream)},
            new int[]{Types.BLOB}
    ); 

但我的问题是,我不知道其他列的类型。那么如何将类型数组中的类型指定为update(query, args, typeArgs)方法的第三个参数。

是否有任何值(如通配符)来告诉 jdbctemplate 跳过检查其他列的类型?

我想要这样的东西?

jdbcTemplate.update(
            "INSERT INTO LOB_ (BLOB_, NAME, AGE) VALUES (?, ?, ?)",
            new Object[]{new SqlLobValue(inputStream), "Some string", 34},
            new int[]{Types.BLOB, ?, ?}
    ); 

【问题讨论】:

    标签: java sql oracle blob spring-jdbc


    【解决方案1】:

    请您尝试以下方法。 https://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html

    final File blobIn = new File("spring2004.jpg");
    final InputStream blobIs = new FileInputStream(blobIn);
    final File clobIn = new File("large.txt");
    final InputStream clobIs = new FileInputStream(clobIn);
    final InputStreamReader clobReader = new InputStreamReader(clobIs);
    jdbcTemplate.execute(
        "INSERT INTO lob_table (id, a_clob, a_blob) VALUES (?, ?, ?)",
        new AbstractLobCreatingPreparedStatementCallback(lobHandler) { 1
            protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
                ps.setLong(1, 1L);
                lobCreator.setClobAsCharacterStream(ps, 2, clobReader, (int)clobIn.length()); 2
                lobCreator.setBlobAsBinaryStream(ps, 3, blobIs, (int)blobIn.length()); 3
            }
        }
    );
    blobIs.close();
    clobReader.close();
    

    【讨论】:

    • 这不需要类型作为参数。但是在这里我不能将一组值作为对象数组传递。我必须手动将值设置为准备好的语句,这在我的情况下是不可能的。
    猜你喜欢
    • 1970-01-01
    • 2020-10-05
    • 2015-10-06
    • 2013-08-25
    • 2011-08-22
    • 2017-08-21
    • 1970-01-01
    • 2011-04-30
    • 2021-05-20
    相关资源
    最近更新 更多