2015-08-13 45 views
0

我無法通過下面的代碼以在用戶的​​圖像插入到blob字段:我的SQL查詢在servlet/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> 

} 

它表示以下錯誤:

"com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where signup.name =mummy' at line 1"

其中木乃伊是檢索到的屬性(「UNAME 「)?

我是jsp/servlets中的新手,所以很好解釋我應該怎麼做才能使它工作。

回答

0

沒有定義

語法insert into ...values .. where看起來像要更新一列,所以你必須使用:

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

有關更新語法看在MySQL官方欲瞭解更多信息documantation