GO/GoLang is an open source programming language that is getting popular in the recent years because of its performance, memory usage, built-in concurrency mechanism, and powerful libraries, etc.
In this blog, I am going to show you how to to connect to Db2 for z/OS using a GO application and perform some basic database operations.
Here is my setup:
- Ubuntu 18
- Db2 11 for z/OS
Pre-requisites
Activation utility is ran in the Db2 server. Otherwise, ODBC license file for Db2 for z/OS is required
Step 1 : Install GO
I install GO in /root
directory, you can install in any directory you want.
wget https://dl.google.com/go/go1.11.2.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.11.2.linux-amd64.tar.gz
Details can be found in GO website.
Add following to /root/.bashrc
so the GO environment is setup whenever you login.
export PATH=$PATH:/usr/local/go/bin
export GOPATH=/root/go/
Step 2 : Install go_ibm_db
Install go_ibm_db
from GitHub
go get -d github.com/ibmdb/go_ibm_db
ibmdb
will be installed in /root/go/src/github.com
Step 3 : Install CLIdriver
You can skip this step if you already installed CLIdriver.
cd /root/go/src/github.com/ibmdb/go_ibm_db/installer
chmod 777 setup.go
go run ./setup.go
Step 4 : Install gcc
You can skip this step if you already have gcc
installed.
sudo apt update
apt install gcc
gcc --version
Step 5: Implement your first database GO application
Below is an application (example1.go
) I adapted from GitHub to select first 3 rows from a catalog table. I have to admit there is plenty of room for improvement (like error handling, etc.).
To make the application easy to read, I put everything in main()
. If you have implemented database application before, you will see familiar steps here:
- import required libraries
- connect to your database
- specify a query. In this application, I select first 3 rows from a catalog table.
- prepare the query
- execute the query
- loop through the resultset, print out the resultset.
- close the connection
package mainimport (
_ "github.com/ibmdb/go_ibm_db"
"database/sql"
"fmt"
)func main(){
con:="HOSTNAME=<host>;DATABASE=<name>;PORT=<number>;UID=<username>;PWD=<password>"
db, err:=sql.Open("go_ibm_db", con)
if err != nil{
fmt.Println(err)
}query := "SELECT STRINGID, SUBSTR(STRING,1,60) AS STRING FROM SYSIBM.SYSXMLSTRINGS ORDER BY STRINGID FETCH FIRST 3 ROWS ONLY"
st, err:=db.Prepare(query)
if err !=nil{
fmt.Println(err)
}rows, err:=st.Query()
if err!=nil{
fmt.Println(err)
}cols, _:=rows.Columns()
fmt.Printf("%s %s\n",cols[0],cols[1])
fmt.Println("-------------------------------------")defer rows.Close()
for rows.Next(){
var strid,str string
err = rows.Scan(&strid, &str)
if err !=nil{
fmt.Println(err)
}
fmt.Printf("%v %v\n",strid,str)
}
db.Close()
}
Note: remember to update this line with your own Db2 info
con:="HOSTNAME=<host>;DATABASE=<name>;PORT=<number>;UID=<username>;PWD=<password>"
Step 6 : Execute your GO application
Setup your environment variables.
export DB2HOME=/root/go/src/github.com/ibmdb/go_ibm_db/installer/clidriver
export CGO_CFLAGS=-I$DB2HOME/include
export CGO_LDFLAGS=-L$DB2HOME/lib
export LD_LIBRARY_PATH=$DB2HOME/lib
Run your application
go run example1.go
Sample output:
STRINGID STRING
-------------------------------------
1001 product
1002 description
1003 name
Troubeshooting
- License issue
SQLDriverConnect: {42968} [IBM][CLI Driver] SQL1598N An attempt to connect to the database server failed because of a licensing problem. SQLSTATE=42968
This can be solved by executing the activation utility or copying the Db2 for z/OS license file db2consv_zs.lic
to /root/go/src/github.com/ibmdb/go_ibm_db/installer/clidriver/license
directory.
2. -805 error
SQLExecute: {51002} [IBM][CLI Driver][DB2] SQL0805N Package "STLEC1.NULLID.SYSSH200.5359534C564C3031" was not found. SQLSTATE=51002
Contact your System Admin to bind the ODBC packages.