images in Database


Web tier: servlets, JSP, Web frameworks: images in Database

  1. images in Database (2 messages)

    Hi ,
    I am facing problem in inserting images in Database through JSP.I am using Blob as the datatype in Oracle.Following
    Following is the code of the JSP
    <body bgcolor=fuschia>
    <h1 align=center><i>EMPLOYEE RECORD</I><br>
    <table bgcolor=lightyellow border=2 align=center>
    <%@ page language ="java" import ="java.sql.*,java.util.*,*" %>
       byte buf[] = new byte[40960];
           try {
               FileInputStream in = new FileInputStream("/Sea1.jpg");
     , 0, 40960);
           catch (Exception e)
            System.out.println("Error: " + e.toString());
       String str = new String(buf, 0);
         Connection conn = DriverManager.getConnection("jdbc:odbc:hello","techno","c");
         Statement st = conn.createStatement();
        str = str.trim();
        String qry= "insert into LOBTABLE values(200 ,EMPTY_BLOB())";
        System.out.println("qry ::" +qry);
        String qry1 = "UPDATE LOBTABLE SET B_LOB = ? WHERE KVAL = 200 " ;
        PreparedStatement pstmt = conn.prepareStatement(qry1);
          System.out.println("Record Added");

    Please let me know if you find the problem in code or help me by giving the source code for inserting and retreiving images from Database.

    Threaded Messages (2)

  2. images in Database[ Go to top ]

    Hi, The following is my code for inserting a file into oracle(blob). I am using this code with weblogic!It gets the file name as input from the HTML form!Try it..and get back to me
    <!--Author: Raghuram Ethirajan-->

    <body bgcolor=#FFFFFF>

    <font color=white>

    <%@ page info="File uploader"%>
    <%@ page import="*"%>
    <%@ page import=""%>
    <%@ page import="java.util.*"%>
    <%@ page import="java.sql.*"%>
    <%@ page import="java.sql.Blob"%>
    <%@ page import="java.sql.PreparedStatement"%>
    <%@ page import="javax.servlet.*"%>
    <%@ page import="javax.servlet.http.*"%>

    //initializing database connection

    String url="jdbc:weblogic:oracle";

    //get parameters from HTML
     String des=request.getParameter("ff");
    File ff=new File(request.getParameter("fn"));
    String fname=ff.getName();
    System.out.println("Name of the file is:" + ff.getName());
    long fi=ff.length();
    int fl;

    System.out.println("size of the selected file is = " + ff.length()+"bytes");

    //reading the binary file and writing into database

    InputStream fin = new FileInputStream(ff);
    Connection con=DriverManager.getConnection(url,"system","manager");
    PreparedStatement pstmt = con.prepareStatement("insert into files (b,fname,fsize,fdes) values(?,?,?,?)");
    pstmt.setBinaryStream (1,fin,fl);

    out.println("<center><font size=5> Data inserted into Database</font></center>");
    //catch Exception

    catch (Exception e) {
    //print error message
    out.println("<center><font size=5> Error in inserting the data</font></center>");
    out.println("the error is:"+e.toString());
    System.out.println("Error is:"+e.toString());

    //print the confirmation message here


  3. Anti-Pattern[ Go to top ]

    Good example of the antipattern of inserting data access into JSP.