<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: XpertIvy / SQL : How can i write a file in a sql db?</title><link>https://answers.axonivy.com/questions/1001/xpertivy-sql-how-can-i-write-a-file-in-a-sql-db</link><description>&lt;p&gt;Hello guys i have the the xpertivy v5....following process: 
User have to upload a pdf file. #1 Step The pdf file have to save into a postsql db (bytea field). #2 Step The user have to open the file from db
What have i done? 
I created a java method. I can save and read a pdf from the postsqldb  but it wont work in the xpert ivy process.
How can i fetch a file over the fileinputstream with the databasestep and write it to the db? How can i read the file over the db step? &lt;/p&gt;
&lt;p&gt;Thank you so much for your help&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/1001/xpertivy-sql-how-can-i-write-a-file-in-a-sql-db" rel="self"></atom:link><language>en</language><lastBuildDate>Mon, 01 Dec 2014 13:39:42 -0500</lastBuildDate><item><title>Answer by MaxIvy</title><link>https://answers.axonivy.com/questions/1001/xpertivy-sql-how-can-i-write-a-file-in-a-sql-db/1075</link><description>&lt;p&gt;Hi Daniel, 
Thank you for your answer. 
I tried this of course, but i wont work. Look at the screen &lt;img alt="alt text" src="http://answers.axonivy.com/upfiles/01-12-_2014_13-31-33.png"&gt; &lt;/p&gt;
&lt;p&gt;Im am not sure whats wrong &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">MaxIvy</dc:creator><pubDate>Mon, 01 Dec 2014 13:39:42 -0500</pubDate><guid>https://answers.axonivy.com/questions/1001/xpertivy-sql-how-can-i-write-a-file-in-a-sql-db/1075</guid></item><item><title>Answer by Daniel Oechslin</title><link>https://answers.axonivy.com/questions/1001/xpertivy-sql-how-can-i-write-a-file-in-a-sql-db/1070</link><description>&lt;p&gt;Hi MaxIvy&lt;/p&gt;
&lt;p&gt;below you see a sample database configuration. You can add your connection as shown in the picture and configure with the parameters you need and add the name- here: building as your myDatabaseContextName.&lt;/p&gt;
&lt;p&gt;Hope this helps
Daniel&lt;/p&gt;
&lt;p&gt;&lt;img alt="Database Config" src="http://answers.axonivy.com/upfiles/contextName.png"&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Daniel Oechslin</dc:creator><pubDate>Mon, 01 Dec 2014 11:41:51 -0500</pubDate><guid>https://answers.axonivy.com/questions/1001/xpertivy-sql-how-can-i-write-a-file-in-a-sql-db/1070</guid></item><item><title>Answer by MaxIvy</title><link>https://answers.axonivy.com/questions/1001/xpertivy-sql-how-can-i-write-a-file-in-a-sql-db/1069</link><description>&lt;p&gt;Thank you su much for your answer and soory that i get a little bit late get back: 
For the moment i got an error in this line IIvyEntityManager entityManager= Ivy.persistence().get(&lt;strong&gt;myDatabaseContextName&lt;/strong&gt;); --&amp;gt; What have i put here inside? &lt;/p&gt;
&lt;p&gt;Thank u so much &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">MaxIvy</dc:creator><pubDate>Mon, 01 Dec 2014 11:11:39 -0500</pubDate><guid>https://answers.axonivy.com/questions/1001/xpertivy-sql-how-can-i-write-a-file-in-a-sql-db/1069</guid></item><item><title>Answer by Daniel Oechslin</title><link>https://answers.axonivy.com/questions/1001/xpertivy-sql-how-can-i-write-a-file-in-a-sql-db/1006</link><description>&lt;p&gt;I guess you did something like this:&lt;/p&gt;
&lt;p&gt;UI-Dialog:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    p:row&amp;gt;
        p:column&amp;gt;
            p:fileUpload label="Upload" fileUploadListener="#{uploadBean.handleFileUpload}" mode="advanced"
                auto="true" /&amp;gt;
        /p:column&amp;gt;
   /p:row&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Due to this is a primeface-action there should be no problem with this.&lt;/p&gt;
