Reading Blob data from Oracle-using JDBC

Source: Internet
Author: User

Read Blob data through JDBC to demonstrate code

  1. ImportJava. io. File;
  2. ImportJava. io. FileOutputStream;
  3. ImportJava. io. IOException;
  4. ImportJava. io. InputStream;
  5. ImportJava. io. OutputStream;
  6. ImportJava. SQL. Connection;
  7. ImportJava. SQL. DriverManager;
  8. ImportJava. SQL. ResultSet;
  9. ImportJava. SQL. Statement;
  10. Public ClassJDBC {
  11. // Public static void main (String [] args) throws Exception {
  12. //// Write BLOB Data
  13. //
  14. /// Obtain the database connection
  15. // Class. forName ("Oracle. jdbc. driver. OracleDriver ");
  16. //
  17. // Connection con = DriverManager. getConnection (
  18. //
  19. // "Jdbc: oracle: thin: @ 192.168.0.68: 1521: myOracle", "my", "123 ");
  20. //
  21. //// Process the transaction
  22. //
  23. // Con. setAutoCommit (false );
  24. //
  25. // Statement st = con. createStatement ();
  26. //
  27. /// Use the for update method to lock data rows
  28. //
  29. // ResultSet rs = st.exe cuteQuery (
  30. //
  31. // "Select content from user_content where id = 2 for update ");
  32. //
  33. // If (rs. next ()){
  34. //
  35. //// Obtain the java. SQL. Blob Object, and Cast is oracle. SQL. BLOB.
  36. //
  37. // Oracle. SQL. BLOB blob = (oracle. SQL. BLOB) rs. getBlob (1 );
  38. //
  39. //// Output stream to the database
  40. //
  41. // OutputStream outStream = blob. getBinaryOutputStream ();
  42. //
  43. //// Here, a file is used to simulate the input stream.
  44. //
  45. // File file = new File ("d: \ proxy.txt ");
  46. //
  47. // InputStream fin = new FileInputStream (file );
  48. //
  49. //// Write the input stream to the output stream
  50. //
  51. // Byte [] B = new byte [blob. getBufferSize ()];
  52. //
  53. // Int len = 0;
  54. //
  55. // While (len = fin. read (B ))! =-1 ){
  56. //
  57. // OutStream. write (B, 0, len );
  58. //
  59. //// Blob. putBytes (1, B );
  60. //
  61. //}
  62. //
  63. //// Close (pay attention to the Order)
  64. //
  65. // Fin. close ();
  66. //
  67. // OutStream. flush ();
  68. //
  69. // OutStream. close ();
  70. //
  71. // Con. commit ();
  72. //
  73. // Con. close ();
  74. //}
  75. //}
  76. Public Static VoidMain (String [] args)ThrowsException {
  77. // Read BLOB Data
  78. Class. forName ("Oracle. jdbc. driver. OracleDriver");
  79. Connection con = DriverManager. getConnection (
  80. "Jdbc: oracle: thin: @ 192.168.0.68: 1521: myOracle","My","123");
  81. Con. setAutoCommit (False);
  82. Statement st = con. createStatement ();
  83. // The SQL statement here does not need "for update"
  84. ResultSet rs = st.exe cuteQuery (
  85. "Select content from user_content where id = 2");
  86. If(Rs. next ()){
  87. Java. SQL. Blob blob = rs. getBlob (1);
  88. InputStream ins = blob. getBinaryStream ();
  89. // Simulate the output stream with a file
  90. File file =NewFile ("D: \ output.txt");
  91. OutputStream fout =NewFileOutputStream (file );
  92. // Write BLOB data to the file below
  93. Byte[] B =New Byte[1024];
  94. IntLen =0;
  95. While(Len = ins. read (B ))! =-1){
  96. Fout. write (B,0, Len );
  97. }
  98. // Close in sequence
  99. Fout. close ();
  100. Ins. close ();
  101. Con. commit ();
  102. Con. close ();
  103. }
  104. }
  105. }

Related Article

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.