Re: JDBC + PostgreSQL + LargeObjects
От | Paulo Delgado |
---|---|
Тема | Re: JDBC + PostgreSQL + LargeObjects |
Дата | |
Msg-id | 20020218152432.389d2c3f.pdelgado@pasaportevip.com обсуждение исходный текст |
Ответ на | Re: JDBC + PostgreSQL + LargeObjects (Justin Clift <justin@postgresql.org>) |
Список | 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%, iguess 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()); } } }
В списке pgsql-jdbc по дате отправления: