Save Big on Cyber Monday! Up to 40% Off
ends in   {{days}}
Days
{{timeFormat.hours}}
:
{{timeFormat.minutes}}
:
{{timeFormat.seconds}}

What is UniDAC?

UniDAC (Universal Data Access Components) is a powerful data access library for Delphi, C++ Builder, and RAD Studio that provides a unified interface for connecting to a wide range of databases, including Oracle, MySQL, PostgreSQL, SQL Server, and IBM Db2. It simplifies database connectivity by offering a set of highly optimized components that allow developers to interact with databases without needing to write complex low-level code. With UniDAC, Delphi developers can easily manage database connections, execute queries, and handle data from various relational database management systems, all while maintaining high performance and reliability.

What is UniDAC

What is IBM Db2?

Db2 is a relational database management system developed by IBM. It's known for its scalability, high availability, and strong support for complex data workloads. IBM Db2 offers various features like ACID compliance, sophisticated indexing, and support for both SQL and NoSQL models. It's widely used in enterprise environments for managing mission-critical applications and large volumes of data. IBM Db2 supports multiple platforms, including Linux, UNIX, and Windows, making it a flexible choice for modern database solutions.

Connect to IBM Db2 from Delphi via UniDAC

UniDAC enables you to configure the connection to your Db2 database visually in the Form Designer or programmatically in the Code Editor.

Prerequisites
  1. Install RAD Studio.
  2. Install UniDAC from Devart.
  3. Install IBM Data Server Driver Package (DS Driver). You can download it from the IBM website.
  4. Prepare IBM Db2 connection details: host name, port number, database name, username, and password.

Configure the connection in the Form Designer

  1. Open RAD Studio.
  2. Select File > New > Windows VCL Application - Delphi to create a new Delphi project.
RAD Studio with the expanded New submenu under the File menu.
  1. Add the TUniConnection component:
    • 3.1. On the Palette, expand the UniDAC category.
    • 3.2. Locate the TUniConnection component and drag it to the form.
The form in RAD Studio with the added TUniConnection component.
  1. Double-click the UniConnection1 label on the form, then specify the following TUniConnection properties:
    • Provider – Select DB2.
    • Server – Enter your Db2 server host name.
    • Port – Enter your Db2 port number. The default Db2 port is 50000.
    • Username – Enter your Db2 database username.
    • Password – Enter your Db2 database password.
    • Database – Enter your Db2 database name.
  2. Clear the LoginPrompt checkbox.
  3. Click Connect.

Configure the connection from the Code Editor

  1. In your Delphi project, place the TUniConnection component on the form.
  2. Open the Code Editor.
  3. Configure the TUniConnection properties programmatically to establish the connection.
    You can place the code like this within a method that you call when your form is created or when a specific event is triggered.
// Configure connection
UniConnection1.ProviderName := 'DB2';
UniConnection1.Server := 'your-db2-server-address';
UniConnection1.Port := your-db2-port;
UniConnection1.Username := 'your-username';
UniConnection1.Password := 'your-password';
UniConnection1.Database := 'your-database';
UniConnection1.LoginPrompt := False;

try
  // Try to connect
  UniConnection1.Connect;
  ShowMessage('Connection successful.');

except
  on E: Exception do
    ShowMessage('Connection failed: ' + E.Message);
end;

Retrieve IBM Db2 data via UniDAC

You can use UniDAC components to retrieve data from a Db2 database visually (at design time) or using code (at runtime).

Retrieve data at design time

  1. Drag the following components from the Palette to the form:
    • TUniQuery (the UniDAC category).
    • TUniDataSource (the UniDAC category).
    • TDB2UniProvider (the UniDAC Providers category).
    • TDBGrid (the Data Controls category).
    • TButton (the Standard category).
  1. Select the DBGrid1 label on the form and, in the Object Inspector, set the DataSource property to the UniDataSource1 value.
  2. Select the UniDataSource1 label on the form and, in the Object Inspector, set the DataSet property to the UniQuery1 value.
  3. Double-click the UniQuery1 label on the form, then enter a query to select data from the database. In this example, we'll retrieve the EMPLOYEE table.
SELECT * FROM EMPLOYEE;
  1. Click OK to save the changes and close the dialog.
  1. Double-click the Button1 label on the form, then add the following code to the button's click event to call the Open method and execute the SQL query in your application.
UniQuery1.Open;
  1. Click Run.
  2. In the window that opens, click Button1 to display the query result.

Retrieve data at runtime

  1. Drag the following components from the Palette to the form:
    • TUniQuery (the UniDAC category).
    • TUniDataSource (the UniDAC category).
    • TDB2UniProvider (the UniDAC Providers category).
  2. Select the UniDataSource1 label on the form and, in the Object Inspector, set the DataSet property to UniQuery1.
  3. In the Code Editor, add the following code to call the Open method and execute the SQL query in your application—for example, to retrieve the EMPLOYEE table.
UniQuery1.SQL.Text := 'SELECT * FROM EMPLOYEE';
UniQuery1.Open;

Manipulate IBM Db2 data via UniDAC

You can add, edit, or delete Db2 data visually (at design time) or using code (at runtime).

