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.
[CODE=Java]
import java.sql.*;
import java.io.*;
import javax.servlet.* ;
import javax.servlet.h ttp.*;
public class LoadImage extends HttpServlet{
public void doPost(HttpServ letRequest req, HttpServletResp onse res) throws ServletExceptio n, IOException
{
entrance(req, res);
}
public void doGet(HttpServl etRequest req, HttpServletResp onse res) throws ServletExceptio n, IOException
{
entrance(req, res);
}
public void entrance(HttpSe rvletRequest req, HttpServletResp onse res) throws ServletExceptio n, IOException
{
res.setContentT ype("text/html");
PrintWriter out = res.getWriter() ;
String photo=req.getPa rameter("photo" );
Connection conn = null;
PreparedStateme nt ps = null;
try
{
Class.forName( "com.mysql.jdbc .Driver" );
conn = DriverManager.g etConnection("j dbc:mysql://localhost:3306/first","root"," root");
ps = conn.prepareSta tement("INSERT INTO uplo (photo) VALUES( ? )" );
File image = new File( photo );
FileInputStream fis = new FileInputStream ( image );
ps.setBinaryStr eam(1, fis, ( int )image.length() );
// Execute the INSERT
int count = ps.executeUpdat e();
System.out.prin tln( "Rows inserted: " + count );
}
catch( Exception e )
{
e.printStackTra ce();
}
finally
{
try
{
if( ps != null ) ps.close();
if( conn != null ) conn.close();
}
catch( Exception ee )
{
ee.printStackTr ace();
}
}
}
}[/CODE]
[CODE=Java]
import java.sql.*;
import java.io.*;
import javax.servlet.* ;
import javax.servlet.h ttp.*;
public class LoadImage extends HttpServlet{
public void doPost(HttpServ letRequest req, HttpServletResp onse res) throws ServletExceptio n, IOException
{
entrance(req, res);
}
public void doGet(HttpServl etRequest req, HttpServletResp onse res) throws ServletExceptio n, IOException
{
entrance(req, res);
}
public void entrance(HttpSe rvletRequest req, HttpServletResp onse res) throws ServletExceptio n, IOException
{
res.setContentT ype("text/html");
PrintWriter out = res.getWriter() ;
String photo=req.getPa rameter("photo" );
Connection conn = null;
PreparedStateme nt ps = null;
try
{
Class.forName( "com.mysql.jdbc .Driver" );
conn = DriverManager.g etConnection("j dbc:mysql://localhost:3306/first","root"," root");
ps = conn.prepareSta tement("INSERT INTO uplo (photo) VALUES( ? )" );
File image = new File( photo );
FileInputStream fis = new FileInputStream ( image );
ps.setBinaryStr eam(1, fis, ( int )image.length() );
// Execute the INSERT
int count = ps.executeUpdat e();
System.out.prin tln( "Rows inserted: " + count );
}
catch( Exception e )
{
e.printStackTra ce();
}
finally
{
try
{
if( ps != null ) ps.close();
if( conn != null ) conn.close();
}
catch( Exception ee )
{
ee.printStackTr ace();
}
}
}
}[/CODE]
Comment