java 使用 NamedParameterJdbcTemplate 插入 Clob

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5791662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:46:46  来源:igfitidea点击:

Inserting Clob with NamedParameterJdbcTemplate

javadatabasespringclob

提问by jabal

I usually use the lobHandler + JdbcTemplate + PreparedStatementSetter triplet to insert my Clob into the database, as I saw on http://www.java2s.com/Code/Java/Spring/InsertClobData.htm

我通常使用 lobHandler + JdbcTemplate + PreparedStatementSetter 三元组将我的 Clob 插入数据库,正如我在http://www.java2s.com/Code/Java/Spring/InsertClobData.htm 上看到的

My question is how to do this with a NamedParameterJdbcTemplate? It has no methods accepting the mysterious PreparedStatementSetter interface as a parameter.

我的问题是如何使用 NamedParameterJdbcTemplate 做到这一点?它没有接受神秘的 PreparedStatementSetter 接口作为参数的方法。

回答by Jonas

This works without using the PreparedStatementCallback and lobHandler, at least when inserting a string.

这在不使用 PreparedStatementCallback 和 lobHandler 的情况下工作,至少在插入字符串时是这样。

NamedParameterJdbcTemplate template; //= new NamedParameterJdbcTemplate(pDs);
String INSERT_STMT = "INSERT INTO MYTABLE (ID, LONG_TEXT) VALUES (:id, :clob)";
MapSqlParameterSource paramSource = new MapSqlParameterSource();
paramSource.addValue("id", 1L, Types.NUMERIC);
paramSource.addValue("clob", "a long long text", Types.CLOB);
template.update(INSERT_STMT, paramSource);

回答by Karthik Ramachandran

I do something like this, obviously we use an Oracle database if you use something else you will have to fiddle with some of the parameter. The getJdbcTemplate method is a helper method of JdbcDaoSupport (a spring helper class.)

我做这样的事情,显然我们使用Oracle数据库,如果你使用其他东西,你将不得不摆弄一些参数。getJdbcTemplate 方法是 JdbcDaoSupport(一个 spring 帮助类)的一个帮助方法。

getJdbcTemplate().execute(new ConnectionCallback() {

        public Object doInConnection(Connection con) throws SQLException, DataAccessException {

            PublishResponseObject responseObject = new PublishResponseObject();
            OracleCallableStatement ocstmt = null;
            CLOB clob = null;

            try {
                clob = createCLOB(xmlString, con);
                ocstmt = (OracleCallableStatement) con.prepareCall("{call schmea.publish(?)}");
                //When in insert mode and update By Pk is specified updates are possible and version numbers will be returned.
                ocstmt.setCLOB(1, clob);
             ...
             }
             finally {
               clob.close()
               stmt.close
            }

回答by Jordan Silva

I'm using Spring 2.5.6 + Oracle and for me it worked straight away.

我正在使用 Spring 2.5.6 + Oracle,对我来说它立即起作用。

// Inserts file into DB and returns the key for the new row
public Number insert(String filename, byte[] data) {
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("filename", filename);
    params.addValue("data", data);

    // Returns the autogenerated ID
    KeyHolder keyHolder = new GeneratedKeyHolder();
    String[] columnNames = {"ID"};

    // This is a NamedParameterJdbcTemplate
    jdbcTemplate.update(INSERT_SQL, params, keyHolder, columnNames);

    return keyHolder.getKey();
}