JDBC to read and write CLOB

Creating a Test Table

create table CLOB_TEST (ID number (10), CLOB_TEXT CLOB)


import java.sql.*;
import java.io.OutputStream;
import java.io.Writer;
import java.io.Reader;

// create table CLOB_TEST(ID number(10), CLOB_TEXT CLOB)

public class ClogTest {

public static void main(String[] args) throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");

Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:zhongyj", "scott", "tiger");

// Each time it is executed, it is necessary to modify the second parameter id

insertClob(conn, 4);
readClob(conn, 4);

conn.close();
}

static void insertClob(Connection conn, int id) throws Exception {
conn.setAutoCommit(false);
PreparedStatement ps = conn.prepareStatement("insert into CLOB_TEST(ID, CLOB_TEXT) values(?,empty_clob())");
ps.setInt(1, id);
ps.executeUpdate();

ps= conn.prepareStatement("select CLOB_TEXT from CLOB_TEST where");
ps.setInt(1, id);

ResultSet rs = ps.executeQuery();
Clob clob = null;

if(rs.next()) {
clob = rs.getClob(1);
}

// This section of the deposit to the CLOB_TEXT field CLOB_TEXT_CONTENT

String str = "Write in String: Hello Jdbc CLOB";
clob.setString(10, str); // The first is from the first few characters began to write


// // This section of the deposit to the CLOB_TEXT field CLOB_TEXT_CONTENT

// Writer wr = clob.setCharacterStream(10); // Parameter is from the first few characters began to write

// wr.write("Write in Writer");

// wr.flush();

// wr.close();


rs.close();
conn.commit();
ps.close();
}

static void readClob(Connection conn, int id) throws Exception{
PreparedStatement ps = conn.prepareStatement("select CLOB_TEXT from CLOB_TEST where");
ps.setInt(1, id);

ResultSet rs = ps.executeQuery();

if(rs.next()) {
Clob clob = rs.getClob("CLOB_TEXT");
Reader rd = clob.getCharacterStream();
char [] str = new char[12];
while(rd.read(str) != -1) {
System.out.print(str);
}
}
}
}


  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of JDBC to read and write CLOB

  • ORACLE10G full version centos5 installed (the installation has passed)

    ORACLE10G full version centos5 installed (the installation has passed) 1. Centos 5.0 install rn GUI must be installed, it is best not to start selinux rn rn rn 2. . Ready to install the software: (this is very important, is the first installation fai ...

  • SGA extended the principle of 32bit oracle

    SGA extended the principle of 32bit oracle From: http://www.itpub.net/247048.html Because the median 32bitrnoracle restrictions can only visit the oracle process 4g (2 of 32 power) following virtual memory address, the time at a lot of people this is ...

  • SQL optimization (3) - four index

    Test the following four types of index: Unique Index Scan (INDEX UNIQUE SCAN) Index range scan (INDEX RANGE SCAN) Scan the whole index (INDEX FULL SCAN) Express-wide scan index (INDEX FAST FULL SCAN) 1. To create the test table and the only composite ...

  • Dbms_obfuscation_toolkit use of Oracle's encryption and decryption of data (to)

    In order to protect sensitive data, oracle start from 8i to provide a data encryption package: dbms_obfuscation_toolkit. Take advantage of this package, our data can be DES, Triple DES or MD5 encryption. This article on the use of this and the use of ...

  • Oracle XDB relax resolve port conflict of 8080

    In this paper, carried: http://www.enet.com.cn/article/2008/0306/A20080306175452.shtml Oracle 9i from the start, Oracle includes the installation of the default XDB. After starting the database, Oracle XDB's http service will automatically take u ...

  • Oracle in the relationship between User and Schema

    If we want to know the database and the User What is the relationship between Schema, we must first know about User and Schema database What is the concept in the end. In SQL Server2000 in architecture because of the reason, User and Schema there is ...

  • High Availability Oracle Flashback

    Brief introduction Flashback Database is a point in time (PIT) restore the database approach. This incomplete recovery strategy can be used to restore the logic because of human error cause damage to the database. At the introduction of 10g, it is de ...

  • An example of the use of TKPROF

    First, view and edit parameters SQL> show parameter max_dump_file_size NAME TYPE VALUE ------------------------------------ ----------- --- --------------------------- max_dump_file_size string UNLIMITED SQL> show parameter user_dump_dest NAME TYPE

  • Diagnosis and principles of order

    SQL> select disk.value "Disk", mem.value "Mem", 2 (disk.value / mem.value) * 100 "Ratio" 3 from v $ sysstat mem, v $ sysstat disk 4 where mem.name = 'sorts (memory)' 5 and disk.name = 'sorts (disk)'; D ...

  • ORACLE 10G dataguard configuration Step by Step

    oracle dataguard

Leave a Reply

Recent
Recent Entries
Tag Cloud
Random Entries
Latest Comments