我的servlet / jdbc中的sql查询错误?将blob插入数据库..?

时间:2015-08-13 11:18:13

标签: mysql servlets jdbc

我无法通过以下代码将用户的图像插入到blob字段中:

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.*;
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;

/**
 *
 * @author SUKIRTI
 */
@WebServlet(urlPatterns = {"/imageupload"})
@MultipartConfig(maxFileSize = 16177215) 
public class imageupload extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet imageupload</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet imageupload at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
//        processRequest(request, response);

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        InputStream inputStream = null;
        Part filePart = request.getPart("photo");
         if (filePart != null) {
         // obtains input stream of the upload file

            inputStream = filePart.getInputStream();
        }
        try{
         Class.forName("com.mysql.jdbc.Driver");
         Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/final_project?zeroDateTimeBehavior=convertToNull", "root", "madhumakhis");
         String sql = "insert into signup(dp) values(?) where signup.name =" + request.getSession(false).getAttribute("uname")+";";
         PreparedStatement statement = con.prepareStatement(sql);
         if (inputStream != null) {
                // fetches input stream of the upload file for the blob column
                statement.setBlob(1, inputStream);
            }
         int row = statement.executeUpdate();
            if (row > 0) {
               out.println("image uploaded");
            } 

        }catch(Exception e){out.println(e);}




    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

它显示以下错误:

  

&#34; com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便在&#39; where signup.name = mummy&#39;附近使用正确的语法。在第1行&#34;

其中mummy是检索到的属性(&#34; uname&#34;)?

我是jsp / servlets的新手,所以请解释我应该怎么做才能使它工作。

1 个答案:

答案 0 :(得分:0)

没有定义insert into ...values .. where语法

看起来您想要更新列,因此您必须使用:

Update signup set dp =? where signup.name = ?

有关更新语法的更多信息,请参阅官方mysql documantation