Wednesday, May 29, 2019

Insert byte[] as a direct value to MySQL update operation in Ballerina

Ballerina version used: 0.992.0

Insert byte[] into BIT data type in MySQL using Java

First, you need to create a database and table as below to work with the following sample.


Sample:

Working with JDBC Client Call Procedures with BIT data type using Ballerina

This sample explains how to execute stored procedures using JDBC Client with BIT data type. Before running the sample, copy the MySQL JDBC driver to the BALLERINA_HOME/bre/lib folder.

Sample:
Ballerina version used: 0.992.0

How to run:

ballerina run callProcedureWithBitDataType.bal

Output:

Creating table and procedures:
Create bittypes table status: 0
Update bittypes table status: 1
Update bittypes table using sql:Parameter. status: 1
Update bittypes table by passing as direct parameter. status: 1
Stored procedure with IN and OUT param creation status: 0
Call operation with OUT params successful
=============bitVal1============= true
=============bitVal2============= 258
Drop stored procedure TestBitOutParam status: 0

Working with Postgresql database using Ballerina

  1. First, install Postgresql database.
  2. Download the postgresql driver jar and copy the Postgresql JDBC driver jar into $BALLERINA_HOME/bre/lib.
  3. Connect to your postgresql server and create a database:

Sample

This is the sample to create a table using the Ballerina jdbc client, then insert a BIT data type record to the table.

Ballerina version used: 0.992.0


Running the sample:

ballerina run workingWithBitDataPostgresql.bal

Output:

Create BitTypes table status: 0
Update BitTypes table status: 1

Working with an Oracle database using Ballerina

Ballerina version used: 0.992.0
Oracle database version used: Oracle-xe-11g (Follow these instructions explained here to install Oracle-xe-11g with Docker).


This Sample is written using ballerinax/jdbc module. Before running this sample you need to copy the Oracle JDBC driver jar into $BALLERINA_HOME/bre/lib.

First, create the table using the following command:

This is a sample to insert byte array into oracle database:

Running the sample:

ballerina run insertByteArray.bal

Output:

Update oracledb table status: 1

Working with mysql BIT data type using Ballerina

This example is written using Ballerina MYSQL Client which is used to access and manipulate data in MYSQL databases.
  1. First, you need to install MySQL. 
  2. Download and install Ballerina 0.992.0.
  3. Copy the MySQL JDBC driver(mysql-connector-java-5.1.39.jar) to the BALLERINA_HOME/bre/lib folder and change the DB connection properties as required.
Sample


How to run:

ballerina run workingWithBitData.bal

Output:
Create database testdb status: 1
Create bittypes table status: 0
Update bittypes table status: 1
Update bittypes table status: 1
===================================
1
true
[0, 0, 0, 0, 0, 0, 0, 14]
2
true
[0, 0, 0, 0, 0, 1, 2, 3]
===================================
Drop Database testdb status: 1

Install SQL*Plus on Ubuntu 16.04

  1. First, you need to download Instant Client Downloads.

  2. Install alien software.

  3. sudo apt-get install alien
    

  4. Navigate to the folder where the downloaded rpm files are located and execute the following commands:

  5. sudo alien -i oracle-instantclient*-basic*.rpm
    sudo alien -i oracle-instantclient*-sqlplus*.rpm
    sudo alien -i oracle-instantclient*-devel*.rpm
    

  6. Type following command to install libaio.so

  7. sudo apt-get install libaio1
    

  8. Create an Oracle configuration file:

  9. sudo nano /etc/ld.so.conf.d/oracle.conf
    

  10. Put the following line in the created file:

  11. /usr/lib/oracle/<your oracle version>/client64/lib/
    

  12. Update the configuration by running the following command:

  13. sudo ldconfig
    

  14. Try to connect using the following command:

  15. sqlplus system/oracle@localhost:49161/xe
    


Installing Oracle XE 11g on Ubuntu

Installing Oracle XE with Docker


Docker is an application container for Linux. It is based on LXC and gives you the ability to package complete application including their dependencies to a self-containing file called an image. These images can be exchanged and run on every Linux machine where Docker is installed.  It is the easiest way to install Oracle XE. Docker images are also shared around the community on https://index.docker.io.

  1. First, you need to install Docker on Ubuntu. Follow the instructions given in this doc.

  2. Pull the image to your machine using the following command :

  3. docker pull arahman/docker-oracle-xe-11g
    

  4. Run the image using the following command :

  5. docker run -d -p 49160:22 -p 49161:1521 -p 49162:8080 arahman/docker-oracle-xe-11g /sbin/my_init
    

  6. Connect to the database with the following setting:

  7. hostname: localhost
    port: 49161
    sid: xe
    username: system
    password: oracle
    

  8. You can connect to the database using SQL*Plus