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.
- /**
- *
- * Description: Create a clob or blob.
- *
- * @ Param conn database connection object
- * @ Param lobclassname
- * Oracle. SQL. clob or Oracle. SQL. blob
- * @ Return oracle. SQL. clob or Oracle. SQL. BLOB Object
- * @ Throws exception
- * @ Blog. csdn. ne t/sunyujia/
- * @ Mail sunyujia@yahoo.cn
- * @ Since: Oct 1, 2008 6:42:08
- */
- Public static object createcanclelob (connection Conn, string lobclassname)
- Throws exception {
- Class lobclass = conn. getclass (). getclassloader (). loadclass (
- Lobclassname );
- Final integer duration_session = new INTEGER (lobclass. getfield (
- "Duration_session"). getint (null ));
- Final integer mode_readwrite = new INTEGER (lobclass. getfield (
- "Mode_readwrite"). getint (null ));
- Method createtemporary = lobclass. getmethod ("createtemporary ",
- New Class [] {connection. Class, Boolean. Class, Int. Class });
- Object lob = createtemporary. Invoke (null, new object [] {Conn, false,
- Duration_session });
- Method open = lobclass. getmethod ("open", new class [] {Int. Class });
- Open. Invoke (lob, new object [] {mode_readwrite });
- Return lob;
- }
- /**
- *
- * Description: converts a clob object to a string object. Blob is processed in the same way.
- *
- * @ Param clob
- * @ Return
- * @ Throws exception
- * @ Mail sunyujia@yahoo.cn
- * @ Blog. csdn. ne t/sunyujia/
- * @ Since: Oct 1, 2008 7:19:57
- */
- Public static string oracleclob2str (clob) throws exception {
- Return (clob! = NULL? Clob. getsubstring (1, (INT) clob. Length (): NULL );
- }
- /**
- *
- * Description: converts a String object to a clob object. Blob is processed in the same way.
- *
- * @ Param Str
- * @ Param lob
- * @ Return
- * @ Throws exception
- * @ Mail sunyujia@yahoo.cn
- * @ Blog. csdn. ne t/sunyujia/
- * @ Since: Oct 1, 2008 7:20:31
- */
- Public static clob oraclestr2clob (string STR, clob LOB) throws exception {
- Method methodtoinvoke = lob. getclass (). getmethod (
- "Getcharacteroutputstream", (class []) null );
- Writer writer = (writer) methodtoinvoke. Invoke (lob, (object []) null );
- Writer. Write (STR );
- Writer. Close ();
- Return lob;
- }
- /**
- *
- * Description: Check all source code.
- * Org. springframework. JDBC. Support. lob. Handler lelobhandler
- *
- * @ Param ARGs
- * @ Throws exception
- * @ Mail sunyujia@yahoo.cn
- * @ Blog. csdn. ne t/sunyujia/
- * @ Since: Oct 1, 2008 7:26:16
- */
- Public static void main (string [] ARGs) throws exception {
- // Create a data source
- Connection conn = sqlutil. getconnection ();
- Clob = (clob) create‑lelob (Conn, "oracle. SQL. clob"); // If blob is used, it is passed to Oracle. SQL. Blob.
- // Create table testtb (theclob clob );
- Preparedstatement pstmt = Conn
- . Preparestatement ("insert into testtb (theclob) values (?) ");
- Pstmt. setclob (1, oraclestr2clob ("test", clob ));
- Pstmt.exe cute ();
- Sqlutil. closestmt (pstmt );
- Statement stmt = conn. createstatement ();
- Resultset rs = stmt.exe cutequery ("select * From testtb ");
- While (Rs. Next ()){
- String STR = oracleclob2str (Rs. getclob (1 ));
- System. Out. println (STR );
- }
- Sqlutil. closers (RS );
- Sqlutil. closestmt (stmt );
- Sqlutil. closeconn (conn );
- }