更新時間:2021-12-31 09:48:35 來源:動力節點 瀏覽1401次
數據庫應用,尤其是基于Web的應用,往往涉及圖片信息的存儲和顯示。一般我們采用將要顯示的圖片存放在特定目錄中,將對應圖片的名稱保存在數據庫中,在JSP中建立對應的數據源,利用數據庫訪問技術對圖片信息進行處理的方法。但是,如果我們想動態地顯示圖片,上述方法是不能滿足需要的。我們必須將圖片存儲在數據庫中,然后通過編程動態顯示我們需要的圖片。在實踐中,可以使用JSP編程模式在數據庫中存儲和顯示圖像。
假設我們處理圖片消息,那么我們就可以建立相應的數據庫和數據表對象。我們要訪問的數據表結構的SQL腳本如下:
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[picturenews]') andOBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[picturenews]
GO
CREATE TABLE [dbo].[picturenews] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[image] [image] NULL ,
[content] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL ,
[detail] [varchar] (5000) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
表格圖片中以字段ID作為標識,每存儲一行數據自動加1。Field image
用于存儲圖片信息,數據類型為“image”。
創建一個新的 JSP 文件。代碼如下。
<%@ page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
< title > store pictures < / Title >
</HEAD>
<body>
<! -- the form below will pass data to testimage.jsp file in post method -- >
<FORM METHOD=POST ACTION="testimage.jsp">
News Title: < input type = "text" name = "content" > < br >
News picture: < input type = "file" name = "image" > < br >
EA > < br >
<INPUT TYPE="submit"></form>
</body>
</HTML>
將此文件保存為inputimage.jsp文件,其中testimage.jsp文件用于將圖像數據存儲到數據庫中。具體代碼如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<html>
<body>
<% class. Forname ("com. Microsoft. JDBC. Sqlserver. Sqlserverdriver"); // load driver class
Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=upload_Image","sa","sa");
//Set up the database online, where upload image is the database name and SA is the account number and password to connect to the database.
Statement stmt=con.createStatement();
//Create statement object
String content=request.getParameter("content");
content=new String(content.getBytes("8859_1"),"gb2312");
String filename=request.getParameter("image");
filename=new String(filename.getBytes("8859_1"),"gb2312");
String detail=request.getParameter("txtmail");
detail=new String(detail.getBytes("8859_1"),"gb2312");
//Get the title, storage path and content of the picture to be displayed, and code it in Chinese
FileInputStream str=new FileInputStream(filename);
String sql="insert into picturenews(content,image,detail) values(?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,content);
pstmt.setBinaryStream(2,str,str.available());
pstmt.setString(3,detail);
pstmt.execute();
//Store data in database
out.println("Success,You Have Insert an Image Successfully");
%>
接下來,我們需要編程從數據庫中提取圖像,代碼如下。
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<html>
<body>
<%
% Class.forName (" com.microsoft.jdbc.sqlserver.SQLServerDriver "); // load driver class
Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=upload_Image","sa","sa");
//Set up the database online, where upload image is the database name and SA is the account number and password to connect to the database.
Statement stmt=con.createStatement();
ResultSet rs=null;
//Create a resultset object
int id= Integer.parseInt(request.getParameter("id"));
//Get the number ID of the picture to be displayed and convert it to integer
String sql = "select image from picturenews WHERE";
//SQL statement to execute query
rs=stmt.executeQuery(sql);
while(rs.next()) {
ServletOutputStream sout = response.getOutputStream();
//Output stream of picture output
InputStream in = rs.getBinaryStream(1);
byte b[] = new byte[0x7a120];
for(int i = in.read(b); i != -1;)
{
sout.write(b);
//Output buffer input to page
in.read(b);
}
sout.flush();
//Input complete, clear buffer
sout.close();
}
%>
</body>
</html>
將此文件另存為 testimageout.jsp 文件。下一步是使用 HTML 標簽:
<IMG src=”testimageout.jsp?id=<%=rs.getInt(“id”)%>” width=100 height=100>
取出要顯示的圖片,其中id為要顯示的圖片編號帶走。在這個例子中,我們輸出了第一張和最后一張圖片信息。詳細的程序代碼如下所示。
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<html>
<head>
< title > dynamically display database picture < / Title >
</head>
<body>
<%
% Class.forName (" com.microsoft.jdbc.sqlserver.SQLServerDriver "); // load driver class
Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=upload_Image","sa","sa");
//Set up the database online, where upload image is the database name and SA is the account number and password to connect to the database.
Statement stmt=con.createStatement();
String sql=new String();
sql= "select * from picturenews";
ResultSet rs=stmt.executeQuery(sql);
rs.last();
//Move pointer to last record
%>
<table>
<tr><td><IMG height=99 src="testimageout.jsp?id=1" width=136></td>
//Take out the first picture
<td><IMG height=99 src="testimageout.jsp?id=<%=rs.getInt("id")%>" width=136></td>
//Take out the last picture
</tr></table>
</body>
</html>
以上web應用在Windows XP / SQL Server 2000 / Apache Tomcat 4.0/jbuilder環境下調試通過。如果大家想了解更相關知識,可以關注一下動力節點的Java在線學習,里面的課程內容詳細,由淺到深,適合沒有基礎的小伙伴學習,希望對大家能夠有所幫助。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習