如何使用JSP将映像插入SQL Server数据库

时间:2018-08-15 01:38:11

标签: sql-server image jsp

我需要将一个imagen插入SQL Server数据库中,我需要使用jsp来完成它,但是我不知道该怎么做。有人可以教我最好的方法吗?

2 个答案:

答案 0 :(得分:0)

我不建议单独使用jsp来完成此任务,因为它打算用作视图技术。

对于任何文件上传功能,请使用Servlet 3.0规范中的@MultiPartConfig

在您的jsp文件中:

<form method="post" enctype="multipart/form-data" action="/your-servlet-path">
    <input type="file" name="myimage" />
</form>

在您的servlet中:

@MultiPartConfig
@WebServlet("/your-servlet-path")
public class FileUploadServlet extends HttpServlet{

    @Override
    public void doPost(HttpServletRequest req,HttpServletResponse res) throws Exception{
        Part imagePart = req.getPart("myimage");
        InputStream partInputStream = imagePart.getInputStream();
        //read byte array from input stream


         //insert into database (assuming that your image field is of blob type, it will accept inputStream type in JDBC API)


         //and finally of course redirect to your upload result page
    }
}

上面的代码片段只是让您了解如何在servlet中上传文件。它不涉及任何文件类型验证或异常处理。

答案 1 :(得分:0)

这是使用MVC模式的方法。.在此示例中,我使用了mysql数据库,可以使用自己喜欢的DBMS。该代码可以解决您的问题。
HTML代码

在HTML或JSP页面的表单标签中

add-- enctype =“ multipart / form-data”

模型类代码-封装所有字段

private int studentRoll;
private String studentName;
private String studentClass;
private String studentAddress;
private Object studentImage;
private byte image_data[];

servlet代码

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
         if (request.getParameter("submit") != null) {

            String student_Name = request.getParameter("student_name");
            String student_Class = request.getParameter("student_class");
            String student_Address = request.getParameter("student_address");

            Part student_Image = request.getPart("student_image");

            StudentModel student = new StudentModel();
            student.setStudentName(student_Name);
            student.setStudentClass(student_Class);
            student.setStudentAddress(student_Address);
            student.setStudentImage(student_Image);

            StudentDAO stu_dao = new StudentDAO();
            stu_dao.insertStudent(student);
        }
    }catch(Exception ex){
        ex.printStackTrace();;
    }
}

连接类-DatabaseConnection.java

public static Connection getConnection() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/newstudentdatabase", "root", "");
        return connect;
    } catch (ClassNotFoundException | SQLException ex) {
        ex.printStackTrace();
    }
    return null;
}

}

dao类-插入方法

public void insertStudent(StudentModel student) {
    try (Connection connect = DatabaseConnection.getConnection()) {
        String query = "insert into studentrecord(student_name, student_class, student_address, student_image) values(?,?,?,?)";
        PreparedStatement message = connect.prepareStatement(query);
        message.setString(1, student.getStudentName());
        message.setString(2, student.getStudentClass());
        message.setString(3, student.getStudentAddress());
        message.setBlob(4, ((Part) student.getStudentImage()).getInputStream());
        message.execute();
        connect.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}