Problem in uploading image to to mysql database

Problem in uploading image to to mysql database

Hi, need some help here, i have a program where a user can input name, city and upload image. when a user click the submit button the name, city and the image(Save as BLOB) must be save in the database. and the image will also save in the desired folder. i have no problem in saving the image in the folder, my problem is it can't save the record to mysql database.

heres my code...

index.jsp

<%@ page language="java" %>

<HTML>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>
<BODY> <FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
<br><br><br>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center>
<td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td><b>Name:</b></td>
<td><INPUT NAME="name" TYPE="text"></td>
</tr>
<tr>
<td><b>City:</b></td>
<td><INPUT NAME="city" TYPE="text"></td>
</tr>
<tr>
<td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Send File"> </td>
</tr>
<table>
</center>
</FORM>
</BODY>
</HTML>

upload.jsp

<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.zip.*"%>
<%
    String saveFile = "";
    String contentType = request.getContentType();
    if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
        DataInputStream in = new DataInputStream(request.getInputStream());
        int formDataLength = request.getContentLength();
        byte dataBytes[] = new byte[formDataLength];
        int byteRead = 0;
        int totalBytesRead = 0;
        while (totalBytesRead < formDataLength) {
            byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
            totalBytesRead += byteRead;
        }
        String file = new String(dataBytes);
        saveFile = file.substring(file.indexOf("filename=\"") + 10);
        saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
        saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
        int lastIndex = contentType.lastIndexOf("=");
        String boundary = contentType.substring(lastIndex + 1, contentType.length());
        int pos;
        pos = file.indexOf("filename=\"");
        pos = file.indexOf("\n", pos) + 1;
        pos = file.indexOf("\n", pos) + 1;
        pos = file.indexOf("\n", pos) + 1;
        int boundaryLocation = file.indexOf(boundary, pos) - 4;
        int startPos = ((file.substring(0, pos)).getBytes()).length;
        int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
        File ff = new File("C:/f/" + saveFile);
        FileOutputStream fileOut = new FileOutputStream(ff);
        fileOut.write(dataBytes, startPos, (endPos - startPos));
        fileOut.flush();
        fileOut.close();
