Use JSP to read pictures from the database and display the environment Mysql+tomcat on the Web page:
<1> first set up the following table under MySQL. and insert the image. The Mysql.sql file is as follows:
CREATE TABLE int (6notNULLimagePRIMARYKEY (' photo_no '))
<2> put show.jsp in any directory of Tomcat. show.jsp function: Reads the blob from the database and generates a image/jpg. The show.jsp file is as follows:
<%@ Page ContentType="text/html; CHARSET=GBK" %> <%@ Page Import="java.io.*"%> <%@ Page Import="java.sql.*, javax.sql.*" %> <%@ Page Import="java.util.*"%> <%@ Page Import="java.math.*"%> <% StringPhoto_no=Request.getparameter ("Photo_no"); //MySQL connection class.forname ("Com.mysql.jdbc.Driver"). newinstance (); StringURL="jdbc:mysql://localhost:3306/job?user=root&password=111111"; Connection Con=drivermanager.getconnection (URL);//Oracle Connectivity//StringURL="Jdbc:oracle:[email Protected]:1521:orcl2"; //User="system"; //Password="Manager"; //Connection con=drivermanager.getconnection (Url,user,password); try{//prepare the statement execution Object Statement stmt=con.createstatement ();StringSQL= "SELECT * from PHOTO WHERE photo_no ="+Photo_no; ResultSet RS=stmt.executequery (SQL);if(Rs.next ()) {Blob B=Rs.getblob ("Photo_image"); Longsize=b.length ();//out.print (size);byte[] BS=B.getbytes (1, (int) size); Response.setcontenttype ("Image/jpeg"); OutputStream Outs=Response.getoutputstream (); Outs.write (BS); Outs.flush (); Rs.close ();} Else{rs.close (); Response.sendredirect ("./images/error.gif"); }} finally{Con.close ();}%>
<3> Place the following files in the same directory as the show.jsp.
The index.html file is as follows:
<HTML> <HEAD> <TITLE>Image testing</TITLE> </HEAD> <BODY> <TABLE> <TR> <TD>Image testing</TD> </TR> <TR> <TD><imgsrc= "show.jsp?photo_no=2"></TD> </TR> </TABLE> </BODY> </HTML>
JSP implementation read the database display picture