Storage and display of images in databases Based on JSP

Source: Internet
Author: User

1. Introduction

Database applications, especially WEB-based database applications, usually involve the storage and display of image information. The common method is to store the image to be displayed in a specific directory, store the name of the image in the database, and create the corresponding data source in JSP, use Database Access technology to process image information. However, if we want to dynamically display images, the above method cannot meet our needs. We must store images in the database and dynamically display the images we need through programming. In practice, the JSP programming mode can be used to store and display image databases.

2. Create a background database

Suppose we are processing image news, then we can create corresponding databases and data table objects. The SQL script of the data table structure to be accessed is as follows:


If exists (select * from dbo. sysobjects where id =
Object_id (N [dbo]. [picturenews]) andOBJECTPROPERTY (id, NIsUserTable) = 1)
Drop table [dbo]. [picturenews]
GO
Create table [dbo]. [picturenews] (
[Id] [int] IDENTITY (1, 1) not null,
[Image] [image] NULL,
[Content] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL,
[Detail] [varchar] (5000) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
 

In the picturenews table, the field id is used as the identifier. each row of data is stored, which is automatically increased by 1. Field image

Used to store image information. The data type is "image ".

3. store binary images to the database

Start Dreamweaver MX and create a new JSP file. The Code is as follows.


<% @ Page contentType = "text/html; charset = gb2312" %>
<HTML>
<HEAD>
<TITLE> store images </TITLE>
</HEAD>
<Body>
<! -- The following form will pass the data to the testimage. jsp file using the Post method -->
<Form method = post action = "testimage. jsp">
New Question: <input type = "text" NAME = "content"> <BR>
New image: <input type = "file" NAME = "image"> <BR>
News content:
<TEXTAREA name = "txtmail" rows = "15" cols = "90"
Style = "BORDER-BOTTOM: #000000 1px solid; BORDER-LEFT: #000000 1px solid;
BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; FONT-SIZE: 9pt;
HEIGHT: 200px, WIDTH: 100% "wrap =" physical "> </TEXTAREA> <br>
<Input type = "submit"> </form>
</Body>
</HTML>
 

Save this file as an InputImage. jsp file. The testimage. jsp file is used to store image data to the database. The specific code is as follows:


<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "java. SQL. *" %>
<% @ Page import = "java. util. *" %>
<% @ Page import = "java. text. *" %>
<% @ Page import = "java. io. *" %>
<Html>
<Body>
<%
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
// Load the Driver Class
Connection con = DriverManager. getConnection ("jdbc: odbc: denglu", "sa", "sa ");
// Create a database online, where denglu is the database name and sa is the account and password used to connect to the database.
Statement stmt = con. createStatement ();
// Create a Statement object
String content = request. getParameter ("content ");
Content = new String (content. getBytes ("8859_1"), "gb2312 ");
String filename = request. getParameter ("image ");
Filename = new String (filename. getBytes ("8859_1"), "gb2312 ");
String detail = request. getParameter ("txtmail ");
Detail = new String (detail. getBytes ("8859_1"), "gb2312 ");
// Obtain the title, storage path, and content of the image to be displayed, and encode it in Chinese.
FileInputStream str = new FileInputStream (filename );
String SQL = "insert into picturenews (content, image, detail) values (?,?,?) ";
PreparedStatement pstmt = con. prepareStatement (SQL );
Pstmt. setString (1, content );
Pstmt. setBinaryStream (2, str, str. available ());
Pstmt. setString (3, detail );
Pstmt.exe cute ();
// Store data into the database
Out. println ("Success, You Have Insert an Image Successfully ");
%>
 


4. dynamically display images on the webpage

Next, we will program to retrieve images from the database. The Code is as follows.


<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "java. SQL. *" %>
<% @ Page import = "java. util. *" %>
<% @ Page import = "java. text. *" %>
<% @ Page import = "java. io. *" %>
<Html>
<Body>
<%
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
// Load the Driver Class
Connection con = DriverManager. getConnection ("jdbc: odbc: denglu", "sa", "sa ");
Statement stmt = con. createStatement ();
ResultSet rs = null;
// Create a ResultSet object
Int id = Integer. parseInt (request. getParameter ("id "));
// Obtain the id of the image to be displayed and convert it to an integer.
String SQL = "select image from picturenews WHERE id =" + id + "";
// SQL statement to be queried
Rs1_stmt.exe cuteQuery (SQL );
While (rs. next ()){
ServletOutputStream sout = response. getOutputStream ();
// Output stream of image output
InputStream in = rs. getBinaryStream (1 );
Byte B [] = new byte [0x7a120];
For (int I = in. read (B); I! =-1 ;)
{
Sout. write (B );
// Output the buffer input to the page
In. read (B );
}
Sout. flush ();
// After the input is complete, clear the buffer.
Sout. close ();
}
%>
</Body>
</Html>
 

Save this file as a testimageout. jsp file. The next step is to use HTML tags:


"width = 100 height = 100>
 

Retrieve the image to be displayed. The id is the id of the image to be retrieved. In this example, we output the first and last image information. The detailed program code is as follows.


<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "java. SQL. *" %>
<Html>
<Head>
<Title> dynamically display database images </title>
</Head>
<Body>
<%
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
Connection con = DriverManager. getConnection ("jdbc: odbc: denglu", "sa", "sa ");
Statement stmt = con. createStatement ();
String SQL = new String ();
SQL = "select * from picturenews ";
ResultSet rs1_stmt.exe cuteQuery (SQL );
Rs. last ();
// Move the pointer to the last record
%>
<Table>
<Tr> <td> </td>
// Retrieve the first image
<Td> "width = 136> </td>
// Retrieve the last image
</Tr> </table>
</Body>
</Html>
 

The preceding WEB applications are successfully debugged in the Windows 2000 Professional/SQL Server 2000/Apache Tomcat 4.0/JDK 1.4 JAVA environment.

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.