I've had nighmares about this since 3 days ago and couldn't find anything by googling so here I am.

As the title says so i need to find a way to store and then retrieve images from a mysql database I have tired the code below to do so (the storing part) but it just saves empty blobs.

I'd apreciate the help and since i haven't seen much about the topic so will a lot of people

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            String description = null;

            MyImage image= new MyImage();

                //Download com.oreilly package
                MultipartRequest multi= new MultipartRequest(request,".",5*1024*1024);
                Enumeration files=multi.getFileNames();
                File file=null;

                for(int i = 0 ; files.hasMoreElements() ; i++)
                    String name=(String)files.nextElement();
                    String filename=multi.getFilesystemName(name);
                    String type=multi.getContentType(name);
                    description = multi.getParameter("description");

                    out.println("The File is "+archivo.getCanonicalPath() + "<br>");

                    BufferedImage img = ImageIO.read(file);
                    ByteArrayOutputStream bas = new ByteArrayOutputStream();

                    ImageIO.write(img, "pnm", bas);
                    byte[] data = bas.toByteArray();

                    if(i == 0){
                    if(i == 1){


               String error = validateFields();

                        EntityManager em = MyEntityManager.getEntityManager();
                    }catch(Exception e){
                        session.setAttribute("Error", e.getMessage());
                    session.setAttribute("Error", error);
            }catch(Exception e){
                session.setAttribute("Error", e.getMessage());

Have look at this example. If you modify code correctly you will be able to select image to upload through JSP and submit it to servlet for database insertion

Thanx Peter_budo I had already seen an example like that one but this one is easier to modify, I'll give it a try

hey m a biotechnologist. i want to develop a database . can u tell what would be easy to learn.. JSP or PHP???

I will say PHP if you looking for faster learning and development cycle as JSP require quite extensive knowledge of Java to be able to comprehend and correctly use other Java web based technologies.
On other hand Java is more secure and provide large scope of add-on technologies that may increase your productivity

It depends a lot on you background if you already know java it'll be much easier with jsp but if you're starting from zero php is better, more flexible and there's more documentation about it

This article has been dead for over six months. Start a new discussion instead.