954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem in uploading image

hi ..problem in uploading image..using servlet to upload image in mysql..use html form so user given the path of image..but giving error.here is the code..help me on this.


import java.sql.*;
import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class LoadImage extends HttpServlet{

public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
entrance(req, res);
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
entrance(req, res);
}
public void entrance(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String photo=req.getParameter("photo");

Connection conn = null;
PreparedStatement ps = null;
try
{
Class.forName( "com.mysql.jdbc.Driver" );

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/first","root","root");
ps = conn.prepareStatement("INSERT INTO uplo (photo) VALUES( ? )" );


File image = new File( photo );
FileInputStream fis = new FileInputStream( image );
ps.setBinaryStream(1, fis, ( int )image.length() );

// Execute the INSERT
int count = ps.executeUpdate();
System.out.println( "Rows inserted: " + count );
}
catch( Exception e )
{
e.printStackTrace();
}
finally
{
try
{
if( ps != null ) ps.close();
if( conn != null ) conn.close();
}
catch( Exception ee )
{
ee.printStackTrace();
}
}
}
}

jatin29
Newbie Poster
5 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You