2012-05-11 40 views
0

匹配值計我有MySQL的datatbase,並在servlet的工作: 這是我的表架構:重度:值java.sql.SQLException:列數並不在行1

CREATE TABLE Files (
File_Name    VARCHAR(50), 
File_Data    Blob , 
File_Date   VARCHAR(20), 
File_Course_Code  VARCHAR(45) REFERENCES Course(Course_Code) , 
PRIMARY KEY (File_Name , File_Date, File_Course_Code) 
); 

這裏是servlet代碼:

protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    response.setContentType("text/html;charset=UTF-8"); 
    ServletOutputStream os = response.getOutputStream(); 
    try { 
     InputStream uploadedFile = null; 

     DiskFileUpload fu = new DiskFileUpload(); 
     // If file size exceeds, a FileUploadException will be thrown 
     fu.setSizeMax(10000000); 

     List fileItems = fu.parseRequest(request); 
     Iterator itr = fileItems.iterator(); 

     while (itr.hasNext()) { 
      FileItem fi = (FileItem) itr.next(); 

      //Check if not form field so as to only handle the file inputs 
      //else condition handles the submit button input 
      if (!fi.isFormField()) { // If the form fiel is a file 
       uploadedFile = fi.getInputStream(); 
      } 
     } 

      // to get the file name: 
      String fileName= "String"; 



      // to extract the date: 
      java.util.Date now = new java.util.Date(); 
      String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss"; 
      SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); 
      String strDateNew = sdf.format(now); 


      HttpSession session = request.getSession(); 
      String a = (String) session.getAttribute("fileccode"); 



      // set connection up: 
      Class.forName("com.mysql.jdbc.Driver"); 
      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/VC", "root", ""); 
      PreparedStatement stmt = null; 
      stmt = conn.prepareStatement("INSERT INTO Files (File_Name,File_Data,File_Date,File_Course_Code) VALUES (?,? ?,?)"); 
        stmt.setString(1,fileName); 
      stmt.setBinaryStream(2,uploadedFile); 
        stmt.setString(3,strDateNew); 
        stmt.setString(4,a); 
      stmt.executeUpdate(); 



    } catch (FileUploadException e) { 
     os.print(e.getLocalizedMessage()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     os.close(); 
    } 
} 

我看過很多關於這個錯誤的文章,但是幾乎所有的人在編寫查詢時都有語法錯誤。 我相信沒有語法錯誤。 但也許(File_Data)作爲主鍵和默認空造成錯誤?

回答

3

你缺少一個逗號

VALUES (?,? ?,?) 

應該

VALUES (?,?,?,?) 
+0

OMG謝謝你的sooo多.. 我無法相信這個逗號讓我哭了! – Lujain

相關問題