Tuesday, 26 February 2013

Retrieving image from database

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%
    Connection connection = null;
    String connectionURL = "jdbc:mysql://localhost:3306/test";
    ResultSet rs = null;
    PreparedStatement psmnt = null;        
    InputStream sImage;
    try
{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connection = DriverManager.getConnection(connectionURL, "root", "");
        psmnt = connection.prepareStatement("SELECT imagefile FROM imageupload WHERE id = ?");
        psmnt.setString(1, "4");
        rs = psmnt.executeQuery();
   if(rs.next())
{
            byte[] bytearray = new byte[1048576];
            int size=0;
            sImage = rs.getBinaryStream(1);
            response.setContentType("image/jpeg");      
            while((size=sImage.read(bytearray))!= -1 )
            {
                response.getOutputStream().write(bytearray,0,size);
}
}
}
    catch(Exception ex)
{
out.println("error :"+ex);
    }
%>

<!-- Table Structure

CREATE TABLE `imageupload` (
`id` bigint(20) NOT NULL auto_increment,
`imagefile` blob NOT NULL,
PRIMARY KEY (`id`)
)

-->

DBT - Models

Models are where your developers spend most of their time within a dbt environment. Models are primarily written as a select statement and ...