A simple blob access example

Source: Internet
Author: User

 

Code:
  1. Import java. Io .*;
  2. Import java. SQL .*;
  3. /**
  4. * Test the BLOB data operation.
  5. */
  6. Public class blobtest {
  7. Connection con = NULL;
  8. Private Static string filepath = "D:/test/smile.jpg ";
  9. Private Static string fileoutpath = "D:/test/smilecopy.jpg ";
  10. /**
  11. * Add BLOB data to the database
  12. */
  13. Public void addblob (){
  14. Try {
  15. // Create a preparedstatement instance
  16. Preparedstatement pstmt = con
  17. . Preparestatement ("insert into blobtest values (1 ,?) ");
  18. File file = new file (filepath );
  19. Fileinputstream FCM = new fileinputstream (File );
  20. // Set the input stream as the object of the preprocessing statement.
  21. Pstmt. setbinarystream (1, FS, (INT) file. Length ());
  22. // Execute update
  23. Pstmt.exe cuteupdate ();
  24. Pstmt. Close ();
  25. FCM. Close ();
  26. } Catch (Exception e ){
  27. E. printStackTrace ();
  28. }
  29. }
  30. /**
  31. * Create a table
  32. */
  33. Public void createTable (){
  34. Try {
  35. Con.createstatement(cmd.exe cute (
  36. "Create table blobtest (ID int, PIC blob ,"
  37. + "Constraint pk_blobtest primary key (ID ));");
  38. } Catch (exception e ){
  39. E. printstacktrace ();
  40. }
  41. }
  42. /**
  43. * Reading BLOB data from the database
  44. */
  45. Public void getblob (){
  46. Try {
  47. // Create a statement instance
  48. Statement stmt = con. createstatement ();
  49. Resultset rst = stmt
  50. . Executequery ("select * From blobtest where id = 1 ");
  51. RST. Next ();
  52. // Obtain BLOB data and its input stream, and then write the data to the file through the input stream.
  53. Blob blob = RST. getblob ("pic ");
  54. Fileoutputstream out = new fileoutputstream (new file (
  55. Fileoutpath ));
  56. Inputstream in = blob. getbinarystream ();
  57. Int I;
  58. While (I = in. Read ())! =-1)
  59. Out. Write (I );
  60. // Close the input and output streams.
  61. In. Close ();
  62. Out. Close ();
  63. } Catch (exception e ){
  64. E. printstacktrace ();
  65. }
  66. }
  67. Public static void main (string [] ARGs) throws exception {
  68. Class. forname ("com. MySQL. JDBC. Driver"). newinstance ();
  69. Blobtest test = new blobtest ();
  70. Test. Con = java. SQL. drivermanager. getconnection (
  71. "JDBC: mysql: // localhost: 3306/test", "root", "123456 ");
  72. Test. createtable ();
  73. Test. addblob ();
  74. Test. getblob ();
  75. }
  76. }

MySQL has four blob types:

· Tinyblob: Only 255 characters long

· BLOB: up to KB

· Mediumblob: up to 16 MB bytes

· Longblob: up to 4 GB

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.