Learn: How to Connect Netezza using JDBC Driver and Working Examples

Explore the process of connecting to Netezza by utilizing various methods and programming languages. Netezza supports ODBC, OLEDB, and JDBC drivers for connections.

Before you begin

Netezza JDBC Driver

Connecting to Netezza using the JDBC driver is straightforward and commonly employed.

Netezza offers a JDBC driver, which you can utilize with any programming language that supports JDBC connections like Java, Python, etc. You can download the JDBC driver from IBM fix central site. You should have the jdbc jar named nzjdbc.jar.

Alternatively, you will receive software packages if you purchase Netezza or IBM PureData Systems for analytics.

1.1) Install Jaydebeapi

The JayDeBeApi module allows you to connect from Python code to databases using Java JDBC. It provides a Python DB-API v2.0 to that database.

Install it using pip:

pip install Jaydebeapi

1.2) Setting Database Connection Settings

dsn_database = "DBNAME"
dsn_hostname = "191.168.100.10"
dsn_port = "5480"
dsn_uid = "admin"
dsn_pwd = "pwd"
jdbc_driver_name = "org.netezza.Driver"
jdbc_driver_loc = os.path.join('D:\\Work\\Connections_Softwares\\Jar\\nzjdbc.jar')
sql_str = "select now()"
connection_string=’jdbc:netezza://’+dsn_hostname+’:’+dsn_port+’/’+dsn_database
url = '{0}:user={1};password={2}'.format(connection_string, dsn_uid, dsn_pwd)
print("Connection String: " + connection_string)

1.3) Creating Database Connection

connection = jaydebeapi.connect(jdbc_driver_name, connection_string, {'user': dsn_uid, 'password': dsn_pwd}, jars=jdbc_driver_loc)

1.4) Processing SQL Query

cursor = connection.cursor()
cursor.execute(sql_str)
results = cursor.fetchall()
print(results[0])

1.5) Closing all connections

cursor.close()
connection.close()

Learning to Connect Netezza Using JDBC Driver: Working Examples

Introduction

In this tutorial, we will walk you through the process of connecting to a Netezza database using Java and its JDBC (Java Database Connectivity) driver. We'll provide you with working examples to get started quickly.

Prerequisites

Step 1: Download the Netezza JDBC driver

You can download the IBM DataServer Driver for JDBC and SQLJ (Type 4) from this link:

Download Netezza JDBC driver

Step 2: Create a Java project and add the JAR file to your classpath

Create a new Java project, and add ibmdb4.jar to your classpath.

Step 3: Write the Java code to connect to Netezza using JDBC

Example 1 - Simple Connection


import java.sql.*;

public class NetezzaConnection {
    public static void main(String[] args) {
        String url = "jdbc:db2://hostname:port/database";
        String username = "username";
        String password = "password";

        try (Connection connection = DriverManager.getConnection(url, username, password)) {
            System.out.println("Connected to Netezza successfully!");
        } catch (SQLException e) {
            System.err.println("Error connecting to Netezza: " + e.getMessage());
        }
    }
}

Example 2 - Executing a Query


import java.sql.*;

public class NetezzaQuery {
    public static void main(String[] args) {
        String url = "jdbc:db2://hostname:port/database";
        String username = "username";
        String password = "password";

        try (Connection connection = DriverManager.getConnection(url, username, password)) {
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT * FROM table");

            while (resultSet.next()) {
                System.out.println(resultSet.getString(1) + ", " + resultSet.getString(2));
            }
        } catch (SQLException e) {
            System.err.println("Error executing the query: " + e.getMessage());
        }
    }
}

Conclusion

In this tutorial, we have shown you how to connect to a Netezza database using Java and its JDBC driver. We provided simple examples for establishing a connection and executing queries.

Next Steps