Java JDBC PreparedStatement insert clob data

Description

Java JDBC PreparedStatement insert clob data


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

public class Main {
   public static void loadClob(Connection conn) {
      PreparedStatement pstmt = null;
      String sql = null;/*from   w ww  .  ja va  2 s  .c om*/
      Clob textClob = null;
      try {
          textClob = conn.createClob();
          textClob.setString(1, "clob test");
          sql = "INSERT INTO RECIPE_TEXT VALUES("
                  + "?, "
                  + "?)";
          pstmt = conn.prepareStatement(sql);

          pstmt.setInt(1, 1);
          // set the clob value
          pstmt.setClob(2, textClob);
          pstmt.executeUpdate();


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



PreviousNext

Related