A note on connecting to Teradata from a Java application using the Teradata JDBC Driver.
Download the driver from here. A Teradata Downloads account is required to download.
Place the obtained terajdbc4.jar under {PROJECT_ROOT} / libs
.
In addition, add the following to the dependencies block of build.gradle and add it as a dependency package.
dependencies {
implementation files('libs/terajdbc4.jar')
}
(* In versions prior to 16.20.00.11, it is necessary to place and add tdgssconfig.jar as well)
Set TeraDataSource which is the implementation of DataSource, get the connection and execute the query. I will. (Of course you can write using DriverManager, but it is recommended to use DataSource.)
The code I wrote roughly is below.
import com.teradata.jdbc.TeraDataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Application {
public static void main(String[] args){
TeraDataSource teraDataSource = new TeraDataSource();
//Set according to the environment
//Parameter details: https://teradata-docs.s3.amazonaws.com/doc/connectivity/jdbc/reference/current/jdbcugjp/jdbcug_chapter_2.html#BABJIHBJ
teraDataSource.setDSName("your.teradata.host");
teraDataSource.setUser("johndoe");
teraDataSource.setPassword("XXXX");
teraDataSource.setLOGMECH("TD2");
teraDataSource.setTMODE("ANSI");
teraDataSource.setCHARSET("UTF8");
teraDataSource.setENCRYPTDATA("ON");
String query = "SELECT id FROM yourdb.yourtable";
try (Connection conn = teraDataSource.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query)){
while (rs.next()) {
// ResultSet#column of getXXX is 1-Note that it is based index
System.out.println(rs.getInt(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
You have now selected a Teradata table.
https://docs.gradle.org/current/userguide/dependency_types.html#sub:file_dependencies https://teradata-docs.s3.amazonaws.com/doc/connectivity/jdbc/reference/current/jdbcugjp/jdbcug_chapter_2.html https://docs.oracle.com/javase/jp/8/docs/api/javax/sql/DataSource.html
Recommended Posts