16.20 - Java Method Implementation - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL External Routine Programming

Product
Advanced SQL Engine
Teradata Database
Release Number
16.20
Release Date
April 2020
Content Type
Programming Reference
Publication ID
B035-1147-162K
Language
English (United States)

Here are the contents of a file called Web.java that you can compile to generate the file Web.class that you can place into a JAR file called Web.jar.

import com.teradata.fnc.*;
import java.net.*;
import java.io.*;
import java.sql.*;

public class Web {
   public static void getWebData(int ID, String url) throws SQLException
   {
      final int ClobSize = 1024*1000;
      final char[] buffer = new char[ ClobSize ];

      try {
         /* Establish default connection. */
         Connection con =
            DriverManager.getConnection( "jdbc:default:connection" );

         /* Prepare the command */
         String sql = "INSERT INTO webtab ( ? , ? )";
         PreparedStatement stmt = con.prepareStatement( sql );
         stmt.setInt( 1, ID );

         /* Get the data */
         URL urlcon = new URL( url );
         InputStreamReader data =
            new InputStreamReader(urlcon.openStream());
         int length = data.read( buffer );
         stmt.setCharacterStream(2, data, length );

         /* Insert the data. */
         stmt.execute();
         stmt.close();
      }
      catch (Exception e) {
         throw new SQLException(e.getMessage(), "38U01");
      }
   }
}