&lt;p&gt;Your Java-Class:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

@ManagedBean
@ViewScoped
public class UploadBean {

    public void handleFileUpload(FileUploadEvent event) {
        UploadedFile uploadedFile = event.getFile();
        byte[] myFile= uploadedFile.getContents();
    }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The byte[] you now can save to your database as bytea.&lt;/p&gt;
&lt;p&gt;&lt;img alt="How it works" src="http://answers.axonivy.com/upfiles/primefaces.png"&gt;&lt;/p&gt;
&lt;p&gt;In case you wonder how the persistence could be implemented:&lt;/p&gt;
&lt;p&gt;Dataclass:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;@Entity
@Table(name = "myFunny_FilesTableName")
@Access(AccessType.PROPERTY)
public class Files implements Serializable {

private Long id;
    private static final long serialVersionUID = 1L;
        private byte[] myPdf;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Lob //this annotation is only nescessary if you want it to save as a large object in your database. If you use @Lob //you get a postgres oid. If you just send the byte[] without annotation you get a bytea.
    public byte[] getMyPdf() {
      return myPdf;
    }

    public void set(MyPdf(byte[] myPdf){
       //do sth
     }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Persistence class:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;public class FilesDao {

public Files saveOrUpdate(Files files) throws Exception {
    IIvyEntityManager entityManager= Ivy.persistence().get(myDatabaseContextName);
    Files newFiles;
    try {
        newFiles = entityManager.merge(files);
    } catch (Exception e) {
        Ivy.log().error("Unable to save file.");
        throw e;
    }
    return newFiles;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And now you can add these line to save your files:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;byte[] pdfByteArray;  //the byteArray from the primefaces

Files myPdf= new Files(); //create a new entity
myPdf.setMyPdf(pdfByteArray) //fill it with your data
Files thePersistedPdf= new FilesDao().saveOrUpdate(myPdf); //merge it to your databse.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Hope this helps a bit. &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Daniel Oechslin</dc:creator><pubDate>Tue, 14 Oct 2014 08:38:23 -0400</pubDate><guid>https://answers.axonivy.com/questions/1001/xpertivy-sql-how-can-i-write-a-file-in-a-sql-db/1006</guid></item><item><title>Answer by MaxIvy</title><link>https://answers.axonivy.com/questions/1001/xpertivy-sql-how-can-i-write-a-file-in-a-sql-db/1004</link><description>&lt;p&gt;&lt;img alt="alt text" src="http://answers.axonivy.com/upfiles/2014-10-13_16_17_51-Microsoft_Visio-Zeichnung_(neu)_(2).vsd_-_Microsoft_Visio.png"&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">MaxIvy</dc:creator><pubDate>Mon, 13 Oct 2014 16:18:27 -0400</pubDate><guid>https://answers.axonivy.com/questions/1001/xpertivy-sql-how-can-i-write-a-file-in-a-sql-db/1004</guid></item><item><title>Answer by Daniel Oechslin</title><link>https://answers.axonivy.com/questions/1001/xpertivy-sql-how-can-i-write-a-file-in-a-sql-db/1002</link><description>&lt;p&gt;Hi MaxIvy&lt;/p&gt;
&lt;p&gt;You can do it the very same as explained here: 
&lt;a href="http://jeebestpractices.blogspot.ch/2011/03/save-images-into-database-postgres-with.html"&gt;http://jeebestpractices.blogspot.ch/2011/03/save-images-into-database-postgres-with.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Just adjust the naming a little and remove the conversion part.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Daniel Oechslin</dc:creator><pubDate>Mon, 13 Oct 2014 16:02:55 -0400</pubDate><guid>https://answers.axonivy.com/questions/1001/xpertivy-sql-how-can-i-write-a-file-in-a-sql-db/1002</guid></item></channel></rss>