如何映射来自不同来源的图像和文本

How to map image and text coming from different source?

本文关键字:图像 文本 何映射 映射      更新时间:2023-09-26

如何在HTML中映射图像和文本? 假设图像文件夹中有图像并且文本来自数据库?如何将它们放在网页中的一个位置?

假设 Milkbottel 在我的图像文件夹中,并且牛奶瓶文本来自数据库,那么如何**

动态映射

**这两个在某种程度上,牛奶瓶图像和牛奶瓶文本将在网页中的一个地方?

数据库内容示例:

id (INT)   | caption VARCHAR(255)     | location VARCHAR(255)
   1              milkbottle            /images/milkbottle.jpg
   2                orange              /images/orange.jpg

JSP 示例

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
<table border="2">
<%
try {
   Class.forName("com.mysql.jdbc.Driver");
   String url="jdbc:mysql://localhost/test";
   String username="root";
   String password="root";
   String query="SELECT * FROM image_table WHERE caption = 'milkbottle' LIMIT 1";
   Connection conn=DriverManager.getConnection(url,username,password);
   Statement stmt=conn.createStatement();
   ResultSet rs=stmt.executeQuery(query);
   if(rs != null) {
   %>
    <tr>
      <td><%=rs.getString("caption"); %></td>
      <td><img src='<%=rs.getString("location"); %>'/></td>
    </tr>
<%
}
%>
    </table>
    <%
    rs.close();
    stmt.close();
    conn.close();
}
catch(Exception e)
{
    e.printStackTrace();
}
%>
</body>
</form>
</html>

我假设您的问题围绕着在为网页加载图像时检索标题(存储在数据库中)。

在这种情况下,只需将图像的位置与标题一起存储在数据库表中。请看一下此示例表布局:

id (INT)   | caption VARCHAR(255)     | location VARCHAR(255)

因此,当您存储新图像和标题时,请存储保存图像的位置 location .然后,您可以通过以下查询检索给定图像位置的标题:

SELECT caption FROM image_table WHERE location = '/exact/location'

虽然我必须说,在WHERE子句中使用文本是不好的。如果可以的话,使用表的id列拉取位置和相应的图像要好得多(即更快)。