db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Valentin Cozma <vco...@elfyard.com>
Subject Re: Saving XML into a database
Date Mon, 25 Aug 2008 22:23:33 GMT
jay _ wrote:
> I've been struggling to find some decent examples of how to save XML 
> into a Derby database.
>
> I've managed to figure out that XML needs to be serialised into a 
> different datatype before it can saved in Derby. I am using a CLOB. 
> However, I am not quite sure how exactly to get the data into the XML 
> column.
>
> My sample code looks like this ....
>
>         String expression = "INSERT INTO XML_DATA (DOCUMENT ) " +
>                 "VALUES (XMLPARSE( DOCUMENT CAST (? AS CLOB) PRESERVE 
> WHITESPACE))";
>
>         try
>         {
>             PreparedStatement preparedStatement = 
> connection.prepareStatement(expression);
>             // update statement and execute
> //            Element ele = getHomeViewElement();
>             logger.debug("xmlDocument = " + 
> xmlDocument.getDocument().toString());
>             preparedStatement.setString(1, xmlDocument.toString()); 
>
>             preparedStatement.executeUpdate();
>
>         } catch (SQLException e) {
>             logger.warn(e);
>         }
>
> Using either the Element or the Document toString() methods result in 
> an exception ....
> java.sql.SQLException: Invalid XML DOCUMENT: Content is not allowed in 
> prolog.





I am inserting xml as blob ( binary ) . works like a charm . example 
follows .

-- write
    public void addFileVersion(int idFile, String autor, String 
comentarii, InputStream stream, int fileSize) throws SQLException {
        addVersionToFile.setInt(1, idFile);
        addVersionToFile.setString(2, autor);
        addVersionToFile.setString(3, comentarii);
        addVersionToFile.setBinaryStream(4, stream, fileSize);
        addVersionToFile.execute();
}

-- read

    public void saveFileVersionToDisk(int idFile, File location) throws 
SQLException, IOException {
        InputStream result = null;
        getFileVersion.setInt(1, idFile);
        ResultSet rs = getFileVersion.executeQuery();
        if(rs.next()) {
            result = rs.getBinaryStream("bytes");
        }
        if(result != null) {
            OutputStream of = new FileOutputStream(location);
            FileUtils.copyFiles(result, of);
        }
        rs.close();
    }

-- table

create table file_versions(
        id int NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START 
WITH 1, INCREMENT BY 1),
        id_file int not null constraint FK_FILE_VERSIONS__files 
references files(id) on delete cascade,
        autor varchar(100) NOT NULL,
        comentarii varchar(1000) NOT NULL,
        bytes blob not null);



>
> Can someone suggest what I am doing wrong here?
>
> Cheers,
> Jay
>
> ------------------------------------------------------------------------
> Be the filmmaker you always wanted to be—learn how to burn a DVD with 
> Windows®. Make your smash hit 
> <http://clk.atdmt.com/MRT/go/108588797/direct/01/>


Mime
View raw message