这是后端代码,
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%! Connection con=null; %>
<%! Statement st= null; %>
<%
String lunchName = null;
String lunchDate = null;
InputStream lunchImage = null;
//out.print("lunch name:::::");
//out.print(request.getParameter("lunch_name"));
//out.print("lunch date:::::");
//out.print(request.getParameter("lunch_date"));
String id = request.getParameter("id");
//Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
//Configure a repository (to ensure a secure temp location is used)
ServletContext servletContext = this.getServletConfig().getServletContext();
File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
factory.setRepository(repository);
//Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
//Parse the request
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
if(name.equals("lunch_name"))
lunchName = value;
else if(name.equals("lunch_date"))
lunchDate = value;
} else {
lunchImage = item.getInputStream();
//String fieldName = item.getFieldName();
//String fileName = item.getName();
//String contentType = item.getContentType();
//boolean isInMemory = item.isInMemory();
//long sizeInBytes = item.getSize();
//out.print("fieldName : " + fieldName+"<br/>");
//out.print("fileName : " + fileName+"<br/>");
}
}
try{
String message = null;
Connection con=null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/first_app";
String user = "root";
String password = "asd462";
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url, user, password);
String sql = "insert into lunch_menu(lunch_image,lunch_name,lunch_date) values(?,?,?)";
PreparedStatement statement = con.prepareStatement(sql);
if (lunchImage != null) {
// fetches input stream of the upload file for the blob column
statement.setBlob(1, lunchImage);
}
statement.setString(2, lunchName);
statement.setString(3, lunchDate);
// sends the statement to the database server
int row = statement.executeUpdate();
if (row > 0) {
message = "File uploaded and saved into database";
}
}catch(SQLException ex){
ex.printStackTrace();
}finally {
if (con != null) {
// closes the database connection
try {
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
%>
我检查了前面的数据是否正常。
但如果我运行它,我会遇到这样的错误。
org.apache.jasper.JasperException: An exception occurred processing JSP page /jsp/writeMenu.jsp at line 73
70: PreparedStatement statement = con.prepareStatement(sql);
71: if (lunchImage != null) {
72: // fetches input stream of the upload file for the blob column
73: statement.setBlob(1, lunchImage);
74: }
75: statement.setString(2, lunchName);
76: statement.setString(3, lunchDate);
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:553)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:442)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
javax.servlet.ServletException: java.lang.AbstractMethodError: com.mysql.jdbc.PreparedStatement.setBlob(ILjava/io/InputStream;)V
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
org.apache.jsp.jsp.writeMenu_jsp._jspService(writeMenu_jsp.java:174)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
java.lang.AbstractMethodError: com.mysql.jdbc.PreparedStatement.setBlob(ILjava/io/InputStream;)V
org.apache.jsp.jsp.writeMenu_jsp._jspService(writeMenu_jsp.java:135)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.12 logs.
我按照这里的代码,
http://www.codejava.net/coding/upload-files-to-database-servlet-jsp-mysql
加,
http://commons.apache.org/proper/commons-fileupload/using.html
应该是什么问题?