Clob and string type conversion in Oracle

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/sunyujia/

I have never used clob because I did not have this problem on the Forum. But how can I find a solution as quickly as possible to compete for points, because spring encapsulates ORM tools, such as ibatis and hibernate, in spring. jar roughly flipped through the package. According to the package name and class name, the following suspicious class org was found. springframework. orm. ibatis. support. clobstringtypehandler tracks Org. springframework. JDBC. support. lob. the oraclelobhandler class contains everything I want. Hey hey, sorry, I copied it all.

The spring package contains org. springframework. JDBC. Support. lob. abstractlobhandler, which defines the basic clob and blog processing methods.

Org. springframework. JDBC. Support. lob. defaultlobhandler is the default implementation, except for other Oracle databases.

Org. springframework. JDBC. support. lob. oraclelobhandler is specially used for Oracle implementation. It can be seen that the bt of Oracle is implemented by reflection in order not to cause direct package dependency. if you are interested, you can read these two classes to learn about the particularity of oracle.

After analysis, the clob creation and conversion code with string are put out for your use. of course it is impossible to copy it completely. I have simplified it, but I have tested it and it is useless. for more details, refer to the org. springframework. JDBC. support. lob package source code.

The sqlutil class is written to facilitate testing. You can guess the content by code, so you will not post it all. Just paste the topic part of this article.

  1. /**
  2. *
  3. * Description: Create a clob or blob.
  4. *
  5. * @ Param conn database connection object
  6. * @ Param lobclassname
  7. * Oracle. SQL. clob or Oracle. SQL. blob
  8. * @ Return oracle. SQL. clob or Oracle. SQL. BLOB Object
  9. * @ Throws exception
  10. * @ Blog. csdn. ne t/sunyujia/
  11. * @ Mail sunyujia@yahoo.cn
  12. * @ Since: Oct 1, 2008 6:42:08
  13. */
  14. Public static object createcanclelob (connection Conn, string lobclassname)
  15. Throws exception {
  16. Class lobclass = conn. getclass (). getclassloader (). loadclass (
  17. Lobclassname );
  18. Final integer duration_session = new INTEGER (lobclass. getfield (
  19. "Duration_session"). getint (null ));
  20. Final integer mode_readwrite = new INTEGER (lobclass. getfield (
  21. "Mode_readwrite"). getint (null ));
  22. Method createtemporary = lobclass. getmethod ("createtemporary ",
  23. New Class [] {connection. Class, Boolean. Class, Int. Class });
  24. Object lob = createtemporary. Invoke (null, new object [] {Conn, false,
  25. Duration_session });
  26. Method open = lobclass. getmethod ("open", new class [] {Int. Class });
  27. Open. Invoke (lob, new object [] {mode_readwrite });
  28. Return lob;
  29. }
  30. /**
  31. *
  32. * Description: converts a clob object to a string object. Blob is processed in the same way.
  33. *
  34. * @ Param clob
  35. * @ Return
  36. * @ Throws exception
  37. * @ Mail sunyujia@yahoo.cn
  38. * @ Blog. csdn. ne t/sunyujia/
  39. * @ Since: Oct 1, 2008 7:19:57
  40. */
  41. Public static string oracleclob2str (clob) throws exception {
  42. Return (clob! = NULL? Clob. getsubstring (1, (INT) clob. Length (): NULL );
  43. }
  44. /**
  45. *
  46. * Description: converts a String object to a clob object. Blob is processed in the same way.
  47. *
  48. * @ Param Str
  49. * @ Param lob
  50. * @ Return
  51. * @ Throws exception
  52. * @ Mail sunyujia@yahoo.cn
  53. * @ Blog. csdn. ne t/sunyujia/
  54. * @ Since: Oct 1, 2008 7:20:31
  55. */
  56. Public static clob oraclestr2clob (string STR, clob LOB) throws exception {
  57. Method methodtoinvoke = lob. getclass (). getmethod (
  58. "Getcharacteroutputstream", (class []) null );
  59. Writer writer = (writer) methodtoinvoke. Invoke (lob, (object []) null );
  60. Writer. Write (STR );
  61. Writer. Close ();
  62. Return lob;
  63. }
  64. /**
  65. *
  66. * Description: Check all source code.
  67. * Org. springframework. JDBC. Support. lob. Handler lelobhandler
  68. *
  69. * @ Param ARGs
  70. * @ Throws exception
  71. * @ Mail sunyujia@yahoo.cn
  72. * @ Blog. csdn. ne t/sunyujia/
  73. * @ Since: Oct 1, 2008 7:26:16
  74. */
  75. Public static void main (string [] ARGs) throws exception {
  76. // Create a data source
  77. Connection conn = sqlutil. getconnection ();
  78. Clob = (clob) create‑lelob (Conn, "oracle. SQL. clob"); // If blob is used, it is passed to Oracle. SQL. Blob.
  79. // Create table testtb (theclob clob );
  80. Preparedstatement pstmt = Conn
  81. . Preparestatement ("insert into testtb (theclob) values (?) ");
  82. Pstmt. setclob (1, oraclestr2clob ("test", clob ));
  83. Pstmt.exe cute ();
  84. Sqlutil. closestmt (pstmt );
  85. Statement stmt = conn. createstatement ();
  86. Resultset rs = stmt.exe cutequery ("select * From testtb ");
  87. While (Rs. Next ()){
  88. String STR = oracleclob2str (Rs. getclob (1 ));
  89. System. Out. println (STR );
  90. }
  91. Sqlutil. closers (RS );
  92. Sqlutil. closestmt (stmt );
  93. Sqlutil. closeconn (conn );
  94. }

 

 

 

 

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.