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 establish the corresponding database as follows:
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" Language = "Java" Import = "Java. SQL. *" errorpage = "" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> untitled document </title>
</Head>
<Body>
<Form action = "testimage. jsp" method = "Post" name = "form1">
<P align = "center">
News Title
<Input name = "content" type = "text" id = "content">
</P>
<P> & nbsp; </P>
<P align = "center"> news images
<Input type = "file" name = "image">
</P>
<P align = "center"> news content
<Textarea name = "txtmail" Cols = "90" rows = "15" id = "txtmail"> </textarea>
</P>
<P align = "center">
<Input type = "Submit" name = "Submit" value = "Submit">
</P>
</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>
<% Try {class. forname ("org. gjt. Mm. MySQL. Driver ");
// Load the Driver Class
Connection con = drivermanager. getconnection ("JDBC: mysql: // localhost/test", "root ","");
// Create a database online. test indicates the database name, root indicates the account used to connect to the database, and the password is blank.
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 ");
}
Catch (exception ex) {out. println ("failure ");}
%>
</Body>
</Html>
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 ("org. gjt. Mm. MySQL. Driver ");
// Load the Driver Class
Connection con = drivermanager. getconnection ("JDBC: mysql: // localhost/test", "root ","");
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:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<Head>
<Title> dynamically display database images </title>
</Head>
<Body>
<%
Class. forname ("org. gjt. Mm. MySQL. Driver ");
// Load the Driver Class
Connection con = drivermanager. getconnection ("JDBC: mysql: // localhost/test", "root ","");
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>
This file is saved as getimage. jsp
The preceding web applications are successfully debugged in the Windows 2000 Professional/mysql4.0.18/Apache Tomcat 5.5/JDK 1.5 Java environment.