Re: import_bytea function

From: Stephen Davies <sdavies(at)sdc(dot)com(dot)au>
To: Thomas Kellerer <spam_eater(at)gmx(dot)net>, pgsql-general(at)postgresql(dot)org
Subject: Re: import_bytea function
Date: 2016-10-08 00:57:32
Message-ID: fe5e9880-68da-c3c4-2951-1c5cf2870967@sdc.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 07/10/16 19:24, Thomas Kellerer wrote:
> Stephen Davies schrieb am 07.10.2016 um 10:46:
>>> You can store the contents of a file in a bytea using plain JDBC no lo_import() required
>>>
>>> String sql = "insert into images (id, image_data) values (?,?)";
>>> Connection con = ....;
>>> File uploaded = new File("...");
>>> InputStream in = new FileInputStream(uploaded);
>>> PreparedStatement pstmt = con.prepareStatement(sql);
>>> pstmt.setInt(1, 42);
>>> pstmt.setBinaryStream(in, (int)uploaded.length());
>>> pstmt.executeUpdate();
>>>
>>> This *only* works with bytea column, not with "large objects".
>>>
>>> In production code you obviously need to close all resources and handle errors.
>>> I left that out for simplicity.
>
>> That looks reasonable but I need to update rather than insert and my similar
>> code with sql="update part set pic=? where id=3" did not work.
>
> That *will* work (using that myself for updates as well).
>
> What exactly is your problem? What was the error/exception?
>
>
>
>
>
I will have to regenerate that code to get the exact error message text but it
basically said that the parameter substitution was invalid.

A follow-up question.
Once the bytea column is populated, how best to display the content in a web
page?

I have :

byte [] imgB;
ResultSet rs = st1.executeQuery("select pic from part where pno='" + p + "'");
if(rs.next()){
imgB = rs.getBytes(1);
if (imgB != null){
out.write("Content-type: image/jpeg");
out.write("Content-length: " + (int)imgB.length);
out.write(imgB.toString());
}
}

but this does not work.
The toString() looks wrong but removing it makes the write fail.

Cheers and thanks,
Stephen

--
=============================================================================
Stephen Davies Consulting P/L Phone: 08-8177 1595
Adelaide, South Australia. Mobile:040 304 0583

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Stephen Davies 2016-10-08 06:36:14 Re: import_bytea function
Previous Message Natan Abolafya 2016-10-07 21:54:28 BDR: changing dsn on a running node