How to convert Clob and String types in Oracle

Source: Internet
Author: User
In fact, I have never used Clob because I do not have this requirement. But how can I find a solution as quickly as possible to grab points?

In fact, I have never used Clob because I do not have this requirement. But how can I find a solution as quickly as possible to grab points?

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.

Source: ()-Clob and String type conversion in Oracle _ laser header _ Sina Blog

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.

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;
}
Public static String oracleClob2Str (Clob clob) throws Exception {
Return (clob! = Null? Clob. getSubString (1, (int) clob. length (): null );
}
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;
}
Public static void main (String [] args) throws Exception {
// Create a data source
Connection conn = SqlUtil. getConnection ();
Clob 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 );
}

,

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.