How to store images in database using java

hii
how to store images in database using java..........
Message was edited by:
naveennaga

I asked a similar question once and the answer I got was - Don't.
Instead, store the images on a drive somewhere and store the path in the database.
But I'm no expert - that's just what I was told by someone who does this kind of stuff a lot.
Hope that helps.

Similar Messages

  • How to store images in database and retrieve them back to page?

    Well I don't know how to store an image file to a database (say MSSQL) from the JSP and retrieve it back whenever needed and put it on the JSP page? Please help me.

    I am not sure how to store images in database but what you can do is store the image into particular folder using FileOutputStream and its unique name into the database...and than retrieve it from the folder using that name retrieved from database...
    <%
    response.setContentType("text/html");
    response.setHeader("Cache-control","no-cache");
    String err = "";
    String lastFileName = "";
    String contentType = request.getContentType();
    String boundary = "";
    final int BOUNDARY_WORD_SIZE = "boundary=".length();
    System.out.println("contentType --> "+contentType);
    System.out.println("BOUNDARY_WORD_SIZE --> "+BOUNDARY_WORD_SIZE);
    if(contentType == null || !contentType.startsWith("multipart/form-data"))
    err = "Ilegal ENCTYPE : must be multipart/form-data\n";
    err += "ENCTYPE set = " + contentType;
    else
    boundary = contentType.substring(contentType.indexOf("boundary=") + BOUNDARY_WORD_SIZE);
    System.out.println("boundary --> "+boundary);
    boundary = "--" + boundary;
    try
    ServletInputStream sis = request.getInputStream();
    byte[] b = new byte[1024];
    int x=0;
    int state=0;
    String name=null,fileName=null,contentType2=null;
    java.io.FileOutputStream buffer = null;
    while((x=sis.readLine(b,0,1024))>-1)
         System.out.println("************ x ********** "+x);
         String s = new String(b,0,x);
                   System.out.println("************ s ********** \n"+s);
         if(s.startsWith(boundary))
         state = 0;
         System.out.println("name="+name);
         System.out.println("filename="+fileName);
         name = null;
         contentType2 = null;
         fileName = null;
         else if(s.startsWith("Content-Disposition") && state==0)
              System.out.println("-- 1 --");
              state = 1;
              System.out.println("s.indexOf(filename=) --> "+s.indexOf("filename="));
              if(s.indexOf("filename=") == -1)
                   name = s.substring(s.indexOf("name=") + "name=".length(),s.length()-2);
                   System.out.println("after name substring 1 "+name);
              else
                   name = s.substring(s.indexOf("name=") + "name=".length(),s.lastIndexOf(";"));
                   System.out.println("after name substring 2 "+name);
                   fileName = s.substring(s.indexOf("filename=") + "filename=".length(),s.length()-2);
                   System.out.println("fileName --> "+fileName);
                   //String fileName1 = s.substring(s.indexOf("filename=") + "filename=".length(),s.length());
                   //System.out.println("fileName1 -->"+fileName1);
                   if(fileName.equals("\"\""))
                   fileName = null;
                   else
                        String userAgent = request.getHeader("User-Agent");
                        System.out.println("userAgent --> "+userAgent);
                        String userSeparator="/"; // default
                        if (userAgent.indexOf("Windows")!=-1)
                        System.out.println("test --> "+"\\");
                        userSeparator="\\";
                        if (userAgent.indexOf("Linux")!=-1)
                        userSeparator="/";
                        System.out.println("userSeparator "+userSeparator);
                        System.out.println("fileName before inserting userSeparators "+fileName);
                        fileName = fileName.substring(fileName.lastIndexOf(userSeparator)+1,fileName.length()-1);
                        System.out.println("fileName after userSeparators "+fileName);
                        if(fileName.startsWith( "\""))
                        fileName = fileName.substring( 1);
              name = name.substring(1,name.length()-1);
              System.out.println("name 2 --> "+name);
              System.out.println("final file name "+fileName);
              if (name.equals("file"))
                   if (buffer!=null)
                   buffer.close();
                   lastFileName = fileName;
                   buffer = new java.io.FileOutputStream("/Documents and Settings/sunil/Desktop/images/"+fileName);
         else if(s.startsWith("Content-Type") && state==1)
                             System.out.println("-- 2 --");
              state = 2;
              contentType2 = s.substring(s.indexOf(":")+2,s.length()-2);
              System.out.println("contentType2 --> "+contentType2);
         else if(s.equals("\r\n") && state != 3)
                   System.out.println("-- 3 --");
              state = 3;
         else
              System.out.println("-- 4 --");     
              if (name.equals("file"))
              System.out.println("Final x :: "+x);     
              buffer.write(b,0,x);
    }     // while closing
    sis.close();
    buffer.close();
    }catch(java.io.IOException e)
    err = e.toString();
    boolean ok = err.equals("");
    if(!ok)
    out.println(err);
    else
    %>
              <SCRIPT language="javascript">
              history.back(1);
              alert('Uploaded <%=lastFileName%>');
              window.location.reload(false);
              </SCRIPT>
    <%
         out.println("done");
    %>
    </BODY>
    </HTML>
    I think it will solve ur problem..

  • How to store images in database

    Can anyone help me with storing images in database, problem with me is that i m designing a site in which u need to display product information and administrator can add and delete products. now how can i make that administrator can add a image to database, Can u tell me that can image itself can be stored in a database or just the path of it can be stored in a database.
    Waiting for quick reply,
    thanks

    can u plss tell me how to store the path of the image in database , do we just have to mention it as varchar field and write the path thats it or do we have to do anything else . and pls also tellme when do we have to set connections ? only at the time to retrieving the images since we are not uploading the image sin DB just mentioning their path . pls reply ASAP

  • How t store images in database

    hi
    i want coding to store images,files in database

    hi,
    You can use the BLOB data type provided by oracle to store images in the database. BLOB (Binary Large Object). To Store data into the table, we need to use the DBMS_LOB package to manipulate the data and then store it in the table.
    BFILE are used in combination with the BLOB to get the images into the database.
    Thanks, VIkram

  • How to display image in database using php

    i've try search all hope that you all can give me some guidance how to display data that is in image or BLOB using PHP. just a query and php code that will make display of the image. hope that you all show me the example or give me reference that i need to solve my problem.

    Hi,
    Have a check on these Google results, you might find something there...
    http://www.google.com/search?sourceid=navclient-ff&ie=UTF-8&q=%22display%20image%22%2Boracle%20%2Bphp

  • How to display image in database using html or php

    i've try already to work this thing but i can't do it. anyone that can help me or give me any example or reference to resolve my problem to retrieve or display image(blob) from database. hope that all of you help me

    Using JHeadstart, you can display a BLOB column from the database in HTML, by way of ADF. Check out the JHeadstart Tutorial at http://www.oracle.com/technology/products/jdev/tips/muench/jhstutorial/index.html to get started, then do the same for your own database tables that include the BLOB column. JHeadstart will take care of rendering it as a file upload or image, depending on the display type you set at the attribute level.
    Hope this helps,
    Sandra Muller
    JHeadstart Team
    Oracle Consulting

  • How to store images in MAX DB

    hi,
    can u pls tell how to store images in MAXDB using java.......
    regards
    Guru

    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/f5d3c40a-0801-0010-55b7-9e3b01745a40

  • How to store file content in BLOB field MySql database using java

    Hi!
    i want to store the file content in a BLOB field in MySql database using java.
    Please help me out..........
    thanx in advance...
    bye

    i stored images in db, and retrieved them. like that cant i store pdf file in db, and retrieve it back using oracle db?
    Plz help me out how to put a file in db. i need complete code. thanks in advance.

  • How To Store pdf or doc file in Oracle Database using Java Jdbc?

    can any one help me out How To Store pdf or doc file in Oracle Database using Java Jdbc in JSP/Serlet? i tried like anything. using blob also i tried. but i am able 2 store images in DB not files. please if u know or else if u have some code like this plz send that to me, and help me out plz. i need that urgent.

    Hi.. i am not getting error, But i am not getting the original contents from my file. i am getting all ASCII vales, instead of my original data. here i am including my code.
    for Adding PDF in DB i used image.jsp
    Database table structure (table name. pictures )
    Name Null? Type
    ID NOT NULL NUMBER(11)
    IMAGE BLOB
    <%@ page language="java" import="java.util.*,java.sql.*,java.io.*" pageEncoding="ISO-8859-1"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <%
    try{
         Class.forName("oracle.jdbc.driver.OracleDriver");
         Connection con=DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.135:1521:orcl","scott","tiger");
         PreparedStatement ps,pstmt,psmnt;
         ps = con.prepareStatement("INSERT INTO pictures VALUES(?,?)");
    File file =
    new File("D:/info.pdf");
    FileInputStream fs = new FileInputStream(file);
    ps.setInt(1,4);
    ps.setBinaryStream(2,fs,fs.available());
    int i = ps.executeUpdate();
    if(i!=0){
    out.println("<h2>PDF inserted successfully");
    else{
    out.println("<h2>Problem in image insertion");
    catch(Exception e){
    out.println("<h2>Failed Due To "+e);
    %>
    O/P: PDF inserted successfully
    i tried to display that pdf using servlet. i am giving the code below.
    import java.io.IOException;
    import java.sql.*;
    import java.io.*;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    public class DispPDF extends HttpServlet {
         * The doGet method of the servlet. <br>
         * This method is called when a form has its tag value method equals to get.
         * @param request the request send by the client to the server
         * @param response the response send by the server to the client
         * @throws ServletException if an error occurred
         * @throws IOException if an error occurred
         public void service(HttpServletRequest request, HttpServletResponse response)
                   throws ServletException, IOException {
              //response.setContentType("text/html"); i commented. coz we cant use response two times.
              //PrintWriter out = response.getWriter();
              try{
                   InputStream sPdf;
                   Class.forName("oracle.jdbc.driver.OracleDriver");
                        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.135:1521:orcl","scott","tiger");
                        PreparedStatement ps,pstmt,psmnt;
                   psmnt = con.prepareStatement("SELECT image FROM pictures WHERE id = ?");
                        psmnt.setString(1, "4"); // here integer number '4' is image id from the table.
                   ResultSet rs = psmnt.executeQuery();
                        if(rs.next()) {
                   byte[] bytearray = new byte[1048576];
                        //out.println(bytearray);
                        int size=0;
                        sPdf = rs.getBinaryStream(1);
                        response.reset();
                        response.setContentType("application/pdf");
                        while((size=sPdf.read(bytearray))!= -1 ){
                        //out.println(size);
                        response.getOutputStream().write(bytearray,0,size);
                   catch(Exception e){
                   System.out.println("Failed Due To "+e);
                        //out.println("<h2>Failed Due To "+e);
              //out.close();
    OP
    PDF-1.4 %âãÏÓ 2 0 obj <>stream xœ+är á26S°00SIá2PÐ5´1ôÝ BÒ¸4Ü2‹ŠKüsSŠSŠS4C²€ê P”kø$V㙂GÒU×713CkW )(Ü endstream endobj 4 0 obj <>>>/MediaBox[0 0 595 842]>> endobj 1 0 obj <> endobj 3 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj xref 0 7 0000000000 65535 f 0000000325 00000 n 0000000015 00000 n 0000000413 00000 n 0000000168 00000 n 0000000464 00000 n 0000000509 00000 n trailer <<01b2fa8b70ac262bfa939cc786f8770c>]/Root 5 0 R/Size 7/Info 6 0 R>> startxref 641 %%EOF
    plz help me out.

  • How to store image in the oracle database 10.2. using File Maker 10.

    Hi.,
    I want to store image, media file in the oracle database using File Maker as a front end.
    I connect Oracle using odbc from file maker. There are table and in that table there a column "pict" of blob type. but this column is not showing in the file maker.
    2. Here I can not change the data type of any column.
    Now how to store images in the oracle table using odbc or any other tool.
    Regards,
    Shyam

    I wrote an example for my students, you can find it here. It's using PHP as the front end but all you need to do is know how and leverage the stored procedures. All code is downloadable in zip files from the blog.
    http://blog.mclaughlinsoftware.com/php-programming/oracle-lob-processing/

  • How to read the data from Excel file and Store in XML file using java

    Hi All,
    I got a problem with Excel file.
    My problem is how to read the data from Excel file and Store in XML file using java excel api.
    For getting the data from Excel file what are all the steps i need to follow to get the correct result.
    Any body can send me the code (with java code ,Excel sheet) to this mail id : [email protected]
    Thanks & Regards,
    Sreenu,
    [email protected],
    india,

    If you want someone to do your work, please have the courtesy to provide payment.
    http://www.rentacoder.com

  • How do you query Oracle RDF database using Java program?

    Does anyone know how to get data out of the oracle RDF database using Java?
    I'm running the following java code and it returns basically null values. My guess is that the get_triple() function returns a dataset other than resultset. I've tried searching the Oracle web site. Am I supposed to be using CLOB object? Any help would be appreciated. Thanks
    sql = "SELECT a.triple.GET_TRIPLE() AS triple FROM family_rdf_data a";
    ResultSet rs = stmt.executeQuery( sql ) ;
    while ( rs.next() )
    System.out.println( rs.getString(1) ) ;
    rs.close() ;

    get_triple() returns an object, and the code would be different from the code of retrieving a string. Some sample code for retrieving objects is at http://www.oracle.com/technology/sample_code/tech/java/sqlj_jdbc/files/advanced/advanced.html
    get_subject(), get_property() return strings so the code below should work. get_object() returns a CLOB so different handling would be required and there is some sample code at the link above.
    Melli

  • Is It Possible to store an entire  file in database using java?

    Hi All,
    I am new to Programming. please help he out Is It Possible to store an entire file in database using java? is possible, can u tell me the way to make this success? Thank you in advance.

    Thanks PhHein
    i got that application. i stored one file in db, and i displayed that in browser. thank u very much.

  • How to create sql database using java frame or appelet?

    hi ! i am working on database project i want to create a database using java frame or applet where it asks user to select the location for database to be created , after user have specified the path then the programs creates the database, again i want that database to be read and write by another frame or applet but as user select the path how do i make the connectivity. i just have basic knowledge on java. please give me idea how can i process further.
    thanks a lot

    While duffymo is correct in regard to most major database products, it's my understanding (warning! wild-ass guess coming) that the Hypersonic DB is more "application-centric" and the dynamic creation of databases is part of its design. It's pure Java database software, and therefore is not appropriate for all database projects, in particular those that require extremely high-performance.
    See http://hsqldb.org/
    I've not used it yet (but soon though), and I can't really advise anyone about it.
    However, I'm wondering if you phrased you question in a way that is confusing us. To most of us in casual conversation, a "database" is both (1) a large organized collection of data and (2) the software that is used to organize and access it. However, the phrase "create a database" usually means creating a (1) database (a collection of data) using an already created (2) database software, such as Oracle, MySQL, DB2, HSQDB, etc., etc. If you'r question is, how do a create some new database software using Java, the answer is that this is a very very big and hard thing to do for the general case and probably not something you want to be doing.

  • How to restore a mysql dump file to database using java

    hai friends,
    i'm developing an application. in that i need to back up and restore the database. i'm using mysql.
    in that i used a code to back up the db.. its working perfectly and the dump file is created perfectly..
    but while trying to restore the dump file nothing is happening...
    can anyone provide me a sample code to restore the dump to database using java code...
    thanks in advance...

    The first, biggest mistake you made in the code is that you didn't read standard output and error from the process, and/or you're ignoring exceptions. Because if you did, you would have read error messages that would have given you a clue what's wrong. (either that, or you ignored the clues, or didn't understand them and failed to tell us what the error messages are)
    The second mistake is that you invoked exec with the call that takes a single string, when that string includes whitespace not used as delimiters.
    The third mistake was that you used a terrible subject line for your post. Your problem is that you're having trouble with Runtime.exec. The fact that it's about mysql isn't relevent.
    The fourth mistake is that you didn't mention Runtime.exec at all until pushed. You should provide these details. When you try to self-diagnose something that you don't understand, and then hide information, you're just wasting everybody's time, including your won.
    The fifth mistake is that you're using poor spelling.
    The sixth mistake is that you still haven't said why you're doing this in Java.

Maybe you are looking for