简体   繁体   中英

Inserting image to database using servlet from jsp form using setBlob

I keep getting abstract method error while using setBlob()

I searched on this forum as well but none of the answers solved my issue.

I am using :

jdk 1.7

Oracle 11g

Apache Tomcat 8.0

FileUploadDBServlet.java

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet("/uploadServlet")
@MultipartConfig(maxFileSize = 16177215)    // upload file's size up to 16MB
public class FileUploadDBServlet extends HttpServlet {

// database connection settings
String dbURL="jdbc:oracle:thin:@localhost:1521: XE";
    String userId="system";
    String password="moon";


protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // gets values of text fields
    String id= request.getParameter("i1");
    String name= request.getParameter("i2");
    String brand= request.getParameter("i3");
    String price= request.getParameter("i4");
    String desc= request.getParameter("i5");
    String sid= request.getParameter("i6");
    String d= request.getParameter("i7");


    InputStream inputStream = null; // input stream of the upload file

    // obtains the upload file part in this multipart request
    Part filePart = request.getPart("i8");
    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();
    }

    Connection conn = null; // connection to the database
    String message = null;  // message will be sent back to client

    try {
        // connects to the database
        conn= DriverManager.getConnection(dbURL, userId, password);

        // constructs SQL statement
        String sql = "INSERT INTO item (itemid,name,brand,price,sid,img,dt,description) values (?, ?, ?,?,?,?,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,id);
        ps.setString(2,name);
        ps.setString(3,brand);
        ps.setString(4,price);
        ps.setString(5,desc);
        ps.setString(6,d);
        ps.setString(7,desc);



        if (inputStream != null) {
            // fetches input stream of the upload file for the blob column
            ps.setBlob(8, inputStream);
        }

        // sends the statement to the database server
        int row = ps.executeUpdate();
        if (row > 0) {
            message = "File uploaded and saved into database";
        }
    } catch (SQLException ex) {
        message = "ERROR: " + ex.getMessage();
        ex.printStackTrace();
    } finally {
        if (conn != null) {
            // closes the database connection
            try {
                conn.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        // sets the message in request scope
        request.setAttribute("Message", message);

        // forwards to the message page
               getServletContext().getRequestDispatcher("/Message.jsp").forward(request, response);
        }
    }
}

You should use setBinaryStream method instead setBlob method

OR You should use Blob type instead InputStream type in your parameters

Due to

1. public void setBinaryStream(int parameterIndex, InputStream stream, int length)

2. public void setBlob(int parameterIndex,Blob value)

Take into account length parameter if you select first approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM