Re: JDBC + PostgreSQL + LargeObjects

From: Paulo Delgado <pdelgado(at)pasaportevip(dot)com>
To: Justin Clift <justin(at)postgresql(dot)org>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: JDBC + PostgreSQL + LargeObjects
Date: 2002-02-18 20:24:32
Message-ID: 20020218152432.389d2c3f.pdelgado@pasaportevip.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Ok folks, now it works! thank you all! (i'm using a servlet to avoid the white spaces)
Now i have another problem, when the servlet is writing the bytes to the outputstream, the cpu load increases to 100%, i guess the problem is in the for() block, should i look for another way of doing this? or use perl? or what?

check it out:

import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.sql.*;
import org.postgresql.largeobject.*;

public class show_coctel extends HttpServlet
{
public void doGet(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException
{
ServletOutputStream out = response.getOutputStream();
response.setContentType("image/jpeg");
try
{
Class.forName("org.postgresql.Driver");
}
catch(ClassNotFoundException cnfex)
{
cnfex.printStackTrace();
}
try
{
Connection mycon;
mycon= DriverManager.getConnection("jdbc:postgresql://localhost:5432/database", "username" , "password");
mycon.setAutoCommit(false);

// Get the Large Object Manager to perform operations with
LargeObjectManager lobj = ((org.postgresql.Connection)mycon).getLargeObjectAPI();
PreparedStatement ps = mycon.prepareStatement("SELECT pic FROM cocteles WHERE month='"+request.getParameter("m")+"' AND year="+request.getParameter("y"));
ResultSet rs = ps.executeQuery();
if (rs != null) {
while(rs.next()) {
//open the large object for reading
int oid = rs.getInt(1);
LargeObject obj = lobj.open(oid , LargeObjectManager.READ);

//read the data
byte buf[] = new byte[obj.size()];
obj.read(buf, 0, obj.size());

//do something with the data read here
response.setContentLength(obj.size());
int i=0;
for(i=0; i<obj.size() ; i++)
{
out.write(buf[i]);
}
// Close the object
obj.close();
}
rs.close();
}
ps.close();
mycon.close();
}
catch(SQLException sqex)
{
out.println(sqex.toString());
}
}
}

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Dave Cramer 2002-02-18 20:35:41 Re: java.sql.SQLException, message FATAL 1: This connection has been terminated by the administrator
Previous Message Paul Ogden 2002-02-18 20:20:54 java.sql.SQLException, message FATAL 1: This connection has been terminated by the administrator