Manipulate data at design time

  1. Double-click the UniQuery1 label on the form.
  2. Enter an INSERT, UPDATE, or DELETE statement. For example, we'll add a record to the EMPLOYEE table.
INSERT INTO EMPLOYEE (
    EMPNO, FIRSTNME, MIDINIT, LASTNAME, WORKDEPT, PHONENO, HIREDATE,
    JOB, EDLEVEL, SEX, BIRTHDATE, SALARY, BONUS, COMM
)
VALUES (
    '200350', 'JANE', 'M', 'DOE', 'C01', '4821', '2020-06-15', 'ANALYST', 
    16, 'F', '1985-03-12', 72000, 500, 2000
);
  1. Click Execute to run the statement.
  2. Optional: Retrieve data from the modified table to check the result.

Manipulate data at runtime

  1. In the class declaration for your form, define a method that will run a SQL statement to manipulate data in a Db2 table. For this example, let it be the ModifyTable method.
procedure ModifyTable;
  1. In the implementation section, add the ModifyTable method with an INSERT, UPDATE, or DELETE statement. Our examples follow.
  • Example with an INSERT statement.
procedure TForm5.ModifyTable;
begin
  // Write the INSERT statement
  UniQuery1.SQL.Text := 'INSERT INTO EMPLOYEE (EMPNO, FIRSTNME, MIDINIT, LASTNAME, ' +
                        'WORKDEPT, PHONENO, HIREDATE, JOB, EDLEVEL, SEX, BIRTHDATE, ' +
                        'SALARY, BONUS, COMM) ' +
                        'VALUES (:EMPNO, :FIRSTNME, :MIDINIT, :LASTNAME, :WORKDEPT, ' +
                        ':PHONENO, :HIREDATE, :JOB, :EDLEVEL, :SEX, :BIRTHDATE, ' +
                        ':SALARY, :BONUS, :COMM)';

  // Assign parameter values
  UniQuery1.ParamByName('EMPNO').AsString := '200350';
  UniQuery1.ParamByName('FIRSTNME').AsString := 'JANE';
  UniQuery1.ParamByName('MIDINIT').AsString := 'M';
  UniQuery1.ParamByName('LASTNAME').AsString := 'DOE';
  UniQuery1.ParamByName('WORKDEPT').AsString := 'C01';
  UniQuery1.ParamByName('PHONENO').AsString := '4821';
  UniQuery1.ParamByName('HIREDATE').AsDateTime := StrToDate('6/15/2020');
  UniQuery1.ParamByName('JOB').AsString := 'ANALYST';
  UniQuery1.ParamByName('EDLEVEL').AsInteger := 16;
  UniQuery1.ParamByName('SEX').AsString := 'F';
  UniQuery1.ParamByName('BIRTHDATE').AsDateTime := StrToDate('3/12/1985');
  UniQuery1.ParamByName('SALARY').AsFloat := 46000;
  UniQuery1.ParamByName('BONUS').AsFloat := 500;
  UniQuery1.ParamByName('COMM').AsFloat := 2000;

  try
    // Execute the INSERT statement
    UniQuery1.Execute;
    ShowMessage('Employee inserted successfully!');
  except
    on E: Exception do
      ShowMessage('Failed to insert employee: ' + E.Message);
  end;
end;  
  • Example with an UPDATE statement.
procedure TForm5.ModifyTable;
begin
  // Write the UPDATE statement
  UniQuery1.SQL.Text := 'UPDATE EMPLOYEE ' +
                        'SET SALARY = :SALARY ' +
                        'WHERE EMPNO = :EMPNO';

  // Set the new value for the parameter and specify the employee number to update
  UniQuery1.ParamByName('SALARY').AsFloat := 72000;
  UniQuery1.ParamByName('EMPNO').AsString := '200350';

  try
    // Execute the UPDATE statement
    UniQuery1.Execute;
    ShowMessage('Employee details updated successfully!');
  except
    on E: Exception do
      ShowMessage('Failed to update employee details: ' + E.Message);
  end;
end;
  • Example with a DELETE statement.
procedure TForm5.ModifyTable;
begin
  // Write the DELETE statement
  UniQuery1.SQL.Text := 'DELETE FROM EMPLOYEE WHERE EMPNO = :EMPNO';

  // Specify the employee number to delete
  UniQuery1.ParamByName('EMPNO').AsString := '200350';

  try
    // Execute the DELETE statement
    UniQuery1.Execute;
    ShowMessage('Employee record deleted successfully!');
  except
    on E: Exception do
      ShowMessage('Failed to delete employee: ' + E.Message);
  end;
end;
  1. Add code to call the ModifyTable method. In our example, we're calling it when clicking the button in the form.
procedure TForm5.Button1Click(Sender: TObject);
begin
  ModifyTable;
  UniQuery1.Close;
end;

Conclusion

Using UniDAC with IBM Db2 provides Delphi developers with a powerful and efficient way to connect to and manage data in Db2 databases. UniDAC simplifies the process by offering a unified interface for seamless database connectivity, while IBM Db2 delivers the reliability, scalability, and performance required for enterprise-level applications. This combination is especially useful for developers building applications that need to handle large volumes of data, support high availability, and integrate smoothly with IBM Db2 across multiple platforms.