Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2003
    Posts
    8

    Unanswered: Inserting a CLOB with JDBC in Oracle 9

    Hello,

    I'm using JDBC-thin Drivers and Oracle 9i Database.

    I've tried some code which I found on the net for inserting a CLOB in a database but I get a "socket write error". I used the following code:

    private void insertData(String url, String domain, String link, String param, String content)
    {
    CLOB clob = null;
    String query;
    // Initialize statement Object
    PreparedStatement pstmt = null;
    try {
    query = "INSERT INTO INDEX (URL, Domain, Link, Param, Content) VALUES (?, ?, ?, ?, ?)";
    // Get the statement Object
    pstmt = connection.prepareStatement(query);

    // Get the CLOB object using the getCLOB method.
    clob = getCLOB(content);
    // Bind this CLOB with the prepared Statement

    pstmt.setString(1, url);
    pstmt.setString(2, domain);
    pstmt.setString(3, link);
    pstmt.setString(4, param);
    pstmt.setObject(5, clob);
    // Execute the Prepared Statement
    if (pstmt.executeUpdate() == 1)
    {
    System.out.println("Successfully inserted something!");
    }
    //stmt.execute("commit");
    }
    catch (SQLException sqlexp)
    {
    System.out.println("Damn");
    System.out.println(sqlexp.getMessage());
    }
    catch (Exception exp)
    {
    exp.printStackTrace();
    }
    }


    private CLOB getCLOB(String data) throws SQLException
    {
    CLOB tempClob = null;
    try
    {
    // If the temporary CLOB has not yet been created, create new
    tempClob = CLOB.createTemporary(connection, true, CLOB.DURATION_SESSION);
    // Open the temporary CLOB in readwrite mode to enable writing
    tempClob.open(CLOB.MODE_READWRITE);
    // Get the output stream to write
    Writer tempClobWriter = tempClob.getCharacterOutputStream();
    // Write the data into the temporary CLOB
    tempClobWriter.write(data);
    // Flush and close the stream
    tempClobWriter.flush();
    tempClobWriter.close();
    // Close the temporary CLOB
    tempClob.close();
    }
    catch (SQLException sqlexp)
    {
    tempClob.freeTemporary();
    sqlexp.getMessage();
    }
    catch (Exception exp)
    {
    tempClob.freeTemporary();
    exp.getMessage();
    }
    return tempClob;
    }


    Does anyone know a working sample for insertin CLOB Data by using JDBC thin Drivers in an Oracle 9 Database?

  2. #2
    Join Date
    Jan 2003
    Posts
    8
    Umm, I've tried and tried and now I know the cause of the problem:
    It's got to do with special characters like "". If I try to insert such characters into the table, I get a Socket Exception.
    But if I use normal characters, then everything works fine.

    Does anybody know a solution to this?

    P.S.: The exception occurs due to the line "tempClobWriter.write(data);" in the method getClob()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •