Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How to insert BLOB programmatically?

    Hi!

    I have a database table with BLOB field and the corresponding ListGrid and DynamicForm for CRUD operations that work by the book. But, when I try to insert BLOB programmatically, in the batch process, I get an error that indicates to me that I'm doing something wrong, or that I don't know how to do it right.

    So, I have a Spring scheduled task that reads e-mail and saves it to the database, and what I cannot do is to save an attachment.

    Environment: SmartGWT 2.5 build 2011-06-30, GWT 2.2, Java 1.6, PostgreSQL 9.0

    My table:
    Code:
     
    CREATE TABLE upit (
    	  upit_id			SERIAL
            ....
    	, attachment			BYTEA
    	, attachment_filesize		INTEGER
    	, attachment_filename	VARCHAR(255)
    	, attachment_date_created	TIMESTAMP
            ....
    );
    My datasource:
    Code:
     
    <DataSource 
    	dbName="PostgreSQL"
    	tableName="upit"
    	ID="upitDS"
    	serverType="sql"
    	autoDeriveSchema="true"
    >
    	<fields>
    		<field name="upit_id"  type="sequence" hidden="true" primaryKey="true" sequenceName="upit_upit_id_seq"></field>
                   ........
    		<field name="attachment" 	type="binary"   title="Privitak uz mail"     required="false"></field>
                   ........
    	</fields>
    </DataSource>
    My Java code:
    Code:
     
                    byte[] data = ... // read from e-mail
                    String filename = ... // read from e-mail
    		DSRequest req = new DSRequest("upitDS", "add");
    
    		Map<String, Object> m2 = new HashMap<String, Object>();
    		m2.put("attachment", data);
    		m2.put("attachment_filename", filename);
    		m2.put("attachment_filesize", data.length);
    		m2.put("attachment_date_created", new Date());
    
    		req.setValues(m2);
    		try {
    			DSResponse res = req.execute();
    			System.out.println("Proslo: " + res.getStatus());
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    And this is what I got:
    Code:
     
    === 2011-07-18 10:50:09,124 [ad-1] WARN  SQLDriver - [builtinApplication.add_zaprimi_upit] DSRequest has no RPCManager set when testing if we should join a transaction - transaction will not be joined
    === 2011-07-18 10:50:09,124 [ad-1] DEBUG PoolableSQLConnectionFactory - [builtinApplication.add_zaprimi_upit] DriverManager fetching connection for PostgreSQL via jdbc url jdbc:postgresql://localhost:5432/crm_db
    === 2011-07-18 10:50:09,124 [ad-1] DEBUG PoolableSQLConnectionFactory - [builtinApplication.add_zaprimi_upit] Passing credentials getConnection separately from JDBC URL
    === 2011-07-18 10:50:09,126 [ad-1] DEBUG PoolableSQLConnectionFactory - [builtinApplication.add_zaprimi_upit] Returning pooled Connection
    === 2011-07-18 10:50:09,126 [ad-1] INFO  SQLDriver - [builtinApplication.add] Executing SQL update on 'PostgreSQL': INSERT INTO upit (attachment, attachment_date_created, attachment_filename, attachment_filesize, upit_id) VALUES (?, '2011-07-18 10:50:08', 'my_image.JPG', 226432, nextval('upit_upit_id_seq'))
    === 2011-07-18 10:50:09,131 [ad-1] INFO  SQLDriver - [builtinApplication.add] Execute of update: INSERT INTO upit (attachment, attachment_date_created, attachment_filename, attachment_filesize,upit_id) VALUES (?, '2011-07-18 10:50:08', 'my_image.JPG', 226432, nextval('upit_upit_id_seq')) on db: PostgreSQL threw exception: org.postgresql.util.PSQLException: No value specified for parameter 1. - assuming stale connection and retrying update.
    === 2011-07-18 10:50:09,131 [ad-1] DEBUG PoolableSQLConnectionFactory - [builtinApplication.add_zaprimi_upit] DriverManager fetching connection for PostgreSQL via jdbc url jdbc:postgresql://localhost:5432/crm_db
    === 2011-07-18 10:50:09,131 [ad-1] DEBUG PoolableSQLConnectionFactory - [builtinApplication.add_zaprimi_upit] Passing credentials getConnection separately from JDBC URL
    === 2011-07-18 10:50:09,133 [ad-1] DEBUG PoolableSQLConnectionFactory - [builtinApplication.add_zaprimi_upit] Returning pooled Connection
    === 2011-07-18 10:50:09,134 [ad-1] DEBUG DataSourceDMI - Invocation threw exception
    org.postgresql.util.PSQLException: No value specified for parameter 1.
    	at org.postgresql.core.v3.SimpleParameterList.checkAllParametersSet(SimpleParameterList.java:178)
    	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:246)
    	at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
    	at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
    	at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:334)
    	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
    	at com.isomorphic.sql.SQLDriver.doUpdate(SQLDriver.java:471)
    	at com.isomorphic.sql.SQLDriver.update(SQLDriver.java:436)
    	at com.isomorphic.sql.SQLDriver.executeUpdate(SQLDriver.java:545)
    	at com.isomorphic.sql.SQLDataSource.executeNativeUpdate(SQLDataSource.java:388)
    	at com.isomorphic.sql.SQLDataSource.SQLExecute(SQLDataSource.java:1336)
    	at com.isomorphic.sql.SQLDataSource.processRequest(SQLDataSource.java:280)
    	at com.isomorphic.sql.SQLDataSource.executeAdd(SQLDataSource.java:232)
    	at com.isomorphic.datasource.DataSource.execute(DataSource.java:1156)
    	at com.isomorphic.application.AppBase.executeDefaultDSOperation(AppBase.java:721)
    	at com.isomorphic.application.AppBase.executeAppOperation(AppBase.java:658)
    	at com.isomorphic.application.AppBase.execute(AppBase.java:491)
    	at com.isomorphic.datasource.DSRequest.execute(DSRequest.java:1653)
    	at com.dentalvukic.crm.server.dmi.UpitDMI.addZaprimiUpit(UpitDMI.java:62)
    It is not the full stack trace, I know, but it is obvious that prepared statement for INSERT is not fully populated, i.e., the field 'attachment' doesn't get populated with value.

    I would appreciate any help.

    Cheers

    #2
    You would need to put an InputStream into the HashMap, not just a filename.

    Comment


      #3
      Originally posted by Isomorphic
      You would need to put an InputStream into the HashMap, not just a filename.
      I did this, as I understood your advice:
      Code:
       
      
      	byte[] data = ... // read from attachment
      	m2.put("attachment", new ByteArrayInputStream(data));
              ....
      and the result is the same.

      Comment


        #4
        Just to mention, I tried your advice once again after some time and now it works.
        Changes: GWT 2.3 and SmartGWT 2.5 build 2011-08-28.

        Originally posted by neunistivlija
        I did this, as I understood your advice:
        Code:
         
        
        	byte[] data = ... // read from attachment
        	m2.put("attachment", new ByteArrayInputStream(data));
                ....
        and the result is the same.

        Comment

        Working...
        X