Experience in operating blob in MySQL

Source: Internet
Author: User

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

 
 
  1. <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <Html xml (standardization is getting closer and closer) ns = "http://www.w3.org/1999/xhtml">
  3. <Head>
  4. <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
  5. <Title> untitled document </title>
  6. </Head>
  7. <Body>
  8. <Form action = "testblob. jsp (preferred for SUN Enterprise Applications)" method = "post">
  9. <Table width = "291" border = "1">
  10. <Tr>
  11. <Td width = "107" type = "codeph" text = "codeph"> id </td type = "codeph" text = "/codeph">
  12. <Td width = "168"> <input name = "id" type = "text"/> </td>
  13. </Tr>
  14. <Tr>
  15. <Td> file </td>
  16. <Td> <input name = "file" type = "file"/> </td>
  17. </Tr>
  18. <Tr>
  19. <Td> <input type = "submit" value = "submit"/> </td>
  20. </Tr>
  21. </Table>
  22. </Form>
  23. </Body>
  24. </Html>
  25. Testblob. jsp (preferred for SUN Enterprise applications)
  26. <% @ Page contentType = "text/html; charset = gb2312" %>
  27. <% @ Page import = "java. SQL. *" %>
  28. <% @ Page import = "java. util. *" %>
  29. <% @ Page import = "java. text. *" %>
  30. <% @ Page import = "java. io. *" %>
  31. <Html xml (standardization is getting closer and closer) ns = "http://www.w3.org/1999/xhtml">
  32. <Head>
  33. <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
  34. <Title> untitled document </title>
  35. </Head>
  36. <Body>
  37. <%
  38. String id = request. getParameter ("id ");
  39. String file = request. getParameter ("file ");
  40. Out. print (id );
  41. Out. print (file );
  42. FileInputStream str = new FileInputStream (file );
  43. Out. print (str. available ());
  44. Java. SQL. Connection conn;
  45. Java. lang. String strConn;
  46. Class. forName ("org. gjt. mm. MySQL (the best combination with PHP). Driver"). newInstance ();
  47. Conn = java. SQL. DriverManager. getConnection ("jdbc: MySQL (the best combination with PHP): // localhost/test", "root ","");
  48. String SQL = "insert into test (id, pic) values (?,?) ";
  49. PreparedStatement pstmt = conn. prepareStatement (SQL );
  50. Pstmt. setString (1, id );
  51. Pstmt. setBinaryStream (2, str, str. available ());
  52. Pstmt.exe cute ();
  53. Out. println ("Success, You Have Insert an Image Successfully ");
  54. Pstmt. close ();
  55. %>
  56. <A href = "readblob. jsp (preferred for SUN Enterprise Applications)"> View images </a>
  57. <A href = "postblob.html"> return </a>
  58. </Body>
  59. </Html>
  60. Readblob. jsp (preferred for SUN Enterprise applications)
  61. <% @ Page contentType = "text/html; charset = gb2312" %>
  62. <% @ Page import = "java. SQL. *, javax. SQL. *" %>
  63. <% @ Page import = "java. util. *" %>
  64. <% @ Page import = "java. text. *" %>
  65. <% @ Page import = "java. io. *" %>
  66. <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  67. <Html xml (standardization is getting closer and closer) ns = "http://www.w3.org/1999/xhtml">
  68. <Head>
  69. <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
  70. <Title> untitled document </title>
  71. </Head>
  72. <Body>
  73. <%
  74. Java. SQL. Connection conn;
  75. ResultSet rs = null;
  76. Class. forName ("org. gjt. mm. MySQL (the best combination with PHP). Driver"). newInstance ();
  77. Conn = java. SQL. DriverManager. getConnection ("jdbc: MySQL (the best combination with PHP): // localhost/test", "root ","");
  78. Statement stmt = conn. createStatement ();
  79. Rs1_stmt.exe cuteQuery ("select * from test where id = '1 '");
  80. If (rs. next ())
  81. {
  82. Blob B = rs. getBlob ("pic ");
  83. Int size = (int) B. length ();
  84. Out. print (size );
  85. InputStream in = B. getBinaryStream ();
  86. Byte [] by = new byte [size];
  87. Response. setContentType ("image/jpeg ");
  88. ServletOutputStream sos = response. getOutputStream ();
  89. Int bytesRead = 0;
  90. While (bytesRead = in. read ())! =-1 ){
  91. Sos. write (by, 0, bytesRead );
  92. }
  93. In. close ();
  94. Sos. flush ();
  95. }
  96. %>
  97. </Body>
  98. </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.

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.