【发布时间】:2018-12-03 07:02:29
【问题描述】:
我正在尝试从具有字段(IdFilm、Poster、Title、Country、Gender)的数据库的 Films 表中打印电影列表。除海报字段外,所有字段均正确打印。
我不知道应该将图像保存在数据库中的确切数据类型(图像或 varbinary(max))。我已将它们保存为带有图像名称的 varchar,例如 (photo1.jpg),我尝试以这种方式显示图像,但它不起作用:
类节目:
package entity;
import dao.dbmanager;
import java.sql.*;
import java.util.Vector;
public class show {
private final String URL = "images/";
private int id;
private String photo;
private String title;
private String country;
private String gender;
public show(int id, String photo, String title, String country, String gender) {
this.id = id;
this.photo = photo;
this.title = title;
this.country = country;
this.gender = gender;
}
public show() {
}
public static Vector ShowData() {
Vector film = null;
try {
dbmanager db = new dbmanager();
Connection con = db.Connect();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select * from Films");
film = new Vector();
while(rs.next())
film.add(new show(rs.getInt("idFilm"), rs.getString("poster"), rs.getString("title"), rs.getString("country"), rs.getString("gender")));
return film;
}
catch (SQLException e){
System.out.println("Error: " + e.getMessage());
}
return null;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photoName) {
this.photo = URL + photoName;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
index.jsp:
<%@page import="java.util.Vector"%>
<%@page import="entity.show"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form name="f1" method="POST">
<center>
<table border="1">
<tr align="center">
<td>Id</td>
<td>Poster</td>
<td>Title</td>
<td>Country</td>
<td>Gender</td>
</tr>
<%
show objfilm = new show();
Vector film = new Vector();
film = objfilm.ShowData();
for(int i=0; i < film.size(); i++) {
objfilm = (show)film.get(i);
%>
<tr align="center">
<td><%=objfilm.getId() %></td>
<td><img src="<%=objfilm.getPhoto() %>"></td>
<td><%=objfilm.getTitle() %></td>
<td><%=objfilm.getCountry() %></td>
<td><%=objfilm.getGender() %></td>
</tr>
<%
}
%>
</table>
</center>
</form>
</body>
</html>
我想知道如何显示每条记录的图像(海报)。
感谢您的帮助。
【问题讨论】:
-
如何存储图片?
-
喜欢base64还是保存到物理位置并将相关路径保存在db中?
-
如果您在 SQL Server 中存储二进制图像,您应该使用 varbinary(MAX) 作为 SQL 数据类型。 image 数据类型已被弃用多年。
标签: java sql-server jsp