Click here to Skip to main content
15,886,774 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have one problem regarding my project.can anyone please tell me how to save the image path into database.i have already create the upload button and using FILE i have get the image path.but unfortunately i couldn't store it into database.reply me asap.this is urgent for me.
Posted
Comments
Richard MacCutchan 16-Dec-13 13:51pm    
The same way you store anything into the database. Create a column in the table of type NVARCHAR or similar, and store the value there.
nuke_infer 16-Dec-13 14:12pm    
dear fren
if you have any code segment please give me the url then it will be help full to me.i have tried but it is not passing as i expected.thats the problem for me
H.Brydon 16-Dec-13 22:01pm    
I think Google is going to be more helpful for this kind of question than anything you will see here...
Richard MacCutchan 17-Dec-13 4:45am    
Well, unless you show us the code and explain exactly what is happening we cannot offer any suggestions. If you do not know how to use databases from Java then I suggest you work through this tutorial.

Image upload java code in jsp-servlet
C#
Connection class
public class DBConnection {
public Connection conn=null;
     void getConnection()
    {
         try {
             Class.forName("com.mysql.jdbc.Driver");
             }
         catch (ClassNotFoundException ex)
         {

         }
         try {

              conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/ImageProcessing", "root","root");
             } catch (Exception ex)
             {

             }
    }
}


Database Connection

Java
public class DataBase {
	 static String strDB;
	
	public DataBase(String str) throws SQLException
	
	{
		DBConnection obj = new DBConnection();
		obj.getConnection();
		String str1=str;
		
		PreparedStatement pstmt = obj.conn
				.prepareStatement("insert into imagestore (ImagePath,row) values(?,?)");
		int i=1;
		pstmt.setString(1, str1);
		pstmt.setInt(2,i );
		pstmt.executeUpdate();
		
		Statement stmt = obj.conn.createStatement();
	      String query = "SELECT ImagePath FROM ImageStore where row=1";
	      ResultSet rs =  stmt.executeQuery(query);
	  
	      while(rs.next())
	      {
	    	  strDB=rs.getString(1);
	      }
	      Statement stmt1 = obj.conn.createStatement();
	      String query1 = "delete from ImageStore where row=1";
	  stmt1.executeUpdate(query1);
	      

	}

}


Image pathUpload servled code
Java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;




public class FirstImage extends HttpServlet{
	  static  String filePath ;

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		   File file ;
		   int maxFileSize = 5000 * 1024;
		   int maxMemSize = 5000 * 1024;
		   String fileName = null;
		   DBConnection obj = new DBConnection();
			obj.getConnection();
		   ServletContext context = getServletContext();
		   filePath =  getServletContext().getRealPath("/images");
		   System.out.println(filePath);
		   response.setContentType("text/html");
		  
		   // Verify the content type
		   String contentType = request.getContentType();
		   if ((contentType.indexOf("multipart/form-data") >= 0)) {

		      DiskFileItemFactory factory = new DiskFileItemFactory();
		      // maximum size that will be stored in memory
		      factory.setSizeThreshold(maxMemSize);
		      // Location to save data that is larger than maxMemSize.
		      factory.setRepository(new File("C:\temp"));

		      // Create a new file upload handler
		      ServletFileUpload upload = new ServletFileUpload(factory);
		      // maximum file size to be uploaded.
		      upload.setSizeMax( maxFileSize );
		      try{ 
		         // Parse the request to get file items.
		         List fileItems = upload.parseRequest(request);
		         // Process the uploaded file items
		         Iterator i = fileItems.iterator();
		         
		        
		        
		         while ( i.hasNext () ) 
		         {
		            FileItem fi = (FileItem)i.next();
		            if ( !fi.isFormField () )	
		            {
		            // Get the uploaded file parameters
		            String fieldName = fi.getFieldName();
		             fileName = fi.getName();
		            boolean isInMemory = fi.isInMemory();
		            long sizeInBytes = fi.getSize();
		            // Write the file
		            if( fileName.lastIndexOf("\\") >= 0 ){
		            file = new File( filePath +"/"+ 
		            fileName.substring( fileName.lastIndexOf("\\"))) ;
		            }else{
		            file = new File( filePath +"/"+ 
		            fileName.substring(fileName.lastIndexOf("\\")+1)) ;
		            }
		            fi.write( file ) ;
		            
		            }
		         }
		         }catch(Exception ex) {
		         System.out.println(ex);
		      }
		      
		   }else{
		     System.out.println("no file uploaded");
		   }
		   try {
			new DataBase(fileName);

          }

}

in database create two columan row(number) and Imagepath(varchar(100))
 
Share this answer
 
 
Share this answer
 
Comments
nuke_infer 17-Dec-13 1:57am    
but i want not web base it is desktop application.can you please give me another solution this is ok for my reference purpose

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900