Re: JDBC + PostgreSQL + LargeObjects - Mailing list pgsql-jdbc
From | Marc Lavergne |
---|---|
Subject | Re: JDBC + PostgreSQL + LargeObjects |
Date | |
Msg-id | 3C716627.9050602@richlava.com Whole thread Raw |
In response to | JDBC + PostgreSQL + LargeObjects (Paulo Delgado <pdelgado@pasaportevip.com>) |
Responses |
Re: JDBC + PostgreSQL + LargeObjects
|
List | pgsql-jdbc |
I would just change this: ... int i=0; for(i=0; i<obj.size() ; i++) { out.write(buf[i]); } ... to this: ... out.write(buf); out.flush(); out.close(); ... Writing it out 1 byte at a time would be way too inefficient and is probably why your CPU pegs. Cheers, Marc Paulo Delgado wrote: > 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()); > } > } > } > > ---------------------------(end of broadcast)--------------------------- > TIP 2: you can get off all lists at once with the unregister command > (send "unregister YourEmailAddressHere" to majordomo@postgresql.org) > > -- 01010101010101010101010101010101010101010101010101 Marc P. Lavergne [wk:650-576-7978 hm:407-648-6996] Senior Software Developer Global Knowledge Management Worldwide Support Technologies Openwave Systems Inc. -- "Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network." -Tim Berners-Lee (Technology Review, July 1996) 01010101010101010101010101010101010101010101010101
pgsql-jdbc by date: