我试图通过(using this)在jsp页面中显示blob图像,但是我收到错误:
javax.servlet.ServletException: java.sql.SQLException: Column '2' not found.
我的表是:
create table upload_image
(
iImageID int AUTO_INCREMENT primary key,
bImage longblob
);
代码: uploadimage.jsp
<%@ page language="java" errorPage="" %>
<html>
<head>
<title>Image insert into database</title>
</head>
<body>
<form name="frm" action="saveImage.jsp" enctype="multipart/form-data" method="post">
<input type="file" name="uProperty" /> <br>
<input type="submit" name="goUpload" value="Upload" />
</form>
</body>
</html>
saveImage.jsp
<%@ page import="java.sql.*" %>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.io.output.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%
Connection conn=null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/online","root", "12345");
PreparedStatement psImageInsertDatabase=null;
byte[] b=null;
try{
String sqlImageInsertDatabase="insert into upload_image (bImage) values(?)";
psImageInsertDatabase=conn.prepareStatement(sqlImageInsertDatabase);
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(factory);
List items = sfu.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
b = item.get();
}
}
psImageInsertDatabase.setBytes(1,b);
psImageInsertDatabase.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
response.sendRedirect("addimage.jsp");
}
%>
get.jsp
<%@ page import ="java.sql.*" %>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/online","root", "12345");
Statement st = con.createStatement();
ResultSet i = st.executeQuery("select * from upload_image");
%>
<%
while(i.next( )){
%>
<img src="imageServlet?id=<%=i.getString("2")%>" />
<% }%>
执行 get.jsp 后(执行 uploadimage.jsp 之后)我得到"Column '2' not found."
请帮我在jsp页面中显示图像< / p>
答案 0 :(得分:1)
它抛出错误导致它找到一个名为2
的列,因为你已经将2放在双引号中。我觉得它应该是1而不是2(但你知道的更好)
这是下面的答案
<img src="imageServlet?id=<%=i.getString(1)%>" />//with no quotes as you can see
或
<img src="imageServlet?id=<%=i.getString("iImageID")%>" />