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.
1. Create the MSSQLServer database test table pictruenews;
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
2. Add the MSSQLServer jar package
3. Four JSP
Add. jsp
<% @ 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 = "inputimage. 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>
Inputimage. jsp Save image
<% @ 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 ("com. Microsoft. JDBC. sqlserver. sqlserverdriver ");
Connection con = drivermanager. getconnection
("JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = test", "sa", "sa ");
Statement stmt = con. createstatement ();
String content = request. getparameter ("content ");
Content = new string (content. getbytes ("8859_1"), "gb2312 ");
String detail = request. getparameter ("txtmail ");
Detail = new string (detail. getbytes ("8859_1"), "gb2312 ");
String SQL = "insert into picturenews (content, image, detail) values (?,?,?) ";
Preparedstatement pstmt = con. preparestatement (SQL );
Pstmt. setstring (1, content );
String filename = new string (request. getparameter ("image"). getbytes ("8859_1"), "gb2312 ");
// D:/Photo/beauty/221.jpg
Fileinputstream STR = new fileinputstream (filename );
//Fileinputstream (string name)
// Create a fileinputstream by opening a connection to the actual file, which is specified by the path name in the file system.
Pstmt. setbinarystream (2, STR, str. Available ());
// Inputstream. Available () returns the estimated number of bytes that can be read (or skipped) from the input stream without blocking the method called for this input stream.
// Preparedstatement. setbinarystream (INT parameterindex, inputstream X, int length)
Pstmt. setstring (3, detail );
Pstmt.exe cute ();
// Store data into the database
Out. println ("success, you have insert an image successfully ");
%>
Testimageout. jsp fetch image by ID
<% @ 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 ("com. Microsoft. JDBC. sqlserver. sqlserverdriver ");
Connection con = drivermanager. getconnection
("JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = test", "sa", "sa ");
Statement stmt = con. createstatement ();
Resultset rs = NULL;
Int id = integer. parseint (request. getparameter ("ID "));
String SQL = "select image from picturenews where id =" + ID + "";
Rs1_stmt.exe cutequery (SQL );
While (Rs. Next ()){
Servletoutputstream sout = response. getoutputstream ();
Inputstream in = Rs. getbinarystream ("image ");
Byte B [] = new byte [0x7a120];
For (INT I = in. Read (B); I! =-1 ;)//?????????????? One more item
// Read () reads a certain number of bytes from the input stream and stores them in the buffer array B. If the stream is at the end of the file and there are no available bytes, the returned value is-1; the effect of the method is equivalent to: Read (B, 0, B. length)
{
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>
An example of displaying images in index. jsp
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<Head>
<Title> dynamically display database images </title>
</Head>
<Body>
<Table>
<Tr> <TD> </TD>
// Retrieve the first image
</Tr> </table>
</Body>
</Html>