Debabrata Sahoo
Debabrata Sahoo

Reputation: 143

Trying to setClob() In JDBC to insert a string to Oracle database

I am trying to insert data as clob in oracle database and the code i am using for inserting is as below:

Connection connection = getConnection();
    PreparedStatement ps = null;
    try
    {
        System.out.println("datra i retrive is : " + status + "  " + operationType + "   " + errorLog + "    " + clipBoardId);
        int i = 0;
        ps = connection.prepareStatement(SQL_CODELIST_BATCH_UPDATE);
        ps.setString(++i, status);
        
        /*ByteArrayInputStream inputStream = new ByteArrayInputStream(errorLog.getBytes());
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);*/
        
        Clob clob = (java.sql.Clob)oracle.sql.CLOB.createTemporary(
                  connection, false, oracle.sql.CLOB.DURATION_SESSION);
        clob.setString(1, errorLog);
        
        ps.setClob(++i, clob);
        ps.setString(++i, operationType);
        ps.setString(++i, clipBoardId);
        ps.executeUpdate();
    }catch(Exception e){
        e.printStackTrace();
    }
    finally
    {
        close(ps);
        close(connection);
    }

and if i am trying to insert above code at the setClob() method its giving exception as below:

java.lang.ClassCastException: $Proxy43 cannot be cast to oracle.jdbc.OracleConnection

at oracle.sql.CLOB.createTemporary(CLOB.java:676)

at oracle.sql.CLOB.createTemporary(CLOB.java:640)

And if I'm trying to create the CLOB as below

Clob clob = connection.createClob() 

and setting the string value as

clob.setString(1, "test code");

and it is stuck at createClob();

Upvotes: 1

Views: 4343

Answers (2)

Jean de Lavarene
Jean de Lavarene

Reputation: 3773

Your solution is the best if you have a large amount of data to store into the CLOB. Otherwise you can just use ps.setString just as if the column was a VARCHAR instead of a CLOB.

Upvotes: 0

Debabrata Sahoo
Debabrata Sahoo

Reputation: 143

Finally i got the solution and its working for me

Instead of creating

Clob clob = connection.createClob();
clob.setClob(<parameter>, clob object);

what i did is used the method

ps.setCharacterStream(<parameter>, inputStreamReader, inputStream.available());

And its working fine for me

Upvotes: 2

Related Questions