The following articles mainly describe how MySQL operates blob. If you are puzzled by the actual operations on blob in MySQL, you can use the following articles to understand the actual application and functions. The following is a detailed description of the article. I hope you will gain some benefits after browsing the following content.
Jsp (preferred for SUN Enterprise Applications) + MySQL (best combination with PHP) Remember to use the longblob type of MySQL (best combination with PHP) to store the default MySQL operation blob size.
Database field: id char) pic longblob)
Please indicate the source for reprinting. At this time, my cooperation with my confidant has been completed.
When operating blob fields, a null value must be removed first. It is troublesome to query blob. Using prepareStatment does not need to be so troublesome. Haha
Postblob. heml page
- <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <Html xml (standardization is getting closer and closer) ns = "http://www.w3.org/1999/xhtml">
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
- <Title> untitled document </title>
- </Head>
- <Body>
- <Form action = "testblob. jsp (preferred for SUN Enterprise Applications)" method = "post">
- <Table width = "291" border = "1">
- <Tr>
- <Td width = "107" type = "codeph" text = "codeph"> id </td type = "codeph" text = "/codeph">
- <Td width = "168"> <input name = "id" type = "text"/> </td>
- </Tr>
- <Tr>
- <Td> file </td>
- <Td> <input name = "file" type = "file"/> </td>
- </Tr>
- <Tr>
- <Td> <input type = "submit" value = "submit"/> </td>
- </Tr>
- </Table>
- </Form>
- </Body>
- </Html>
- Testblob. jsp (preferred for SUN Enterprise applications)
- <% @ Page contentType = "text/html; charset = gb2312" %>
- <% @ Page import = "java. SQL. *" %>
- <% @ Page import = "java. util. *" %>
- <% @ Page import = "java. text. *" %>
- <% @ Page import = "java. io. *" %>
- <Html xml (standardization is getting closer and closer) ns = "http://www.w3.org/1999/xhtml">
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
- <Title> untitled document </title>
- </Head>
- <Body>
- <%
- String id = request. getParameter ("id ");
- String file = request. getParameter ("file ");
- Out. print (id );
- Out. print (file );
- FileInputStream str = new FileInputStream (file );
- Out. print (str. available ());
- Java. SQL. Connection conn;
- Java. lang. String strConn;
- Class. forName ("org. gjt. mm. MySQL (the best combination with PHP). Driver"). newInstance ();
- Conn = java. SQL. DriverManager. getConnection ("jdbc: MySQL (the best combination with PHP): // localhost/test", "root ","");
- String SQL = "insert into test (id, pic) values (?,?) ";
- PreparedStatement pstmt = conn. prepareStatement (SQL );
- Pstmt. setString (1, id );
- Pstmt. setBinaryStream (2, str, str. available ());
- Pstmt.exe cute ();
- Out. println ("Success, You Have Insert an Image Successfully ");
- Pstmt. close ();
- %>
- <A href = "readblob. jsp (preferred for SUN Enterprise Applications)"> View images </a>
- <A href = "postblob.html"> return </a>
- </Body>
- </Html>
- Readblob. jsp (preferred for SUN Enterprise applications)
- <% @ Page contentType = "text/html; charset = gb2312" %>
- <% @ Page import = "java. SQL. *, javax. SQL. *" %>
- <% @ Page import = "java. util. *" %>
- <% @ Page import = "java. text. *" %>
- <% @ Page import = "java. io. *" %>
- <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <Html xml (standardization is getting closer and closer) ns = "http://www.w3.org/1999/xhtml">
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
- <Title> untitled document </title>
- </Head>
- <Body>
- <%
- Java. SQL. Connection conn;
- ResultSet rs = null;
- Class. forName ("org. gjt. mm. MySQL (the best combination with PHP). Driver"). newInstance ();
- Conn = java. SQL. DriverManager. getConnection ("jdbc: MySQL (the best combination with PHP): // localhost/test", "root ","");
- Statement stmt = conn. createStatement ();
- Rs1_stmt.exe cuteQuery ("select * from test where id = '1 '");
- If (rs. next ())
- {
- Blob B = rs. getBlob ("pic ");
- Int size = (int) B. length ();
- Out. print (size );
- InputStream in = B. getBinaryStream ();
- Byte [] by = new byte [size];
- Response. setContentType ("image/jpeg ");
- ServletOutputStream sos = response. getOutputStream ();
- Int bytesRead = 0;
- While (bytesRead = in. read ())! =-1 ){
- Sos. write (by, 0, bytesRead );
- }
- In. close ();
- Sos. flush ();
- }
- %>
- </Body>
- </Html>
Note: When sos. write (by, 0, bytesRead); is used, the content of inputstream is output in a new page,
If there is other content to output on this page, you only need to change the above method to bytesRead = in. read ());
Use the out. print (new String (by) method to output the result. Note that the by. toString () method cannot be used here. This method returns the memory address of the content to be output. MySQL (the best combination with PHP) has the MySQL operation blob textarea type which is 66536 in size. It is enough to put something small. Haha, however, as digital pic grows, longblob can only be used.