我有Mysql数据库,并且正在使用servlet: 这是我的表架构:
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)作为主键,默认null会出错?
答案 0 :(得分:3)
您错过了逗号
VALUES (?,? ?,?)
应该是
VALUES (?,?,?,?)