<%@ page contentType="text/html;charset=bg2312"%>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>顯示數據庫圖片測試頁</title>
</head>
<body>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:denglu","root","root");
Statement stmt=con.createStatement();
String sql=new String();
sql = "select id from picturenews";
ResultSet rs=stmt.executeQuery(sql);
//顯示最后一條記錄的圖片
rs.last();
%>
<table>
<tr><td><img src='testimageout.jsp?id=<%=rs.getInt("id")%>'></td></tr>
</table>
</body>
</html>
在建另一頁面讀取數據庫中圖片testimageout.jsp
<%@ page contentType="text/html; charset=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/todream";Connection con = DriverManager.getConnection(URL,"root","root");try{// 準備語句執行對象Statement stmt = con.createStatement();String sql = " SELECT * FROM todream_exhibition WHERE id = "+ photo_no;ResultSet rs = stmt.executeQuery(sql);if (rs.next()) {Blob b = rs.getBlob("workimg");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();}
%>
轉載http://wenwen.soso.com/z/q114766705.htm 如果你想嘗試你必須要將數據庫建好,并與這一樣或將jsp頁面改了跟你的一樣。