動態顯示資料庫圖片
態顯示資料庫圖片,本程式為小樣,程式碼過於簡單,只是實現其功能之作用。在程式碼方面沒有考慮。
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;
}
}
相關文章
- layui 輪播圖動態資料不顯示問題UI
- 【YashanDB資料庫】yasboot查詢資料庫狀態時顯示資料庫狀態為off資料庫boot
- jquery 滑鼠移到圖片彈出浮動層顯示大圖片例子jQuery
- 圖表外掛Highcharts的動態化賦值,實現圖表資料的動態化設定顯示賦值
- LaunchScreen.storyboard顯示啟動圖片未更新
- VS+Qt+Halcon——顯示圖片,實現滑鼠縮放、移動圖片QT
- 使用jpeg圖片庫,顯示圖片並簡單實現LCD的觸屏功能
- 資料庫下表和列名顯示資料庫
- CSS圖片的灰色顯示效果CSS
- 小程式button背景顯示圖片
- vue el-image 顯示圖片Vue
- 圖片上傳顯示替換
- 《ASP.NET1200例》高亮顯示ListView中的資料行並自動切換圖片ASP.NETView
- SAP ABAP 動態內表實現 ALV橫向按月份動態顯示資料
- 大頭針顯示多張圖片
- 如何在終端中顯示圖片
- cv2.imshow顯示圖片不全
- Vue 動態繫結圖片 src 屬性閃屏 白屏 無內容顯示 BUG 小記Vue
- 數碼管動態顯示
- 【資料】靜態/動態顯示LCD液晶驅動晶片VKS232,116SEG*2COM晶片
- 仿釘釘頭像(有頭像顯示圖片拼接,無圖顯示暱稱)
- Linux基礎命令---mysqlshow顯示資料庫LinuxMySql資料庫
- iOS 左滑按鈕(UITableViewRowAction)顯示圖片iOSUIView
- psd圖片不能在網頁上顯示網頁
- WPF 顯示 mp3 專輯圖片
- linux系統lcd顯示jpg格式圖片Linux
- QLabel顯示圖片 ,並實現縮放
- html2canvas生成圖片顯示不全HTMLCanvas
- vue迴圈顯示base64圖片Vue
- el-image圖片預覽顯示bug
- 用label顯示帶圖片的富文字
- 資料庫動態脫敏資料庫
- win10系統設定資料夾顯示圖片解析度的方法Win10
- java+pgsql實現儲存圖片到資料庫,以及讀取資料庫儲存的圖片JavaSQL資料庫
- Win10 Xbox商店不顯示圖片怎麼辦 Win10 Xbox商店不顯示圖片解決教程Win10
- 靜態數碼管顯示、動態數碼管顯示、矩陣按鍵矩陣
- word圖文混排複製到CuteEditor圖片不顯示
- word圖文混排複製到KindEditor圖片不顯示
- 網站顯示無法連線資料庫網站資料庫