环境mysql+tomcat:
<1>先在mysql下建立如下的table. 并insert图像
mysql.sql文件如下:
CREATE TABLE photo (
photo_no int(6) unsigned NOT NULL auto_increment,
image blob,
PRIMARY KEY (`photo_no`)
)
<2>把show.jsp放在tomcat的任意目录下. show.jsp作用:从数据库中读出blob,并产生image/jpg.
show.jsp文件如下:
<%@ page contentType="text/htmlcharset=gbk" %>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*, javax.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.math.*"%>
<%
String photo_no = request.getParameter("photo_no")
//mysql连接
Class.forName("com.mysql.jdbc.Driver").newInstance()
String URL="jdbc:mysql://localhost:3306/job?user=root&password=111111"
Connection con = DriverManager.getConnection(URL)
//oracle连接
//String URL="jdbc:oracle:thin@localhost:1521:orcl2"
//user="system"
//password="manager"
//Connection con = DriverManager.getConnection(URL,user,password)
try{
// 准备语句执行对象
Statement stmt = con.createStatement()
String sql = " SELECT * FROM PHOTO WHERE photo_no = "+ photo_no
ResultSet rs = stmt.executeQuery(sql)
if (rs.next()) {
Blob b = rs.getBlob("photo_image")
long size = b.length()
//out.print(size)
byte[] bs = b.getBytes(1, (int)size)
response.setContentType("image/jpeg")
OutputStream outs = response.getOutputStream()
outs.write(bs)
outs.flush()
rs.close()
}
else {
rs.close()
response.sendRedirect("./images/error.gif")
}
}
finally{
con.close()
}
%>
<3>把如下文件放在show.jsp的同一目录下.
index.html文件如下:
<HTML>
<HEAD>
<TITLE>图像测试 </TITLE>
</HEAD>
<BODY>
<TABLE>
<TR>
<TD>图像测试</TD>
</TR>
<TR>
<TD><img src="show.jsp?photo_no=2"></TD>
</TR>
</TABLE>
</BODY>
</HTML>
1.错误主要原因在HTML中,所有元素打开后都必须关闭,打开一个<tr>就必须用</tr>关闭,或者自己开自己关的元素,如<br/><img src="0.jpg" />。
2.解决办法
你的代码中,所有的<tr>都没有关闭。所以要在多处用</tr>关闭。以下是改后的正确代码:
<%@ page contentType="text/htmlcharset=gb2312" import="java.sql.*"%>
<jsp:useBean id="db" class="dbBean.DBBean" scope="session"/>
<center>
<h2>系统维护</h2>
<table border bordercolor='#0099ff' width="50%">
<tr>
<th colspan="3" bgcolor='#ccffff'>删除投票项</th>
</tr>
<%
ResultSet rs=db.executeQuery("select * from vote")
int i=1
while(rs.next()){
out.println("<tr>")
out.println("<td>"+i+"</td>")//这行改前好少了个">"大于号
out.println("<td>"+rs.getString("item")+"</td>")
/*显示"删除"超连接,跳转到删除处理页面delete时传递参数delid,即要删除的投票项id号*/
out.println("<td align='center'><a href='delete.jsp?delid="+rs.getString("id")+"'>删除</a></td>")
out.println("</tr>")//这行就是关闭tr
i++
}
rs.close()
db.close()
%>
<tr><th colspan="3" bgcolor='#ccffff'>添加投票项</th></tr>
<tr>
<td colspan="3">
<%--利用表单输入添加信息,提交给添加投票项处理页面add.jsp--%>
<form method="post" action="add.jsp">
内容:<input type="text" name="additem" size="30">
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</td>
</tr>
<tr>
<td colspan="3" align="center" bgcolor='#ccffff'><a href="index.jsp">返回投票页面</a></td>
</tr>
</table>
</center>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)