%><Br><table border="2"><tr><td><b>You have successfully upload the file:</b>
            <% out.println(saveFile);%></td></tr></table>
            <%
            String Name = request.getParameter("name");
                    String City = request.getParameter("city");
                    if (Name != null && City != null) {
                    Connection connection = null;
                    String connectionURL = "jdbc:mysql://localhost:3306/dropdown";
                    ResultSet rs = null;
                    PreparedStatement psmnt = null;
                    FileInputStream fis;

                        try {
                            Class.forName("com.mysql.jdbc.Driver").newInstance();
                            connection = DriverManager.getConnection(connectionURL, "root", "password");
                            File f = new File("C:/f/" + saveFile);
                            psmnt = connection.prepareStatement("insert into save_image(name, city, image) values(?, ?, ?)");
                            psmnt.setString(1, Name);
                            psmnt.setString(2, City);
                            fis = new FileInputStream(f);
                            psmnt.setBinaryStream(3, (InputStream) fis, (int) (f.length()));
                            int s = psmnt.executeUpdate();
                            if (s > 0) {
                                System.out.println("Uploaded successfully !");
                            } else {
                                System.out.println("Error!");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            %>
View Answers









Related Tutorials/Questions & Answers:
Problem in uploading image to to mysql database
Problem in uploading image to to mysql database  Hi, need some help... be save in the database. and the image will also save in the desired folder. i have no problem in saving the image in the folder, my problem is it can't save
Problem in uploading image to to mysql database
Problem in uploading image to to mysql database  Hi, need some help... be save in the database. and the image will also save in the desired folder. i have no problem in saving the image in the folder, my problem is it can't save
Advertisements
Uploading a Profile with image[file] into a sql database
Uploading a Profile with image[file] into a sql database  I need to upload a file along with the some text into a database ......[ Similar to http... , but upload file to database ] ... Pls help
uploading image in php - PHP
uploading image in php  I am looking for a PHP script to upload image into mysql database
uploading problem
about file into database lib. i use navicat Mysql ... i use this code... <...uploading problem  i use glassfish server.. using netbeans for jsp... curresponding image files start new upload"); %><
error in uploading image from jsp to oracle 10g database
try to insert into the image into the database i got the above error please advice how to get rid of error   JSP upload image in Mysql database...error in uploading image from jsp to oracle 10g database  
Image uploading
Image uploading  Hi,can anyone explain the following code. The code related to uploading an image file to oracle database using java servlet. CODE import java.io.*; import java.io.IOException; import
Save profile and image to mysql database, and view the image in another jsp page
Save profile and image to mysql database, and view the image in another jsp page  Pls. need help in saving the profile info with the image in mysql database.. some basic code pls in jsp... thanks in advance
How to retrieve image from mysql database in JSP?
How to retrieve image from mysql database in JSP?  Hi, I need JSP same codes for learning to get image which is stored in MySQL Database. How to retrieve image from mysql database in JSP? Thanks   Hi, You can write
Image upload in mysql database using jsp servlet
Image upload in mysql database using jsp servlet  Hello, I need code to insert image in mysql database, I have seen the code which is already in your portal but it is not inserting image into database it save in the folder
Uploading an image into the table - JSP-Servlet
Uploading an image into the table  how to upload an image into the table in java  Hi friend, Code to help in solving the problem... to the specified database. */ psmnt = connection.prepareStatement ("insert into save_image
insert name city and upload image in database using mysql and jsp
insert name city and upload image in database using mysql and jsp   insert name city and upload image in database using mysql and jsp
problem in selecting second arraylist from mysql database
problem in selecting second arraylist from mysql database  Hi... in mysql database. In the below program , i have hard-coded that second list, but i want second list to be dynamic from database. Please, help me out. Thank
uploading image in the form
uploading image in the form  Hi All, I am working to build a form like railway registration form which accepts user id and password and image for authentication. And i want all users should be able to see the image once
Image uploading in FTP Server
Image uploading in FTP Server  I want to upload images to a ftp server every 1 hour automatically using java..please help
how to store image in folder and stored image path in mysql database using JSP
how to store image in folder and stored image path in mysql database using JSP  how to store image in folder and stored image path in mysql database using JSP
Problem in uploading java application
Problem in uploading java application  I have uploaded my java application (folder created under webapps) using Filezilla FtpClient.Application... this problem
Uploading Multiple Image On Server?
Uploading Multiple Image On Server?  Hello sir, I am stuck with a problem of uploading multiple images on server. i have done a code which works fine for uploading single image,but it doesn't work with uploading multiple
Uploading a Software and storing in the database
Uploading a Software and storing in the database  I want to upload a software(may be of maximum 20mb) through JSP, and store it in the database. The coding present in the site for uploading and storing in the database
File Uploading Problem
File Uploading Problem  I have a file uploading code but it create problem $(document).ready(function(){ $('#upload').click(function(){ var... it gives the problem org.apache.commons.fileupload.FileUploadException
File Uploading Problem
File Uploading Problem  I have a file uploading code but it create problem $(document).ready(function(){ $('#upload').click(function(){ var... it gives the problem org.apache.commons.fileupload.FileUploadException
change the name of the image after uploading the image
change the name of the image after uploading the image   my form consists of a textbox and uploading the image.after uploading the image i want to change the name of the image with the content of the textbox before saving
how to display image and text in single jsp page from the mysql database
how to display image and text in single jsp page from the mysql database  hello please help me to display the image and text in single jsp page from mysql database if have any reference code please send me Thanks in advance
image is display from path of mysql database
image is display from path of mysql database  <%@ page import="java.io.,java.sql.,java.util.zip.*" %> <% String saveFile=""; String...;% Connection connection = null; String connectionURL = "jdbc:mysql://localhost
Uploading image using jsp
Uploading image using jsp  how to upload image using jsp. Already i tried, But that image file does not read. It returns only -1 without reading...("GET IMAGE PROBLEM :: "+ex); ex.printStackTrace(); } return result; } %> <
How To Store Multilple Images In MySQL database dynamically with image uploader and then retrieve them in image gallery using java
How To Store Multilple Images In MySQL database dynamically with image uploader and then retrieve them in image gallery using java  How To Store Multilple Images In MySQL database dynamically with image uploader
struts image uploading
struts image uploading  please let me know how to upload image in database using struts ..i have written form class and 'm hanging out in action..."); Connection con=DriverManager.getConnection("jdbc:mysql
retrive the employee details with image from mysql database using jsp servlet
retrive the employee details with image from mysql database using jsp servlet  im doing the web project to retrive the employee profile which i stored in the database using jsp servlet then want to show the result in the next jsp
struts image uploading
struts image uploading  please let me know how to upload image in database using struts ..i have written form class and 'm hanging out in action..."); Connection con=DriverManager.getConnection("jdbc:mysql
struts image uploading
struts image uploading  please let me know how to upload image in database using struts ..i have written form class and 'm hanging out in action..."); Connection con=DriverManager.getConnection("jdbc:mysql
struts image uploading
struts image uploading  please let me know how to upload image in database using struts ..i have written form class and 'm hanging out in action..."); Connection con=DriverManager.getConnection("jdbc:mysql://localhost
Uploading an image - JSP-Servlet
Uploading an image  I am doing a jsp project. In this uploading an image is a part. For that i had done a coding for uploading... and uploading is done by different jsp coding. I don't know how to code for file size
insert name city image in database using mysql and jsp
insert name city image in database using mysql and jsp  how to insert name ,city and image in database in mysql and jsp   Here is an example in jsp that insert name, city and image to database. <%@ page import
uploading a text file into a database
uploading a text file into a database  how to upload a text file into a database using jchooser   import java.io.*; import java.sql...."); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost
Insert Image into Mysql Database through Simple Java Code
Insert Image into Mysql Database through Simple Java Code... simple java code that how save image into mysql database. Before running... provides full code for uploading image in database. SaveImageToDatabase.java
Retrieve image from mysql database through jsp
Retrieve image from mysql database through jsp... to retrieve image from mysql database through jsp code. First create a database... Note : In the jsp code given below, image will be retrieved from database
How to insert and retreive an image in mysql database using spring mvc framework???
How to insert and retreive an image in mysql database using spring mvc framework???  I ve solved this problem based on the Product Search Application. Those who need this concept and code segment kindly copy it. Thanks and All
how to upload an image from a jsp page to a mysql database table using jsp
how to upload an image from a jsp page to a mysql database table using jsp  how to upload an image from a jsp page to a mysql database table using jspstrong text
how to retrieve image from mysql database using java and show it in HTML img tag ?
how to retrieve image from mysql database using java and show it in HTML img tag ?  how to retrieve image from mysql database using java and show it in HTML img tag
inserting image in mysql database using browse button in servlets
inserting image in mysql database using browse button in servlets  Hi,I want to insert a image in mysql database using browse button. and, i want to display all inserted images in a form from there i want to download the image
image in database
image in database  how to set image in database and retrieve it using servlet(java)in msaccess
image uploading perminssion in server - JSP-Servlet
image uploading perminssion in server  thanks dear, but i am working in Linux Ubuntu, how can i set the path in server. my problem is i am not able to access the folders wherever we are uploading the files, we are not able
mysql problem - JDBC
mysql problem   hai friends i have some problem with image storing in mysql. i.e while i am using image(blob) for insert the image it says... of creation of table in mysql. it will take any image and store in database
image uploading perminssion in server - JSP-Servlet
image uploading perminssion in server  Dear All, I am facing some problem image uploading in server that i working fine in my system, OS Ubuntu...; Hi friend, Plz give full source code with details to solve the problem
image uploading perminssion in server - JSP-Servlet
image uploading perminssion in server  Thanks deepak following my... to writing image file in this path.. in multipartRequest. if u have any other uploading script please help me.. thanks   Hi, Plz give
Inserting Image in Database Table
This program helps the user to insert an image in the MySQL database table. To insert an image, you need to establish a connection with MySQL database... Inserting Image in Database Table   
Image in mysql
Image in mysql  Hi. How to insert and retrieve images in mysql db using JSP or JAVA Servlet? Thanks in advance
Having problem with image upload....
Having problem with image upload....  I am uploading profile pictures in my site.But after uploading when I visit my home page where it is supposed... is correct.I have renamed the image after uploading with time stamp so as to prevent
problem in database
problem in database   thanks for web site. I want change this code to insert data into PostgreSql database using jsp,servlets. but i getting...){ out.println("Couldn't load database driver: " + e.getMessage()); } catch
database problem
database problem  I did connect my jsp page to database and it showed the result on another jsp page but i want to show the data of that field on which i click to mouse from the jsp page where all the data field has been showed

Ads