Discussions

Web tier: servlets, JSP, Web frameworks: Help Urgent Blob

  1. Help Urgent Blob (1 messages)

    I am inserting a html file in the Oracle as Blob. But I am getting the error on the WLS Console :LOB is NULL

    Can anybody tell me what could be the reason. My code is like this.

    public boolean insertTemplate(String sPid,
    java.io.InputStream oData,String sName)
    {
    makeConnection();//Open the connection to the database
    int update = 0;
    int Template_ID=0;
    try {
    //con.setAutoCommit(false);

    sql ="insert into HTML_TEMPLATE values(HTML_TEMPLATE_SEQ.nextval,'" + sName + "',EMPTY_CLOB(),'" + sPid + "')";
    update = stm.executeUpdate(sql);
    //Get the MAX ID from the table
    sql = "SELECT MAX(TEMPLATE_ID)MAXID FROM HTML_TEMPLATE";
    rs = stm.executeQuery(sql);
    while(rs.next())
    {
    Template_ID = rs.getInt("MAXID");
    }
    java.sql.Blob myBlob = null;
    Statement stmt2 = con.createStatement();

     stmt2.execute("SELECT TEMPLATE from HTML_TEMPLATE FOR UPDATE");
             ResultSet rs = stmt2.getResultSet();
             while (rs.next())
               {
                 myBlob = rs.getBlob("TEMPLATE");
               }
        java.io.InputStream is = oData;

    java.io.OutputStream os = ((weblogic.jdbc.common.OracleBlob) myBlob).getBinaryOutputStream();

    byte[] inBytes = new byte[65534];
    int numBytes = is.read(inBytes);
    while (numBytes > 0)
    {
    os.write(inBytes, 0, numBytes);
    numBytes = is.read(inBytes);
    }

    os.write(inBytes);
    //Clean up
    os.flush();
    os.close();
    stmt2.close();

    } catch (Exception e) {
    System.out.println("EJB - An exception has been raised during the insertion of the Template :" + e.getMessage());
    }
    finally
    {
    try
    {
                    // con.commit();
                    closeConnection();//Close the connection
    }
    catch(Exception ex)
    {
    ex.toString();
    }
    }

    if(update == 0)
    {
    return false;
    }
    else
    {
    return true;
    }
    } //End of insert template method
  2. Help Urgent Blob[ Go to top ]

    Here are a few suggestions:

    1. Change

    stmt2.execute("SELECT TEMPLATE from HTML_TEMPLATE FOR UPDATE");

    to

    stmt2.execute("SELECT TEMPLATE from HTML_TEMPLATE FOR UPDATE WHERE TEMPLATE_ID = " + Template_ID);

    2. Change

    while (numBytes > 0)

    to

    while (numBytes > -1)

    3. Get rid of the last os.write statement (after your while loop).

    John H.