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 (matching with PHP)

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 (matching with PHP)

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. Untitled document
  2. Id
    File
  3. Testblob. jsp (preferred for SUN Enterprise applications)
  4. <% @ Page contentType = "text/html; charset = gb2312" %>
  5. <% @ Page import = "java. SQL. *" %>
  6. <% @ Page import = "java. util. *" %>
  7. <% @ Page import = "java. text. *" %>
  8. <% @ Page import = "java. io. *" %>
  9. Untitled document
  10. <%
  11. String id = request. getParameter ("id ");
  12. String file = request. getParameter ("file ");
  13. Out. print (id );
  14. Out. print (file );
  15. FileInputStream str = new FileInputStream (file );
  16. Out. print (str. available ());
  17. Java. SQL. Connection conn;
  18. Java. lang. String strConn;
  19. Class. forName ("org. gjt. mm. MySQL (the best combination with PHP). Driver"). newInstance ();
  20. Conn = java. SQL. DriverManager. getConnection ("jdbc: MySQL (the best combination with PHP): // localhost/test", "root ","");
  21. String SQL = "insert into test (id, pic) values (?,?) ";
  22. PreparedStatement pstmt = conn. prepareStatement (SQL );
  23. Pstmt. setString (1, id );
  24. Pstmt. setBinaryStream (2, str, str. available ());
  25. Pstmt.exe cute ();
  26. Out. println ("Success, You Have Insert an Image Successfully ");
  27. Pstmt. close ();
  28. %>
  29. View images
  30. Return
  31. Readblob. jsp (preferred for SUN Enterprise applications)
  32. <% @ Page contentType = "text/html; charset = gb2312" %>
  33. <% @ Page import = "java. SQL. *, javax. SQL. *" %>
  34. <% @ Page import = "java. util. *" %>
  35. <% @ Page import = "java. text. *" %>
  36. <% @ Page import = "java. io. *" %>
  37. Untitled document
  38. <%
  39. Java. SQL. Connection conn;
  40. ResultSet rs = null;
  41. Class. forName ("org. gjt. mm. MySQL (the best combination with PHP). Driver"). newInstance ();
  42. Conn = java. SQL. DriverManager. getConnection ("jdbc: MySQL (the best combination with PHP): // localhost/test", "root ","");
  43. Statement stmt = conn. createStatement ();
  44. Rs1_stmt.exe cuteQuery ("select * from test where id = '1 '");
  45. If (rs. next ())
  46. {
  47. Blob B = rs. getBlob ("pic ");
  48. Int size = (int) B. length ();
  49. Out. print (size );
  50. InputStream in = B. getBinaryStream ();
  51. Byte [] by = new byte [size];
  52. Response. setContentType ("image/jpeg ");
  53. ServletOutputStream sos = response. getOutputStream ();
  54. Int bytesRead = 0;
  55. While (bytesRead = in. read ())! =-1 ){
  56. Sos. write (by, 0, bytesRead );
  57. }
  58. In. close ();
  59. Sos. flush ();
  60. }
  61. %>

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.