Example usage for java.sql ResultSet getClob

List of usage examples for java.sql ResultSet getClob

Introduction

In this page you can find the example usage for java.sql ResultSet getClob.

Prototype

Clob getClob(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a Clob object in the Java programming language.

Usage

From source file:CSVWriter.java

private static String getColumnValue(ResultSet rs, int colType, int colIndex)
    throws SQLException, IOException {

  String value = "";
      // w w w.  jav  a  2s . co  m
switch (colType)
{
  case Types.BIT:
    Object bit = rs.getObject(colIndex);
    if (bit != null) {
      value = String.valueOf(bit);
    }
  break;
  case Types.BOOLEAN:
    boolean b = rs.getBoolean(colIndex);
    if (!rs.wasNull()) {
      value = Boolean.valueOf(b).toString();
    }
  break;
  case Types.CLOB:
    Clob c = rs.getClob(colIndex);
    if (c != null) {
      value = read(c);
    }
  break;
  case Types.BIGINT:
  case Types.DECIMAL:
  case Types.DOUBLE:
  case Types.FLOAT:
  case Types.REAL:
  case Types.NUMERIC:
    BigDecimal bd = rs.getBigDecimal(colIndex);
    if (bd != null) {
      value = "" + bd.doubleValue();
    }
  break;
  case Types.INTEGER:
  case Types.TINYINT:
  case Types.SMALLINT:
    int intValue = rs.getInt(colIndex);
    if (!rs.wasNull()) {
      value = "" + intValue;
    }
  break;
  case Types.JAVA_OBJECT:
    Object obj = rs.getObject(colIndex);
    if (obj != null) {
      value = String.valueOf(obj);
    }
  break;
  case Types.DATE:
    java.sql.Date date = rs.getDate(colIndex);
    if (date != null) {
      value = DATE_FORMATTER.format(date);;
    }
  break;
  case Types.TIME:
    Time t = rs.getTime(colIndex);
    if (t != null) {
      value = t.toString();
    }
  break;
  case Types.TIMESTAMP:
    Timestamp tstamp = rs.getTimestamp(colIndex);
    if (tstamp != null) {
      value = TIMESTAMP_FORMATTER.format(tstamp);
    }
  break;
  case Types.LONGVARCHAR:
  case Types.VARCHAR:
  case Types.CHAR:
    value = rs.getString(colIndex);
  break;
  default:
    value = "";
}

    
if (value == null)
{
  value = "";
}
    
return value;
      
}

From source file:com.oracle.tutorial.jdbc.ClobSample.java

public String retrieveExcerpt(String coffeeName, int numChar) throws SQLException {

    String description = null;/* w ww  .j a v a  2  s  .  c  o m*/
    Clob myClob = null;
    PreparedStatement pstmt = null;

    try {
        String sql = "select COF_DESC from COFFEE_DESCRIPTIONS " + "where COF_NAME = ?";
        pstmt = this.con.prepareStatement(sql);
        pstmt.setString(1, coffeeName);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            myClob = rs.getClob(1);
            System.out.println("Length of retrieved Clob: " + myClob.length());
        }
        description = myClob.getSubString(1, numChar);
    } catch (SQLException sqlex) {
        JDBCTutorialUtilities.printSQLException(sqlex);
    } catch (Exception ex) {
        System.out.println("Unexpected exception: " + ex.toString());
    } finally {
        if (pstmt != null)
            pstmt.close();
    }
    return description;
}

From source file:org.zaproxy.zap.extension.plugnhack.db.MessageTable.java

