Saturday, 21 January 2017

CLOB

CLOB stands for Character Large Object. If database table created with column type as CLOB it can store more than 4000 character. PreparedStatement provide the following method -
void setCharacterStream(int paramindex, Reader reader, int length) sets the designated Parameter to a given Reader object, which is the given number of character long.


PROGRAM TO INSERT INTO CLOB COLUMN

SQL> create table member_register(name varchar2(25),data clob);

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class Test20 {
public static void main(String[] args) throws SQLException, ClassNotFoundException, FileNotFoundException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "rakesh");
PreparedStatement ps = con.prepareStatement("insert into member_register values(?,?)");
Scanner scan = new Scanner(System.in);
System.out.println("Input name");
String name=scan.next();
System.out.println("Input data file name");
String f = scan.next();
File file = new File(f);
FileReader fr = new FileReader(file);
ps.setString(1, name);
ps.setCharacterStream(2, fr,(int)file.length());
int count=ps.executeUpdate();
System.out.println(count);
}

}

Output -
Input name
rakesh
Input data file name
Document.text

1


TO READ DATA FROM CLOB COLUMN USE THE BELOW MENTIONED METHOD BY WATCHING MY BLOG ON BLOB

Reader getCharacterStream(int columnindex) - Retrieves the value of the designated column in the current row of this ResultSet object as a java.io.Reader object.


No comments:

Post a Comment