How to use setClob () in PreparedStatement inJDBC

Here is the info:

  • I have a line
  • I want to insert a record into a table with a row in a column, datatype CLOB.
  • I would like to use the setClob()prepared state method .

So my question is how to create an object CLOBfrom this line so that I can use the method setClob().

Thanks in advance, Naveen

+5
source share
6 answers

If you want to write a String column for CLOB just use PreparedStatement.setString.

If you want to know how to create a CLOB from String, this is it

    Clob clob = connection.createClob();
    clob.setString(1, str);
+7
source

You can create clob from the connection object as follows

Connection con = null;// write code to make a connection object
Clob clob = con.createClob();
String str = "this is a stirng";
clob.setString(1, str );

PreparedStatement ps = null;// write code to create a prepared statement
ps.setClob(4, clob);

:

//alternative way       
String str = "this is a stirng";
ByteArrayInputStream inputStream = new ByteArrayInputStream(str.getBytes());
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

int parameterIndex = 1;
PreparedStatement ps = null;// write code to create a prepared statement
ps.setClob(parameterIndex, inputStreamReader);
+6

CLOB . , .setString(), . ORACLE jdbc, , CLOB INPUT, , .

:

INSERT INTO MY_TABL (NUM_COL, VARC_COL, VARC_COL, TS_COL, CLOB_COL) 
   VALUES(?,?,?,?,?);

, CLOB_COL CLOB , .setString(5) 5 ​​- .

+1

Clob, "setString" , : "setString 32766 "

connection.createClob, :

java.lang.AbstractMethodError: org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.createClob()Ljava/sql/Clob;

, , CLOB java throwing, ( setCharacterStream setClob)

/ ( a_horse_with_no_name)

StringReader reader = new StringReader(userAbout);
PreparedStatement insertClob = dbCon.prepareStatement("UPDATE user_data SET user_about=? WHERE user_id=?");
insertClob.setCharacterStream(1, reader, userAbout.length());
insertClob.setInt(2,userId);
+1

, Oracle java-, db. .

, oracle.sql.CLOB

:

create table test_clob (
    c clob
);

create or replace and compile java source named java_clob_insert as

import java.sql.Connection;
import java.sql.PreparedStatement;
import oracle.sql.CLOB;
import java.io.Writer;

public class JavaClobInsert {

    public static void doInsert () {

        try {

            //create the connection and statement
            Connection oracleConn =
                (new oracle.jdbc.OracleDriver()).defaultConnection();

            String stmt = "INSERT INTO test_clob values (?)";

            PreparedStatement oraclePstmt = oracleConn.prepareStatement(stmt);

            //Imagine we have a mysql longtext or some very long string
            String s = "";
            for (int i = 0; i < 32768; i++) {
                s += i % 10;
            }

            //Initialise the Oracle CLOB
            CLOB clob;
            clob = CLOB.createTemporary(oracleConn, true, CLOB.DURATION_CALL);

            //Good idea to check the string is not null before writing to clob
            if (s != null) {
                Writer w = clob.setCharacterStream( 1L );
                w.write(s);
                w.close();
                oraclePstmt.setClob(1, clob);
            } else {
                oraclePstmt.setString(1, "");
            }

            //clean up
            oraclePstmt.executeUpdate();
            oracleConn.commit();
            oraclePstmt.close();
            oracleConn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
/

create or replace procedure clob_insert as language java name 
    'JavaClobInsert.doInsert()';    
/

begin
    clob_insert;
end;
/

select *
from   test_clob; 
+1

...

PreparedStatement, stmt, stmt.setString(colIndex, value) CLOB.

.

, :

ORA-22275: invalid LOB locator

, , - , value . Oracle, MSSQL DB2.

, , ,

if (value == null) {
  stmt.setNull(colIndex, java.sql.Types.CLOB);
}
else {
  stmt.setString(colIndex, value);
}

!

0

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


All Articles