Using Java to Operate IBM DB2
To operate an IBM DB2 database using Java, we can use the DB2 JDBC driver to implement it. The following are the basic steps for Java to operate IBM DB2:
1. Add Maven dependency:
In order to use the IBM DB2 JDBC driver, we need to add the following Maven dependencies to the pom.xml file of the project:
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc</artifactId>
<version>11.5.5.0</version>
</dependency>
Please note that the version number should be adjusted based on your version of DB2.
2. Connect to database:
Firstly, we need to create a Connection object to connect to the DB2 database. The following is an example code for connecting to a DB2 database:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DB2ConnectionExample {
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 DB2 database!");
} catch (SQLException e) {
System.out.println("Failed to connect to DB2 database!");
e.printStackTrace();
}
}
}
Please remember to replace 'url', 'username', and 'password' with the actual database connection information.
3. Insertion, modification, query, and deletion of data:
Once connected to the database, we can perform data insertion, modification, query, and deletion operations using. The following is an example code for a DB2 database:
import java.sql.*;
public class DB2CRUDExample {
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 DB2 database!");
//Insert Data
String insertQuery = "INSERT INTO users (id, name, age) VALUES (?, ?, ?)";
PreparedStatement insertStatement = connection.prepareStatement(insertQuery);
insertStatement.setInt(1, 1);
insertStatement.setString(2, "John");
insertStatement.setInt(3, 25);
insertStatement.executeUpdate();
System.out.println("Data inserted successfully!");
//Modify data
String updateQuery = "UPDATE users SET age = ? WHERE id = ?";
PreparedStatement updateStatement = connection.prepareStatement(updateQuery);
updateStatement.setInt(1, 30);
updateStatement.setInt(2, 1);
updateStatement.executeUpdate();
System.out.println("Data updated successfully!");
//Query data
String selectQuery = "SELECT * FROM users";
Statement selectStatement = connection.createStatement();
ResultSet resultSet = selectStatement.executeQuery(selectQuery);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
}
//Delete data
String deleteQuery = "DELETE FROM users WHERE id = ?";
PreparedStatement deleteStatement = connection.prepareStatement(deleteQuery);
deleteStatement.setInt(1, 1);
deleteStatement.executeUpdate();
System.out.println("Data deleted successfully!");
} catch (SQLException e) {
System.out.println("Failed to connect to DB2 database!");
e.printStackTrace();
}
}
}
Please remember to replace 'url', 'username', and 'password' with the actual database connection information, and modify the table name, field name, and operation logic as needed.
The above are the basic steps and sample code for using Java to operate an IBM DB2 database. You can modify and expand according to actual needs.