public synchronized List<ClientMessage> list() throws SQLException {
    ArrayList<ClientMessage> list = new ArrayList<ClientMessage>();

    ResultSet rs = psGetAllData.executeQuery();
    while (rs.next()) {
        try {// w w  w .j a va2  s  .c om
            InputStream in = rs.getClob("message").getAsciiStream();
            StringWriter w = new StringWriter();
            IOUtils.copy(in, w, StandardCharsets.UTF_8);
            ClientMessage cmsg = new ClientMessage(rs.getString("client_id"),
                    (JSONObject) JSONSerializer.toJSON(w.toString()));
            cmsg.setIndex(rs.getLong("id"));
            cmsg.setReceived(new Date(rs.getTimestamp("timestamp").getTime()));
            cmsg.setChanged(rs.getBoolean("changed"));
            cmsg.setState(State.values()[rs.getInt("state")]);
            list.add(cmsg);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }
    rs.close();
    return list;
}

From source file:nl.nn.adapterframework.util.JdbcUtil.java

public static InputStream getClobInputStream(ResultSet rs, int columnIndex) throws SQLException, JdbcException {
    Clob clob = rs.getClob(columnIndex);
    if (clob == null) {
        throw new JdbcException("no clob found in column [" + columnIndex + "]");
    }//from w  w  w. j  a  va  2  s.  c om
    return getClobInputStream(clob);
}

From source file:nl.nn.adapterframework.util.JdbcUtil.java

public static Reader getClobReader(ResultSet rs, int columnIndex) throws SQLException, JdbcException {
    Clob clob = rs.getClob(columnIndex);
    if (clob == null) {
        throw new JdbcException("no clob found in column [" + columnIndex + "]");
    }/* ww w.  j a v  a 2s.  c  om*/
    return getClobReader(clob);
}

From source file:nl.nn.adapterframework.util.JdbcUtil.java

public static Reader getClobReader(ResultSet rs, String columnName) throws SQLException, JdbcException {
    Clob clob = rs.getClob(columnName);
    if (clob == null) {
        throw new JdbcException("no clob found in column [" + columnName + "]");
    }/*from  www  . j av a2s .c o m*/
    return getClobReader(clob);
}

From source file:org.unitime.commons.hibernate.blob.XmlClobType.java

public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
        throws SQLException {
    Clob clob = rs.getClob(names[0]);
    if (clob == null)
        return null;
    try {/*from  w  w w .j  a  v  a  2  s  .com*/
        SAXReader reader = new SAXReader();
        Document document = reader.read(clob.getCharacterStream());
        return document;
    } catch (DocumentException e) {
        throw new HibernateException(e.getMessage(), e);
    }
}

From source file:nl.nn.adapterframework.util.JdbcUtil.java

/**
 * retrieves an outputstream to a clob column from an updatable resultset.
 *//*from   w  ww  .  j av  a 2  s  . co  m*/
public static OutputStream getClobUpdateOutputStreamxxx(ResultSet rs, int columnIndex)
        throws SQLException, JdbcException {
    Clob clob = rs.getClob(columnIndex);
    if (clob == null) {
        throw new JdbcException("no clob found in column [" + columnIndex + "]");
    }
    return clob.setAsciiStream(1L);
}

From source file:com.wabacus.config.database.type.DB2.java

public String getClobValue(ResultSet rs, int iindex) throws SQLException {
    Clob clob = (Clob) rs.getClob(iindex);
    if (clob == null)
        return "";
    BufferedReader in = null;/*from ww w  . j  a va 2 s.co m*/
    try {
        in = new BufferedReader(clob.getReader());
        StringBuffer sbuffer = new StringBuffer();
        String str = in.readLine();
        while (str != null) {
            sbuffer.append(str).append("\n");
            str = in.readLine();
        }
        return sbuffer.toString();
    } catch (Exception e) {
        log.error("?", e);
        return null;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:it.greenvulcano.gvesb.utils.ResultSetUtils.java

/**
 * Returns all values from the ResultSet as an XML.
 * For instance, if the ResultSet has 3 values, the returned XML will have following fields:
 *                                <RowSet>
 *                                  <data>
 *                                    <row>
 *                                      <col>value1</col>
 *                                      <col>value2</col>
 *                                      <col>value3</col>
 *                                    </row>
 *                                    <row>
 *                                      <col>value4</col>
 *                                      <col>value5</col>
 *                                      <col>value6</col>
 *                                    </row>
 *                                  ..//from  w w  w  . j av a2 s.  c o m
 *                                    <row>
 *                                      <col>valuex</col>
 *                                      <col>valuey</col>
 *                                      <col>valuez</col>
 *                                    </row>
 *                                  </data>
 *                                </RowSet>
 * @param rs
 * @return
 * @throws Exception
 */
public static Document getResultSetAsDOM(ResultSet rs) throws Exception {
    XMLUtils xml = XMLUtils.getParserInstance();
    try {
        Document doc = xml.newDocument("RowSet");
        Element docRoot = doc.getDocumentElement();

        if (rs != null) {
            try {
                ResultSetMetaData metadata = rs.getMetaData();
                Element data = null;
                Element row = null;
                Element col = null;
                Text text = null;
                String textVal = null;
                while (rs.next()) {
                    boolean restartResultset = false;
                    for (int j = 1; j <= metadata.getColumnCount() && !restartResultset; j++) {
                        col = xml.createElement(doc, "col");
                        restartResultset = false;
                        switch (metadata.getColumnType(j)) {
                        case Types.CLOB: {
                            Clob clob = rs.getClob(j);
                            if (clob != null) {
                                Reader is = clob.getCharacterStream();
                                StringWriter strW = new StringWriter();

                                IOUtils.copy(is, strW);
                                is.close();
                                textVal = strW.toString();
                            } else {
                                textVal = "";
                            }
                        }
                            break;
                        case Types.BLOB: {
                            Blob blob = rs.getBlob(j);
                            if (blob != null) {
                                InputStream is = blob.getBinaryStream();
                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                IOUtils.copy(is, baos);
                                is.close();
                                try {
                                    byte[] buffer = Arrays.copyOf(baos.toByteArray(), (int) blob.length());
                                    textVal = new String(Base64.getEncoder().encode(buffer));
                                } catch (SQLFeatureNotSupportedException exc) {
                                    textVal = new String(Base64.getEncoder().encode(baos.toByteArray()));
                                }
                            } else {
                                textVal = "";
                            }
                        }
                            break;
                        case -10: { // OracleTypes.CURSOR
                            Object obj = rs.getObject(j);
                            if (obj instanceof ResultSet) {
                                rs = (ResultSet) obj;
                                metadata = rs.getMetaData();
                            }
                            restartResultset = true;
                        }
                            break;
                        default: {
                            textVal = rs.getString(j);
                            if (textVal == null) {
                                textVal = "";
                            }
                        }
                        }
                        if (restartResultset) {
                            continue;
                        }
                        if (row == null || j == 1) {
                            row = xml.createElement(doc, "row");
                        }
                        if (textVal != null) {
                            text = doc.createTextNode(textVal);
                            col.appendChild(text);
                        }
                        row.appendChild(col);
                    }
                    if (row != null) {
                        if (data == null) {
                            data = xml.createElement(doc, "data");
                        }
                        data.appendChild(row);
                    }
                }
                if (data != null) {
                    docRoot.appendChild(data);
                }
            } finally {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (Exception exc) {
                        // do nothing
                    }
                    rs = null;
                }
            }
        }

        return doc;
    } finally {
        XMLUtils.releaseParserInstance(xml);
    }
}