Netezza JDBC Program
Steps to connect to Netezza Database using Java (JDBC) program
Download the Netezza driver nzjdbc.jar
Connect to Eclipse and upload the nzjdbc.jar using "Build Path" as shown in below images
Java Class to connect to Netezza Database and read table data
---------------------------------------------------
package sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcNetezzaConnection {
public static void main(String[] args) {
Connection conn1 = null;
Statement statement = null;
try {
Class.forName("org.netezza.Driver");
conn1 = DriverManager.getConnection("jdbc:netezza://servername/databasename", "username","password");
if (conn1 != null) {
System.out.println("Connected with connection #1");
}
// Create the statement to be used to get the results.
statement = conn1.createStatement();
// Create a query to use.
String query = "select tablename, owner from _v_table where owner = 'ADMIN' limit 10";
ResultSet resultSet = statement.executeQuery(query);
System.out.println("Printing result...");
while (resultSet.next()) {
String tn = resultSet.getString("tablename");
String opg = resultSet.getString("owner");
System.out.println("\tablename: " + tn + ", owner: " + opg );
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
if (conn1 != null && !conn1.isClosed()) {
conn1.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
----------------------------------------------------
Output of the above class
Output from Netezza Database