jatin29 0 Newbie Poster

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();
}
}
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.