動態顯示資料庫圖片
態顯示資料庫圖片,本程式為小樣,程式碼過於簡單,只是實現其功能之作用。在程式碼方面沒有考慮。
1. 建立資料庫表
if exists (select * from dbo.sysobjects
where id = object_id(N'[dbo].
') and OBJECTPROPERTY(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
2. 向資料庫中儲存圖片
a. InputImage.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>儲存圖片</TITLE>
</HEAD>
<body>
<!-- 下面的窗體將以Post方法,將資料傳遞給testimage.jsp檔案 -->
<FORM METHOD=POST ACTION=<%request.getContextPath();%>"image.do?method=inputImage">
新 聞 標 題:<INPUT TYPE="text" NAME="content"><BR>
新 聞 圖 片:<INPUT TYPE="file" NAME="image"><BR>
新聞內容:
<TEXTAREA name="txtmail" rows="15" cols="90"
style="BORDER-BOTTOM: #000000 1px solid; BORDER-LEFT: #000000 1px solid;
BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; FONT-SIZE: 9pt;
HEIGHT: 200px; WIDTH: 100%" wrap="physical" ></TEXTAREA><br>
<INPUT TYPE="submit"></form>
</body>
</HTML>
b. ImageAction(ImageAction為DispatchAction,本Action在SRC下)
public class ImageAction extends DispatchAction {
public ActionForward inputImage(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException, SQLException, IllegalAccessException, InstantiationException, ClassNotFoundException, FileNotFoundException {
//儲存
GetConnection getCon = new GetConnection();
Connection conn = getCon.getConnection();
Statement stmt = conn.createStatement();
//建立Statement物件
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");
//獲得所要顯示圖片的標題、儲存路徑、內容,並進行中文編碼
FileInputStream str = new FileInputStream(filename);
String sql = "insert into picturenews(content,image,detail) values(?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, content);
pstmt.setBinaryStream(2, str, str.available());
pstmt.setString(3, detail);
pstmt.execute();
pstmt.close();
conn.close();
System.out.println("~~~~~~~~~~~~~關");
return null;
}
public ActionForward showImage(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException, SQLException, IllegalAccessException, InstantiationException, ClassNotFoundException, FileNotFoundException {
// 顯示圖片
GetConnection getCon = new GetConnection();
Connection conn = getCon.getConnection();
Statement stmt = conn.createStatement();
String sql = new String();
sql = "select * from picturenews";
ResultSet rs = stmt.executeQuery(sql);
List list = new ArrayList();
while (rs.next()) {
ImageVO imagevo=new ImageVO();
int id=rs.getInt("id") ;
imagevo.setId(id);
imagevo.setTitle(rs.getString("content"));
imagevo.setNews(rs.getString("detail"));
list.add(imagevo);
}
stmt.close();
conn.close();
System.out.println("~~~~~~~~~~~~~關1");
request.setAttribute("list", list);
return mapping.findForward("show");
}
public ActionForward queryRs(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException, SQLException, IllegalAccessException, InstantiationException, ClassNotFoundException, FileNotFoundException {
// 查詢圖片
GetConnection getCon = new GetConnection();
Connection conn = getCon.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = null;
//建立ResultSet(結果集)物件
int id = (new Integer(request.getParameter("id"))).intValue();
//獲得所要顯示圖片的編號id,並轉換為整型
String sql = "select image from picturenews WHERE id=" + id + "";
System.out.println(sql);
//要執行查詢的SQL語句
rs = stmt.executeQuery(sql);
while (rs.next()) {
ServletOutputStream sout = response.getOutputStream();
//圖片輸出的輸出流
InputStream in = rs.getBinaryStream("image");
byte b[] = new byte[0x7a120];
while (in.read(b) != -1) {
//將緩衝區的輸入輸出到頁面
sout.write(b);
}
sout.flush();
//輸入完畢,清除緩衝
sout.close();
}
System.out.println("close");
stmt.close();
conn.close();
return null;
}
}
c.做一個連線頁面,link.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>顯示圖片</title>
</head>
<body>
<a href=<%request.getContextPath();%>"image.do?method=showImage">顯示圖片 </a>
</body>
</html>
d.圖片顯示頁面。showImage.jsp
<%@ page import="java.util.List,
java.util.ArrayList,
domain.ImageVO"%>
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>動態顯示資料庫圖片</title>
</head>
<body>
<%List list=(ArrayList) request.getAttribute("list");%>
<table border="1">
<%for(int i=0;i<list.size();i++){%>
<%ImageVO imagevo=(ImageVO) list.get(i);%>
<tr>
<td>取出第<%=imagevo.getId()%>個圖片</td>
<td><%=imagevo.getTitle()%></td>
<td><IMG height=99 src=<%request.getContextPath();%>"image.do?method=queryRs&id=<%=imagevo.getId()%>" width=136></td>
<td><%=imagevo.getNews()%></td>
</tr>
<%}%>
</table>
</body>
</html>
e.在SRC下建立domain夾子,在domain下建立ImageVO
public class ImageVO {
private int id=0;//id
private String title = "";//標題
private String news = "";//新聞內容
public ImageVO() {
id=0;
title = "";
news = "";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getNews() {
return news;
}
public void setNews(String news) {
this.news = news;
}
}
f. struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<action-mappings>
<action path="/image" type="ImageAction" parameter="method" scope="request">
<forward name="show" path="/showImage.jsp"/>
</action>
</action-mappings>
</struts-config>
g.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
</web-app>
h.在SRC下建立接接類
public class GetConnection {
private Connection conn = null;
public Connection getConnection() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb ";
String user = "sa";
String password = "";
conn = DriverManager.getConnection(url, user, password);
return conn;
}
}
相關文章
- 資料庫顯示圖片的問題資料庫
- 從資料庫中的表取幾張圖片,用flash形式動態的顯示圖片資料庫
- Java——圖片滾動顯示Java
- layui 輪播圖動態資料不顯示問題UI
- 數碼管顯示動態資料
- 【YashanDB資料庫】yasboot查詢資料庫狀態時顯示資料庫狀態為off資料庫boot
- img圖片無法顯示利用onerror事件顯示替代圖片Error事件
- 為什莫從資料庫中取出的圖片不能顯示出來資料庫
- jquery Banner 圖片自動輪換顯示jQuery
- 圖片設定level-list,根據不同狀態顯示不同圖片
- jquery 滑鼠移到圖片彈出浮動層顯示大圖片例子jQuery
- 使用Hibernate和Struts向資料庫中儲存、讀取並顯示圖片資料庫
- opencv圖片上如何顯示兩個小圖片OpenCV
- win7圖片只顯示圖示不顯示預覽圖解決方案Win7圖解
- 圖表外掛Highcharts的動態化賦值,實現圖表資料的動態化設定顯示賦值
- 網頁圖片不能顯示 網頁圖片顯示不出來的解決辦法網頁
- iOS設定tabbar不顯示文字,只顯示圖片iOStabBar
- VS+Qt+Halcon——顯示圖片,實現滑鼠縮放、移動圖片QT
- 使用jpeg圖片庫,顯示圖片並簡單實現LCD的觸屏功能
- 小程式button背景顯示圖片
- CSS圖片的灰色顯示效果CSS
- ImageView顯示網路上的圖片View
- vue el-image 顯示圖片Vue
- 自定義progressBar顯示靜態資料
- 顯示資料庫所有引數資料庫
- 【sga】資料庫啟動時的的SGA大小顯示資料庫
- Android 開源圖片裁剪工具、圖片顯示工具分享Android
- perf 的資料用火焰圖顯示
- iOS Swift 仿微信聊天圖片顯示iOSSwift
- cv2.imshow顯示圖片不全
- SAP ABAP 動態內表實現 ALV橫向按月份動態顯示資料
- ImageView顯示圖片資源的兩種方法(background/src)View
- jQuery 動態數字顯示jQuery
- alv動態顯示列
- Tree動態顯示Icon
- 數碼管動態顯示
- 修改資料庫的日期顯示格式資料庫
- 顯示資料庫中表的主鍵資料庫