private static String user = "tduser";
private static String password = "passwd";
private static String url = "jdbc:teradata://TDServer/TMODE=ANSI,CHARSET=UTF8";
private String sqlGetData = "SELECT * FROM dbc.tables WHERE databasename='<dbname>'";
public static void getData(String sqlGetData) {
Statement statement = null;
Connection connection = null;
ResultSet resultSet = null;
try {
Class.forName("com.teradata.jdbc.TeraDriver");
connection = DriverManager.getConnection(url, user, password);
statement = connection.createStatement();
resultSet = statement.executeQuery(sqlGetData);
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int columnCount = resultSetMetaData.getColumnCount();
for (int i=1; i<=columnCount; i++) {
System.out.print(resultSetMetaData.getColumnName(i).toUpperCase() + ",");
}
System.out.println("");
while (resultSet.next()) {
for (int i=1; i<=columnCount; i++) {
System.out.print(resultSet.getString(i) + ",");
}
System.out.println("");
}
}
catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
finally {
if (statement != null) {
try {
statement.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
}
↧