This article mainly tells you about the practical experience of using MySQL to operate blob. We all know that jsp (the preferred choice for SUN Enterprise Applications) + MySQL (the best combination with PHP). Remember, you must use the longblob type of MySQL (the best combination with PHP) to save the default 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.
It turns out that when MySQL operates blob fields, a null value must be removed first. It is very 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 blob textarea type, which is 66536 in size. It is enough to put something small. Haha, but now the digital pic is getting bigger and bigger, so we can only use longblob. The size of 4 GB is enough for a movie. Haha
The above content is an introduction to the experience in operating blob in MySQL. I hope you will gain some benefits.