How to Use DB2 CLI on Linux

After connecting to a DB2 database on a Linux system, you can interact with the database using the following command-line tools or programming languages:

Use the command line tool db2cli: You can connect to a DB2 database and execute SQL queries using the db2cli command line tool. For example, you can connect to the database using the following command.

db2cli connect to <database_name> user <username> using <password>

Next, you can perform SQL queries or other database operations.

2. Connecting to DB2 using programming languages: You can use programming languages such as Java, Python, etc. to connect to a DB2 database and execute queries. For example, code written in Java can connect to the database through JDBC and execute SQL queries as shown below.

import java.sql.*;

public class DB2Example {
   public static void main(String[] args) {
      try {
         String url = "jdbc:db2://<hostname>:<port>/<database_name>";
         String username = "<username>";
         String password = "<password>";

         Connection con = DriverManager.getConnection(url, username, password);
         Statement stmt = con.createStatement();

         ResultSet rs = stmt.executeQuery("SELECT * FROM <table_name>");

         while (rs.next()) {
            // process query result
         }

         rs.close();
         stmt.close();
         con.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

You are able to connect to the DB2 database and perform various operations using other programming languages and tools as needed. I hope this helps you successfully connect to and utilize DB2.

bannerAds