Skip to content Skip to sidebar Skip to footer

Passing String to Clob Java Example

Best Java code snippets using java.sql.Clob (Showing top 20 results out of 2,745)

Refine searchRefine arrow

                                                                                                                                              public                                                                                      String handleClob(Clob clob)                                                                                    throws                                                                                      SQLException {                                                                                                                                                                                                                                if                                                                                      (clob == null)                                                                                                                                                                                                                                return                                                                                      null;                                                                                                                                                                              Reader reader = null;                                                                                                                                                                                      try                                                                                      {                                                                                                                                                                    reader = clob.                                                              getCharacterStream                                                              ();                                                                                                                                                                                                                                if                                                                                      (reader == null)                                                                                                                                                                                                                                return                                                                                      null;                                                                                                                                                                                                                                char                                                              [] buffer =                                                                                    new                                                                                      char[(                                                              int                                                              )clob.                                                              length                                                              ()];                                                                                                                                                                                                                                if                                                                                      (buffer.length ==                                                                                    0                                                              )                                                                                                                                                                                                                                return                                                                                      null;                                                                                                                                                                    reader.                                                              read                                                              (buffer);                                                                                                                                                                                                                                return                                                                                                                                                  new                                                                                      String(buffer);                                                                                                                                                                    }                                                                                    catch                                                                                      (IOException e) {                                                                                                                                                                                                                                throw                                                                                                                                                  new                                                                                      RuntimeException(e);                                                                                                                      }                                                                                                                                                                                      finally                                                                                      {                                                                                                                                                                                                                                if                                                                                      (reader != null)                                                                                                                                                                                                                                try                                                                                      {reader.                                                              close                                                              ();}                                                                                    catch                                                                                      (IOException e) {                                                              throw                                                                                                                                                  new                                                                                      RuntimeException(e);}                                                                                                                      }                                                        }                                                                                          
                                                                                                                                        Object obj = rs.                                                              getObject                                                              (index);                                                                                                  String className = null;                                                                                                                                                                if                                                                                      (obj != null) {                                                                                                                                                                                                                            obj = blob.                                                              getBytes                                                              (                                                              1                                                              , (                                                              int                                                              ) blob.                                                              length                                                              ());                                                                                                                                                                                                                            obj = clob.                                                              getSubString                                                              (                                                              1                                                              , (                                                              int                                                              ) clob.                                                              length                                                              ());                                                                                                                                                                                                                            obj = rs.                                                              getTimestamp                                                              (index);                                                                                                                                                                                                                            String metaDataClassName = rs.                                                              getMetaData                                                              ().getColumnClassName(index);                                                                                                                                                                                                                                if                                                                                      (                                                              "java.sql.Timestamp"                                                              .equals(metaDataClassName) ||                                                                                    "oracle.sql.TIMESTAMP"                                                              .equals(metaDataClassName)) {                                                                                                                                                                    obj = rs.                                                              getTimestamp                                                              (index);                                                                                                                                    
                                      @Override                                                                                                                                                                public                                                                                      InputStream getClobAsAsciiStream(ResultSet rs,                                                                                    int                                                                                      columnIndex)                                                                                    throws                                                                                      SQLException {                                                                                                                                                                    logger.debug(                                                              "Returning CLOB as ASCII stream"                                                              );                                                                                                                                                                                                                                if                                                                                      (                                                              this                                                              .wrapAsLob) {                                                                                                                                                                    Clob clob = rs.                                                              getClob                                                              (columnIndex);                                                                                                                                                                                                                                return                                                                                      clob.                                                              getAsciiStream                                                              ();                                                                                                                      }                                                                                                                                                                                      else                                                                                      {                                                                                                                                                                                                                                return                                                                                      rs.                                                              getAsciiStream                                                              (columnIndex);                                                                                                                      }                                                        }                                                                                          
                                                                                                                                              public                                                                                      String getClobAsString(ResultSet rs,                                                                                    int                                                                                      columnIndex)                                                                                    throws                                                                                      SQLException {                                                                                                                                                                    Clob clob = rs.                                                              getClob                                                              (columnIndex);                                                                                                                                                                                                                                                                                        if                                                                                      (clob == null) {                                                                                                                                                                                                                                return                                                                                      null;                                                                                                                      }                                                                                                                                                                                                                                              return                                                                                      clob.                                                              getSubString                                                              (                                                              1                                                              , (                                                              int                                                              ) clob.                                                              length                                                              ());                                                                                                  }                                                                                          
                                      @Override                                                        @Nullable                                                                                                                                                                public                                                                                      String getClobAsString(ResultSet rs,                                                                                    int                                                                                      columnIndex)                                                                                    throws                                                                                      SQLException {                                                                                                                                                                    logger.debug(                                                              "Returning CLOB as string"                                                              );                                                                                                                                                                                                                                if                                                                                      (                                                              this                                                              .wrapAsLob) {                                                                                                                                                                    Clob clob = rs.                                                              getClob                                                              (columnIndex);                                                                                                                                                                                                                                return                                                                                      clob.                                                              getSubString                                                              (                                                              1                                                              , (                                                              int                                                              ) clob.                                                              length                                                              ());                                                                                                                      }                                                                                                                                                                                      else                                                                                      {                                                                                                                                                                                                                                return                                                                                      rs.                                                              getString                                                              (columnIndex);                                                                                                                      }                                                        }                                                                                          
                                                                                                                                              private                                                                                      String readClobAsString(Clob logClob)                                                                                    throws                                                                                      SQLException {                                                                                                                                                                                                                                if                                                                                      (logClob == null) {                                                                                                                                                                                                                                return                                                                                      StringUtils.EMPTY;                                                                                                                      }                                                                                                                                                                                      long                                                                                      length = logClob.                                                              length                                                              ();                                                                                                                                                                                                                                return                                                                                      logClob.                                                              getSubString                                                              (                                                              1                                                              , (                                                              int                                                              ) length);                                                                                                  }                                                                                          
                                                                                                                                        ResultSetMetaData rsmd = rs.                                                              getMetaData                                                              ();                                                                                                                                                                                                          int                                                                                      numColumns = rsmd.getColumnCount();                                                                                                                                            String[] columnNames =                                                                                    new                                                                                      String[numColumns];                                                                                                                                                                                                                                                                  while                                                                                      (rs.                                                              next                                                              ()) {                                                                                                                                                                                                                            l = rs.                                                              getInt                                                              (i +                                                                                    1                                                              );                                                                                                                                                                                                                                if                                                                                      (rs.                                                              wasNull                                                              ()) {                                                                                                                      jgen.writeNull();                                                                                                                                                                                  Blob blob = rs.                                                              getBlob                                                              (i);                                                                                                                                                                    provider.defaultSerializeValue(blob.                                                              getBinaryStream                                                              (), jgen);                                                                                                                                                                    blob.                                                              free                                                              ();                                                                                                                                                                                                                                break                                                              ;                                                                                                                                                                                                                            Clob clob = rs.                                                              getClob                                                              (i);                                                                                                                                                                    provider.defaultSerializeValue(clob.                                                              getCharacterStream                                                              (), jgen);                                                                                                                                                                    clob.                                                              free                                                              ();                                                                                                                                                                                                                                break                                                              ;                                                                                                                                    
                                                                                              dataFileWriter.create(schema, outStream);                                                                                                                                                                                  Clob clob = rs.                                                              getClob                                                              (i);                                                                                                                                                                                                                                if                                                                                      (clob != null) {                                                                                                                                                                                                                                long                                                                                      numChars = clob.                                                              length                                                              ();                                                                                                                                                                                                                                char                                                              [] buffer =                                                                                    new                                                                                      char[(                                                              int                                                              ) numChars];                                                                                                                                                                    InputStream is = clob.                                                              getAsciiStream                                                              ();                                                                                                                                                                                                                                int                                                                                      index =                                                                                    0                                                              ;                                                                                                                                                                                                                                int                                                                                      c = is.read();                                                                                                                                                                                                                            clob.                                                              free                                                              ();                                                                                                                                                                    }                                                                                    else                                                                                      {                                                                                                                                                                    rec.put(i -                                                                                    1                                                              , null);                                                                                                                                                                                                                                                                                        long                                                                                      numChars = (                                                              int                                                              ) nClob.length();                                                                                                                                                                                                                                final                                                                                      CharBuffer buffer = CharBuffer.allocate((                                                              int                                                              ) numChars);                                                                                                                                                                    characterStream.                                                              read                                                              (buffer);                                                                                                                      buffer.flip();                                                                                                                          rec.put(i -                                                                                    1                                                              , buffer.toString());                                                                                                                                                                                                                            blob.                                                              free                                                              ();                                                                                                                                                                    }                                                                                    else                                                                                      {                                                                                                                                                                    rec.put(i -                                                                                    1                                                              , null);                                                                                                                                    
                                      @Override                                                                                                                                                                public                                                                                      String queryAuditTrailMessage(                                                              long                                                                                      id, Connection con)                                                                                    throws                                                                                      SQLException  {                                                                                                                                                                                                                                final                                                                                      StringBuilder sql =                                                                                    new                                                                                      StringBuilder();                                                                                                                                                                    sql.append(                                                              "SELECT LONG_MESSAGE FROM COP_AUDIT_TRAIL_EVENT WHERE SEQ_ID = ?"                                                              );                                                                                                                                                                                                                                                                                        try                                                                                      (PreparedStatement pStmtQueryWFIs = con.                                                              prepareStatement                                                              (sql.toString())) {                                                                                                                                                                    pStmtQueryWFIs.                                                              setObject                                                              (                                                              1                                                              , id);                                                                                                                                                                    ResultSet rs = pStmtQueryWFIs.                                                              executeQuery                                                              ();                                                                                                                                                                                                                                while                                                                                      (rs.                                                              next                                                              ()) {                                                                                                                                                                                                                                try                                                                                      {                                                                                                                                                                    Clob message = rs.                                                              getClob                                                              (                                                              "LONG_MESSAGE"                                                              );                                                                                                                                                                                                                                if                                                                                      ((                                                              int                                                              ) message.                                                              length                                                              () >                                                                                    0                                                              ) {                                                                                                                                                                                                                                return                                                                                      message.                                                              getSubString                                                              (                                                              1                                                              , (                                                              int                                                              ) message.                                                              length                                                              ());                                                                                                                      }                                                                                                                                                                                      return                                                                                      null;                                                                                                                                                                    }                                                                                    catch                                                                                      (Exception e) {                                                                                                                                                                    logger.error(                                                              "decoding of '"                                                                                      + rs +                                                                                    "' failed: "                                                                                      + e.toString(), e);                                                                                                                      }                                                                            }                                                                            }                                                                                                                                                                                                                                              return                                                                                      null;                                                                                                  }                                                                                          
                                      @Override                                                                                                                                                                public                                                                                      String selectAuditTrailMessage(                                                              long                                                                                      id, Connection con) {                                                                                                                      PreparedStatement selectStmt = null;                                                                                                                                                                                      try                                                                                      {                                                                                                                                                                    selectStmt = con.                                                              prepareStatement                                                              (                                                              "SELECT LONG_MESSAGE FROM COP_AUDIT_TRAIL_EVENT WHERE SEQ_ID=?"                                                              );                                                                                                                                                                    selectStmt.                                                              setLong                                                              (                                                              1                                                              , id);                                                                                                                                                                    ResultSet result = selectStmt.                                                              executeQuery                                                              ();                                                                                                                                                                                                                                                                                        while                                                                                      (result.                                                              next                                                              ()) {                                                                                                                                                                    Clob message = result.                                                              getClob                                                              (                                                              1                                                              );                                                                                                                                                                                                                                if                                                                                      ((                                                              int                                                              ) message.                                                              length                                                              () >                                                                                    0                                                              ) {                                                                                                                                                                                                                                return                                                                                      messagePostProcessor.deserialize(message.                                                              getSubString                                                              (                                                              1                                                              , (                                                              int                                                              ) message.                                                              length                                                              ()));                                                                                                                      }                                                                            }                                                                                                                          result.                                                              close                                                              ();                                                                                                                                                                    }                                                                                    catch                                                                                      (SQLException e) {                                                                                                                                                                                                                                throw                                                                                                                                                  new                                                                                      RuntimeException(e);                                                                                                                                                                    }                                                                                    finally                                                                                      {                                                                                                                      JdbcUtils.closeStatement(selectStmt);                                                                            }                                                                                                                                                                                      return                                                                                                                                                  ""                                                              ;                                                                                                  }                                                                                          
                                                                                                                                        Reader l_inStream = x.                                                              getCharacterStream                                                              ();                                                                                                                                                                                                          int                                                                                      l_length = (                                                              int                                                              ) x.                                                              length                                                              ();                                                                                                  LargeObjectManager lom = connection.getLargeObjectAPI();                                                                                                                                                                long                                                                                      oid = lom.createLO();                                                                                                                                                                                                                                                                                        int                                                                                      c = l_inStream.                                                              read                                                              ();                                                                                                                                                                                                                                int                                                                                      p =                                                                                    0                                                              ;                                                                                                                                                                                                                                while                                                                                      (c > -                                                              1                                                                                      && p < l_length) {                                                                                                                      lw.write(c);                                                                                                                          c = l_inStream.                                                              read                                                              ();                                                                                                                      p++;                                                                                          
                                                                                                                  reader = (Reader) data;                                                                                                  }                                                                                    else                                                                                      {                                                                                                                                                                    reader = ((Clob) data).                                                              getCharacterStream                                                              ();                                                                                                                                                          Integer wId;                                                                                                                                                                while                                                              ((wId = setting.getWordId(word)) == null) {                                                                                                                                                                    prepInsertWord.                                                              setString                                                              (                                                              1                                                              , word);                                                                                                                                                                    prepInsertWord.                                                              execute                                                              ();                                                                                                                                                                    ResultSet rs = prepInsertWord.                                                              getGeneratedKeys                                                              ();                                                                                                                                                                                                                                if                                                                                      (rs.                                                              next                                                              ()) {                                                                                                                                                                    wordId = rs.                                                              getInt                                                              (                                                              1                                                              );                                                                                                                                                                                                                                if                                                                                      (wordId !=                                                                                    0                                                              ) {                                                                                                                      setting.addWord(word, wordId);                                                                                          
                                                                                                                                              private                                                                                      OpResult executeUpdate(Connection connection, String query, String key, String value)                                                                                    throws                                                                                      SQLException {                                                                                                                                                                    OpResult result =                                                                                    new                                                                                      OpResult();                                                                                                                      PreparedStatement statement = null;                                                                                                                                                                                      try                                                                                      {                                                                                                                                                                    statement = connection.                                                              prepareStatement                                                              (query);                                                                                                                                                                    Clob clob = connection.                                                              createClob                                                              ();                                                                                                                                                                    clob.                                                              setString                                                              (                                                              1                                                              , value);                                                                                                                                                                    statement.                                                              setClob                                                              (                                                              1                                                              , clob);                                                                                                                                                                    statement.                                                              setString                                                              (                                                              2                                                              , key);                                                                                                                                                                                                                                int                                                                                      status = statement.                                                              executeUpdate                                                              ();                                                                                                                                                                    LOG.info(                                                              "update {} with query={}"                                                              , status, query);                                                                                                                                                                    }                                                                                    finally                                                                                      {                                                                                                                                                                                                                                if                                                                                      (statement != null) {                                                                                                                                                                    statement.                                                              close                                                              ();                                                                                                                      }                                                                            }                                                                                                                                                                                      return                                                                                      result;                                                                                                  }                                                                                          
                                      @Override                                                                                                                                                                public                                                                                                                                                  void                                                                                      close()                                                                                    throws                                                                                      IOException {                                                                                                                                                                                                                                try                                                                                      {                                                                                                                                                                    reader.                                                              close                                                              ();                                                                                                                                                                    }                                                                                    finally                                                                                      {                                                                                                                                                                                                                                try                                                                                      {                                                                                                                                                                    clob.                                                              free                                                              ();                                                                                                                                                                    }                                                                                    catch                                                                                      (SQLException e) {                                                                                                                                                                    log.debug(e.                                                              getMessage                                                              ());                                                                                                                      }                                                                            }                                                        }                                                                                          
                                                                                                                                                                Reader reader = clob.                                                              getCharacterStream                                                              ();                                                                                                                                                                                                                                                                                        char                                                              [] chars =                                                                                    new                                                                                      char[                                                              2048                                                              ];                                                                                                                                                                                                                                for                                                                                      (;;) {                                                                                                                                                                                                                                int                                                                                      len = reader.                                                              read                                                              (chars,                                                                                    0                                                              , chars.length);                                                                                                                                                                                                                                if                                                                                      (len <                                                                                    0                                                              ) {                                                                                                                                                                                                                                break                                                              ;                                                                                                                                                                                                                            reader.                                                              close                                                              ();                                                                                                                      serializer.write(text);                                                                                                  }                                                                                    catch                                                                                      (SQLException e) {                                                                                                                                    
                                                                                                                                              private                                                                                                                                                  static                                                                                      List<Map<String, Object>> getHashMap(ResultSet resultSet)                                                                                    throws                                                                                      Exception {                                                                                                                                                                    ResultSetMetaData metaData = resultSet.                                                              getMetaData                                                              ();                                                                                                                                                                                                                                int                                                                                      colCount = metaData.getColumnCount();                                                                                                                      List<Map<String, Object>> rows = newArrayList();                                                                                                                                                                                      while                                                                                      (resultSet.                                                              next                                                              ()) {                                                                                                                      Map<String, Object> columns = newHashMap();                                                                                                                                                                                      for                                                                                      (                                                              int                                                                                      i =                                                                                    1                                                              ; i <= colCount; i++) {                                                                                                                                                                    Object value = resultSet.                                                              getObject                                                              (i);                                                                                                                                                                                                                                if                                                                                      (value                                                                                    instanceof                                                                                      Clob) {                                                                                                                      Clob clob = (Clob) value;                                                                                                                          value = IOUtils.toString((clob.                                                              getAsciiStream                                                              ()));                                                                                                                      doClobFree(clob);                                                                                                                          }                                                                                    else                                                                                                                                                  if                                                                                      (value                                                                                    instanceof                                                                                      BigDecimal) {                                                                                                                                    
                                                                                                                                              private                                                                                                                                                  static                                                                                      <T> Object getObject(                                                              final                                                                                      ResultSet rs, Class<T> cls,                                                                                    int                                                                                      i) {                                                                                                                                                                                                                                try                                                                                      {                                                                                                                                                                                                                                if                                                                                      (rs.                                                              getObject                                                              (i) == null) {                                                                                                                                                                                                                                return                                                                                      null;                                                                                                                                                                                                                                                                                        final                                                                                                                                                  int                                                                                      type = rs.                                                              getMetaData                                                              ().getColumnType(i);                                                                                                                                                                                                                                                                                        return                                                                                      rs.                                                              getDate                                                              (i, Calendar.getInstance());                                                                                                                                                                                                                                else                                                                                                                                                  if                                                                                      (type == Types.TIME)                                                                                                                                                                                                                                return                                                                                      rs.                                                              getTime                                                              (i, Calendar.getInstance());                                                                                                                                                                                                                            }                                                                                    else                                                                                                                                                  if                                                                                      (type == Types.CLOB && Reader.                                                              class                                                              .isAssignableFrom(cls)) {                                                                                                                                                                    Clob c = rs.                                                              getClob                                                              (i);                                                                                                                                                                    Reader r = c.                                                              getCharacterStream                                                              ();                                                                                                                                                                                                                                return                                                                                      createFreeOnCloseReader(c, r);                                                                                                                                                                    }                                                                                    else                                                                                                                                                  if                                                                                      (type == Types.BLOB && cls.equals(                                                              byte                                                              [].                                                              class                                                              )) {                                                                                                                                                                                                                            }                                                                                    else                                                                                                                                                  if                                                                                      (type == Types.BLOB && InputStream.                                                              class                                                              .isAssignableFrom(cls)) {                                                                                                                                                                                                                                final                                                                                      Blob b = rs.                                                              getBlob                                                              (i);                                                                                                                                                                                                                                final                                                                                      InputStream is = rs.                                                              getBlob                                                              (i).                                                              getBinaryStream                                                              ();                                                                                                                                                                                                                                return                                                                                      createFreeOnCloseInputStream(b, is);                                                                                                                                                                    }                                                                                    else                                                                                                                                                                                                  
                                                                                                                                                                                                                            public                                                                                                                                                  static                                                                                      String getString(Clob data)                                                                                    throws                                                                                      SQLException {                                                                                                                                                                                                                            StringBuilder sb =                                                                                    new                                                                                      StringBuilder();                                                                                                                                                                                                                                char                                                              [] cbuf =                                                                                    new                                                                                      char[                                                              8192                                                              ];                                                                                                                                                                                                                                int                                                                                      l =                                                                                    0                                                              ;                                                                                                                                                                                                                                try                                                                                      (Reader r = data.                                                              getCharacterStream                                                              ()) {                                                                                                                                                                                                                                while                                                                                      ((l = r.                                                              read                                                              (cbuf)) > -                                                              1                                                              ) {                                                                                                                                                                    sb.append(cbuf,                                                                                    0                                                              , l);                                                                                                                      }                                                                                                                          }                                                                                    catch                                                                                      (IOException ioe) {                                                                                                                                                                                                                                throw                                                                                                                                                  new                                                                                                                                                  SQLException                                                              (                                                              "Unable to read character stream from Clob."                                                              , ioe);                                                                                                                      }                                                                                                                                                                                      return                                                                                      sb.toString();                                                                                                  }                                  
                                                                                                                                                                wkt = rs.                                                              getString                                                              (column +                                                                                    "_inline"                                                              );                                                                                                                                            }                                                                                    catch                                                                                      (SQLException e) {                                                                                                                                                                                                                                                                  for                                                                                      (                                                              int                                                                                      i =                                                                                    0                                                              ; i < rs.                                                              getMetaData                                                              ().getColumnCount(); i++) {                                                                                                                                                                                                                                if                                                                                      (column.equals(rs.                                                              getMetaData                                                              ().getColumnName(i +                                                                                    1                                                              ))) {                                                                                                                                                                    index = i +                                                                                    1                                                              ;                                                                                                                                                                                                                                break                                                              ;                                                                                                                                                                                                                                                                                        return                                                                                      null;                                                                                                                                                                                                    InputStream in = clob.                                                              getAsciiStream                                                              ();                                                                                                                                                                                                          try                                                                                      {                                                                                                                                                                                                                                return                                                                                                                                                  new                                                                                      WKTReader(factory).read(                                                              new                                                                                      InputStreamReader(in));                                                                                                                                    
                                                                                                                  java.sql.Connection con;                                                        javax.xml.bind.Marshaller marshaller;                                                                                                                                                          Clob xmlClob = con.                                                              createClob                                                              ();                                                                                                                                            try                                                                                      {                                                                                                                                                                                                                                try                                                                                      (Writer xmlClobWriter = xmlClob.                                                              setCharacterStream                                                              (                                                              1                                                              )) {                                                                                                                      m.marshal(jaxbObject, xmlClobWriter);                                                                                                                          }                                                                                                                                                                                                                                                                                                                                        try                                                                                      (PreparedStatement stmt = con.                                                              prepareStatement                                                              (                                                              "INSERT INTO table (xml) values(?)"                                                              )) {                                                                                                                                                                    stmt.                                                              setClob                                                              (                                                              1                                                              , xmlClob);                                                                                                                                                                    stmt.                                                              executeUpdate                                                              ();                                                                                                                      }                                                                                                  }                                                                                    finally                                                                                      {                                                                                                                                                                    xmlClob.                                                              free                                                              ();                                                                                                  }                                  

frankhaddle.blogspot.com

Source: https://www.tabnine.com/code/java/classes/java.sql.Clob

Post a Comment for "Passing String to Clob Java Example"