Java JDBC ResultSet read clob

Description

Java JDBC ResultSet read clob


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

public class Main {

   public static void readClob(Connection conn) {
      PreparedStatement pstmt = null;
      String qry = null;// w w  w .  j av  a 2 s . co  m
      Clob theClob = null;
      ResultSet rs = null;
      try {
         qry = "select text from recipe_text";
         pstmt = conn.prepareStatement(qry);
         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());
         java.io.InputStream in = theClob.getAsciiStream();
         int i;
         while ((i = in.read()) > -1) {
            System.out.print((char) i);
         }

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

         ex.printStackTrace();
      } finally {
         if (pstmt != null) {
            try {
               pstmt.close();
            } catch (SQLException ex) {
               ex.printStackTrace();
            }
         }
         if (rs != null) {
            try {
               rs.close();
            } catch (SQLException ex) {
               ex.printStackTrace();
            }
         }
      }
   }
}



PreviousNext

Related