JSP implementation read the database display picture

Source: Internet
Author: User
Tags stmt

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.