1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc)

JDBC Tutorials

Q.Handling Blob and CLOB data using JDBC ? Inser CLOB and Retrive CLOB. Convert into String. ?

BLOB (Binary Large Objects ) and CLOB(Character large objects) are special datatypes and can hold the large chunks of data in form of objects or text.
Blob and Clob objects persist the data of the objects into the database as a stream.

Example :

Java code to execute procedure

public class TestDB {
public static void main(String[] args) {
try {

/** Loading the driver*/

Class.forName("com.oracle.jdbc.Driver");

/** Getting Connection*/
Connection con = DriverManager.getConnection("jdbc:oracle://localhost:3306/test","test","test");

PreparedStatement pstmt = con.prepareStatement("insert into Emp(id,name,description)values(?,?,?)");
pstmt.setInt(1,5);
pstmt.setString(2,"Das");
// Create a big CLOB value...AND inserting as a CLOB
StringBuffer sb = new StringBuffer(400000);

sb.append("This is the Example of CLOB ..");
String clobValue = sb.toString();
pstmt.setString(3, clobValue);
int i= pstmt.executeUpdate();
System.out.println("Done Inserted");
pstmt.close();
     con.close();

     // Retrive CLOB values
Connection con = DriverManager.getConnection("jdbc:oracle://localhost:3306/test","test","test");
PreparedStatement pstmt = con.prepareStatement("select * from Emp where id=5");
ResultSet rs = pstmt.executeQuery();
Reader instream = null;
int chunkSize;
if(rs.next()){
String name = rs.getString("name");
java.sql.Clob clob = result.getClob("description")
StringBuffer sb1 = new StringBuffer();

chunkSize = ((oracle.sql.CLOB)clob).getChunkSize();
instream = clob.getCharacterStream();
BufferedReader in = new BufferedReader(instream);
String line = null;
while ((line = in.readLine()) != null) {
sb1.append(line);
    }
if(in != null){
in.close();
}

String clobdata = sb1.toString(); // this is the clob data converted into string

}


} catch (Exception e) {

e.printStackTrace();
}
     }
}

Reviews and Comments


PMP, CAPM, PMI is a registered certification mark of the Project Management Institute, Inc

Copyright © www.techfaq360.com 2016


About US | Contact US | Privacy Policy | Terms and Conditions  | Website disclaimer  | Cancellation and Refund Policy  | Shipping & Delivery Policy