Implementing Informix aggregation queries using Java
To implement various aggregation queries for Informix using Java, you need to complete the following steps:
1. Import necessary dependencies: You can use the Informix JDBC driver to connect to the Informix database. You can add the following dependencies in the Maven project:
<dependency>
<groupId>com.ibm.informix</groupId>
<artifactId>ifxjdbc</artifactId>
<version>4.50.4.0</version>
</dependency>
2. Create a database connection: Use the following code example to create an Informix database connection:
import java.sql.*;
public class InformixConnectionUtil {
public static Connection getConnection() {
Connection connection = null;
String jdbcUrl = "jdbc:informix-sqli://localhost:9088/mydatabase:user=myuser;password=mypassword";
try {
Class.forName("com.informix.jdbc.IfxDriver");
connection = DriverManager.getConnection(jdbcUrl);
}Catch (ClassNotFoundException | SQLException e){
e.printStackTrace();
}
return connection;
}
}
Ensure that the correct JDBC URL, username, and password are used.
3. Implement various aggregation queries: Use the following code example to implement various data aggregation queries.
a) Count aggregation query:
import java.sql.*;
public class CountQueryExample {
public static void main(String[] args) {
try (Connection connection = InformixConnectionUtil.getConnection()) {
String sql = "SELECT COUNT(*) FROM mytable";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
int count = resultSet.getInt(1);
System.out.println("Count: " + count);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
b) Sum aggregation query:
import java.sql.*;
public class SumQueryExample {
public static void main(String[] args) {
try (Connection connection = InformixConnectionUtil.getConnection()) {
String sql = "SELECT SUM(salary) FROM employees";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
double sum = resultSet.getDouble(1);
System.out.println("Sum: " + sum);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
c) Average aggregation query:
import java.sql.*;
public class AverageQueryExample {
public static void main(String[] args) {
try (Connection connection = InformixConnectionUtil.getConnection()) {
String sql = "SELECT AVG(score) FROM scores";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
double average = resultSet.getDouble(1);
System.out.println("Average: " + average);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
d) Maximum aggregation query:
import java.sql.*;
public class MaxQueryExample {
public static void main(String[] args) {
try (Connection connection = InformixConnectionUtil.getConnection()) {
String sql = "SELECT MAX(price) FROM products";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
double max = resultSet.getDouble(1);
System.out.println("Max: " + max);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
e) Minimum aggregation query:
import java.sql.*;
public class MinQueryExample {
public static void main(String[] args) {
try (Connection connection = InformixConnectionUtil.getConnection()) {
String sql = "SELECT MIN(stock) FROM products";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
int min = resultSet.getInt(1);
System.out.println("Min: " + min);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Ensure to replace the corresponding table names, column names, and conditions in the code.
This way, you can use Java to implement various aggregation queries for Informix.