0

I want to upload an image into the database but I ran into a problem. Here is my code, Pojo class

public class Pojo {
    String fnm;
    String lnm;
    InputStream b;
    public String getFnm() {
        return fnm;
    }
    public void setFnm(String fnm) {
        this.fnm = fnm;
    }
    public String getLnm() {
        return lnm;
    }
    public void setLnm(String lnm) {
        this.lnm = lnm;
    }
    public InputStream getB() {
        return b;
    }
    public void setB(InputStream b) {
        this.b = b;
    }

}

BL Manager

public class BusinessLogic {
    public boolean insert(Pojo bean)
    {
        boolean check_connection=false;

        try
        {     
            System.out.println("inside Registration");
            Connection conn= (Connection) DBConnection.connect();

            String sql = "INSERT INTO image1 (first_name, last_name, photo) values (?, ?, ?)";
           PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
            ps.setString(1,bean.getFnm());
            ps.setString(2,bean.getLnm());
            ps.setBlob(3, bean.getB());
            ps.executeUpdate();
            check_connection =true;
            conn.close();
        }
       catch(Exception e)
       {
           System.out.println(""+e);
       }
        return check_connection;
    }
}  

Servlet File

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        InputStream inputStream = null; // input stream of the upload file
        Part filePart = request.getPart("photo");
        if (filePart != null) {
            // prints out some information for debugging
            System.out.println(filePart.getName());
            System.out.println(filePart.getSize());
            System.out.println(filePart.getContentType());

            // obtains input stream of the upload file
            inputStream = filePart.getInputStream();
        }
        Pojo p=new Pojo();
        p.setFnm(firstName);
        p.setLnm(lastName);
        if (inputStream != null) {
            // fetches input stream of the upload file for the blob column
            p.setB(inputStream);
        //  statement.setBlob(3, inputStream);
        }
        BusinessLogic bl=new BusinessLogic();
        boolean b=bl.insert(p);
        if(b)
        {
            System.out.println("success");
        }
        else {
            System.out.println("success");
        }
}

Database connection file...

public class DBConnection {

    public static Connection con;
    public static Connection connect()
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/img","root","");
            System.out.println("connected!!");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        return con;
    }
    public static void close() throws SQLException
    {
        if(con!=null)
        {
            con.close();
        }
    }
}

when I try to insert an image to the database it gives me the following error:

java.lang.AbstractMethodError: Method com/mysql/jdbc/PreparedStatement.setBlob(ILjava/io/InputStream;)V is abstract at com.mysql.jdbc.PreparedStatement.setBlob(PreparedStatement.java) at com.image.model.BusinessLogic.insert(BusinessLogic.java:22)

Please help me solve this problem...

Jeroen Rosenberg
  • 4,552
  • 3
  • 27
  • 39
Akshay
  • 11
  • 2

1 Answers1

0

A JAR file named cos.jar includes the com.oreilly.servlet and com.oreilly.servlet.multipart packages. These packages include several classes, such as all of the Java classes that begin with "Multipart," which can be used to handle file uploading in a servlet. You can find its jar and its very easy to integrate.

MultipartRequest m=new MultipartRequest(request,"d:/new");  
out.print("successfully uploaded");  
Ronak Joshi
  • 1,553
  • 2
  • 20
  • 43