gpt4 book ai didi

mysql - 如何在jsp中显示来自mysql的多个图像和数据

转载 作者:行者123 更新时间:2023-11-29 14:02:59 25 4
gpt4 key购买 nike

我在这些代码中使用了 jsp 和 mysql。我一次只得到一张图像,但检索了所有数据...你能告诉我如何在这段代码中显示多个图像吗?我的数据:imagename varchar()、描述varchar()、imageid varchar()、类别varchar image blob()。索引.jsp:

  <%@page import="java.io.InputStream"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.io.OutputStream"%>``
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.
getConnection("jdbc:mysql://localhost:3306/rich","root","");
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("select *from publishers");
while(rs.next())
{
String imgLen=rs.getString(5);

int len = imgLen.length();
byte [] rb = new byte[len];
InputStream readImg = rs.getBinaryStream(5);
int index=readImg.read(rb, 0, len);
System.out.println("index"+index);
stmt.close();
response.reset();
response.setContentType("image/jpg");
response.getOutputStream().write(rb,0,len);
response.getOutputStream().flush();
}


%>
</body>
</html>

**basic.jsp:**

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.io.OutputStream"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
< form action="index.jsp" method="post">
<%

Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/rich","root","");
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("select *from publishers");
while(rs.next())
{
%>
<table border="1">
<tr> <td>
<img src="index.jsp?" /> </td> </tr>
<tr>
<td> <%=rs.getString(1)%> </td>
<td> <%=rs.getString(2)%> </td>
<td> <%=rs.getString(3)%> </td>
<td> <%=rs.getString(4)%> </td>
</tr>
</table>
<%
}
%>
</form>
</body>
</html>

最佳答案

其实并不难。我们将使用两个 jsps 文件在本页首先显示图像以获取图像。

<%@ Page import="java.sql.*" %>
<%@ Page import="java.io.*" %>

<html>
<%
byte[] imgData = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/try","root","root");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select photo from employee where employee_id=" + request.getParameter("empId"));
while (rs.next())
{
Blob image = rs.getBlob(1);
imgData = image.getBytes(1,(int)image.length());
}
response.setContentType("image/png");
OutputStream o = response.getOutputStream();
o.flush();
o.close();
rs.close();
stmt.close();
con.close();
}
catch (Exception e)
{
out.println("Unable To Display image");
out.println("Image Display Error=" + e.getMessage());
return;
}
%>
</html>














































and here i getting all ids to get all images






<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<html>
<%
try
{
String EmpFirstName;
String EmpSurname;
String EmpId;
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/try","root","root");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select employee_id,first_name,surname from employee");
while (rs.next())
{
EmpFirstName = rs.getString("first_name");
EmpSurname = rs.getString("surname");
EmpId = rs.getString("EmpId");
<DIV><%=EmpFirstName5> <%=Surname%> </DIV>
<img src="http://localhost/GetImage.jsp?empId=<%=EmpId%>" />
}
rs.close();
stmt.close();
con.close();
}
catch (Exception e)
{
out.println(e.Message);
return;
}
%>
</html>


the code and its explanation taken from
http://stackoverflow.com/users/535152/tom%c3%a1s
thank you very much

关于mysql - 如何在jsp中显示来自mysql的多个图像和数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14786724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com