Example usage for java.sql PreparedStatement setCharacterStream

List of usage examples for java.sql PreparedStatement setCharacterStream

Introduction

In this page you can find the example usage for java.sql PreparedStatement setCharacterStream.

Prototype

void setCharacterStream(int parameterIndex, java.io.Reader reader, long length) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Reader object, which is the given number of characters long.

Usage

From source file:org.sakaiproject.assignment.impl.conversion.impl.CombineDuplicateSubmissionsConversionHandler.java

public boolean convertSource(String id, Object source, PreparedStatement updateRecord) throws SQLException {
    List<String> xml = (List<String>) source;
    SortedSet<String> identifiers = new TreeSet<String>();

    List<AssignmentSubmissionAccess> saxlist = new ArrayList<AssignmentSubmissionAccess>();
    for (int i = 0; i < xml.size(); i++) {
        AssignmentSubmissionAccess sax = new AssignmentSubmissionAccess();
        saxlist.add(sax);/* w  w  w.j  av a  2  s. c  om*/
        try {
            sax.parse(xml.get(i));
            identifiers.add(sax.getId());
        } catch (Exception e1) {
            log.warn("Failed to parse " + id + "[" + xml + "]", e1);
            // return false;
        }
    }

    for (int i = saxlist.size() - 1; i > 0; i--) {
        saxlist.set(i - 1, combineItems(saxlist.get(i), saxlist.get(i - 1)));
    }

    if (saxlist.size() > 0) {
        AssignmentSubmissionAccess result = saxlist.get(0);

        String xml0 = result.toXml();
        String submitTime0 = result.getDatesubmitted();
        String submitted0 = result.getSubmitted();
        String graded0 = result.getGraded();
        String id0 = result.getId();

        log.info("updating \"" + id0 + " revising XML");

        if (getDbDriver().indexOf("mysql") != -1) {
            // see http://bugs.sakaiproject.org/jira/browse/SAK-1737
            // MySQL setCharacterStream() is broken and truncates UTF-8
            // international characters sometimes. So use setBytes()
            // instead (just for MySQL).
            try {
                updateRecord.setBytes(1, xml0.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                log.info(e.getMessage() + xml0);
            }
        } else {
            updateRecord.setCharacterStream(1, new StringReader(xml0), xml0.length());
        }

        updateRecord.setString(2, submitTime0);
        updateRecord.setString(3, submitted0);
        updateRecord.setString(4, graded0);
        updateRecord.setString(5, id0);
        return true;
    } else {
        return false;
    }
}

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

public void nullSafeSet(PreparedStatement ps, Object value, int index, SessionImplementor session)
        throws SQLException, HibernateException {
    if (value == null) {
        ps.setNull(index, sqlTypes()[0]);
    } else {/*from  ww  w. j  a  v a  2s  .  co  m*/
        try {
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            XMLWriter writer = new XMLWriter(bytes, OutputFormat.createCompactFormat());
            writer.write((Document) value);
            writer.flush();
            writer.close();
            ps.setCharacterStream(index, new CharArrayReader(bytes.toString().toCharArray(), 0, bytes.size()),
                    bytes.size());
        } catch (IOException e) {
            throw new HibernateException(e.getMessage(), e);
        }
    }
}