Querying and Storing Large Objects using CLOB - Java JDBC

Java examples for JDBC:Binary Data

Description

Querying and Storing Large Objects using CLOB

Demo Code

import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {
  static Connection conn = null;
  public static void loadClob() {
    Clob textClob = null;/*  w ww.j  a  v a2s . com*/
    String sql = "INSERT INTO Emp VALUES (?)";
    try (PreparedStatement pstmt = conn.prepareStatement(sql);) {
      textClob = conn.createClob();
      textClob.setString(1, "This will be the recipe text in clob format");
      pstmt.setClob(1, textClob);
      pstmt.executeUpdate();

    } catch (SQLException ex) {
      ex.printStackTrace();
    }
  }

  public static void readClob() {
    String qry = "select text from Emp";
    Clob theClob = null;
    try (PreparedStatement pstmt = conn.prepareStatement(qry);
        ResultSet rs = pstmt.executeQuery();) {

      while (rs.next()) {
        theClob = rs.getClob(1);
        System.out.println("Clob length: " + theClob.length());
        System.out.println(theClob.toString());
      }
      System.out.println(theClob.toString());

    } catch (SQLException ex) {

      ex.printStackTrace();
    }
  }

}

Related Tutorials