Tuesday, November 24, 2020

How to run .SQL script using JDBC?

 

A database script file is a file that contains multiple SQL quries separated from each other. Usually, these files have the .sql extention.

Running .sql script files in Java

You can execute .sql script files in Java using the runScript() method of the ScriptRunner class of Apache iBatis. To this method you need to pass a connection object.

Therefore to run a script file −

  • Register the MySQL JDBC Driver using the registerDriver() method of the DriverManager class.
  • Create a connection object to establish connection with the MySQL database using the getConnection() method.
  • Initialize the ScriptRunner class of the package org.apache.ibatis.jdbc.
  • Create a Reader object to read the script file.
  • Finally, execute the script using the runScript(reader) method.

Example

Let us create a script file with name sampleScript.sql copy the following contents init. This script creates a table with name cricketers_data in MySQL database an populates it with five records.

CREATE DATABASE exampleDB;
use exampleDB;
CREATE TABLE exampleDB.cricketers_data(
   First_Name VARCHAR(255),
   Last_Name VARCHAR(255),
   Date_Of_Birth date,
   Place_Of_Birth VARCHAR(255),
   Country VARCHAR(255)
);
insert into cricketers_data values('Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India');
insert into cricketers_data values('Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica');
insert into cricketers_data values('Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka');
insert into cricketers_data values('Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India');
insert into cricketers_data values('Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India');
select * from mydatabase.cricketers_data;

Add the following maven dependency (for the jar file mybatis-3.4.1.jar) to your pom.xml file −

<dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis</artifactId>
   <version>3.4.5</version>
</dependency>

Example

Following JDBC program executes the sampleScript.sql file.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.ibatis.jdbc.ScriptRunner;
public class RunningScripts {
   public static void main(String args[]) throws Exception {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/talakai_noppi";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Initialize the script runner
      ScriptRunner sr = new ScriptRunner(con);
      //Creating a reader object
      Reader reader = new BufferedReader(new FileReader("E:\\sampleScript.sql"));
      //Running the script
      sr.runScript(reader);
   }
}

Output

Connection established......
CREATE DATABASE exampleDB
use exampleDB
CREATE TABLE exampleDB.cricketers_data(
   First_Name VARCHAR(255),
   Last_Name VARCHAR(255),
   Date_Of_Birth date,
   Place_Of_Birth VARCHAR(255),
   Country VARCHAR(255)
)
insert into cricketers_data values('Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India')
insert into cricketers_data values('Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica')
insert into cricketers_data values('Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka')
insert into cricketers_data values('Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India')
insert into cricketers_data values('Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India')
select * from mydatabase.cricketers_data
First_Name Last_Name Year_Of_Birth Place_Of_Birth Country
Shikhar Dhawan 1981-12-05 Delhi India
Jonathan Trott 1981-04-22 CapeTown SouthAfrica
Lumara Sangakkara 1977-10-27 Matale Srilanka
Virat Kohli 1988-11-05 Delhi India
Rohit Sharma 1987-04-30 Nagpur India
 Reference: https://www.tutorialspoint.com/how-to-run-sql-script-using-jdbc 

Wednesday, November 4, 2020

How to get a list of images on docker registry v2

 List all repositories (effectively images):

curl -X GET https://myregistry:5000/v2/_catalog
> {"repositories":["redis","ubuntu"]}

List all tags for a repository:

curl -X GET https://myregistry:5000/v2/ubuntu/tags/list
> {"name":"ubuntu","tags":["14.04"]}

Saturday, October 31, 2020

Install letsencrypt cer to trust CA in ubuntu

Install ca-certificates

sudo apt-get install ca-certificates

Down load cer from letsencrypt

cd /usr/share/ca-certificates
sudo wget https://letsencrypt.org/certs/isrgrootx1.pem  -O isrgrootx1.crt
sudo wget https://letsencrypt.org/certs/letsencryptauthorityx3.pem  -O letsencryptauthorityx3.crt

Update CA

sudo dpkg-reconfigure ca-certificates

Wednesday, October 14, 2020

Endpoint is not Created for Service in Kubernetes

The Problem

Endpoints shows ‘none’:

$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.yy.0.1 <none> 443/TCP 9d
test ClusterIP 10.xx.97.97 <none> 6379/TCP 21s
$ kubectl describe svc test
Name: test
Namespace: default
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"test","namespace":"default"},"spec":{"clusterIP":"10.xx.97.97","...
Selector: app=test
Type: ClusterIP
IP: 10.xx.97.97
Port: <unset> 6379/TCP
TargetPort: 6379/TCP
Endpoints: <none>
Session Affinity: None
Events: <none>

The Solution

The service selector doesn’t match any Pod’s labels.

$ kubectl get pods --show-labels |egrep 'app=test'
$

1. Edit the yaml file and correct the selector to match the Pod’s label.

$ kubectl get pods --show-labels |egrep 'app=filebeat'
myapp-ds-c2fwm 1/1 Running 0 21h app=filebeat,controller-revision-hash=54ccfc87bd,pod-template-generation=1,release=stable
myapp-ds-rbn4z 1/1 Running 0 21h app=filebeat,controller-revision-hash=54ccfc87bd,pod-template-generation=1,release=stable
$ vi test-svc.yaml
apiVersion: v1
apiVersion: v1
kind: Service
metadata:
name: test
namespace: default
spec:
selector:
app: filebeat
clusterIP: 10.xx.97.97
type: ClusterIP
ports:
- port: 6379
targetPort: 6379

2. Apply the configuration:

$ kubectl apply -f test-svc.yaml
service/test created
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.yy.0.1  443/TCP 9d
test ClusterIP 10.xx.97.97  6379/TCP 29m

3. Show the details of the service:

$ kubectl describe svc test
Name: test
Namespace: default
Labels: [none]
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"test","namespace":"default"},"spec":{"clusterIP":"10.xx.97.97","...
Selector: app=filebeat
Type: ClusterIP
IP: 10.xx.97.97
Port: [unset] 6379/TCP
TargetPort: 6379/TCP
Endpoints: 10.zzz.1.38:6379,10.zzz.2.36:6379
Session Affinity: None
Events: [none]
$ kubectl get endpoints test
NAME ENDPOINTS AGE
test 10.zzz.1.38:6379,10.zzz.2.36:6379 39m

 Ref: https://www.thegeekdiary.com/endpoint-is-not-created-for-service-in-kubernetes/

Tuesday, September 8, 2020

Run Docker Container as a Service

 Ref: https://www.jetbrains.com/help/youtrack/standalone/run-docker-container-as-service.html


Docker team recommends to use cross-platform built-in restart policy for running container as a service. For this, configure your docker service to start on system boot and simply add parameter --restart unless-stopped to the docker run command that starts YouTrack.


However, when it comes to the sequential start of several services (including YouTrack), the restart policy method will not suit. You can use a process manager instead.


Here's an example of how to run YouTrack container as a service on Linux with help of systemd.


To run YouTrack container as a service on Linux with systemd:

Create a service descriptor file /etc/systemd/system/docker.youtrack.service:

[Unit]
Description=YouTrack Service
After=docker.service
Requires=docker.service
[Service]
TimeoutStartSec=0
Restart=always
ExecStartPre=-/usr/bin/docker exec %n stop
ExecStartPre=-/usr/bin/docker rm %n
ExecStartPre=/usr/bin/docker pull jetbrains/youtrack:<version>
ExecStart=/usr/bin/docker run --rm --name %n \
    -v <path to data directory>:/opt/youtrack/data \
    -v <path to conf directory>:/opt/youtrack/conf \
    -v <path to logs directory>:/opt/youtrack/logs \
    -v <path to backups directory>:/opt/youtrack/backups \
    -p <port on host>:8080 \
    jetbrains/youtrack:<version>
[Install]
WantedBy=default.target


Enable starting the service on system boot with the following command:


systemctl enable docker.youtrack


You can also stop and start the service manually at any moment with the following commands, respectively:


sudo service docker.youtrack stop

sudo service docker.youtrack start


Friday, August 28, 2020

Docker insecure registry

 Edit file /usr/lib/systemd/system/docker.service

add --insecure-registry=myregistrydomain.com:5000

to line 

ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock


reload and restart docker service

systemctl daemon-reload

service docker restart

Wednesday, August 5, 2020

T24 Error: No component defined. $PACKAGE is mandatory !

To fix the issue add this line to tafj.properties( in $TAFJ_HOME/conf folder)
 temn.tafj.compiler.component.strict.mode=false 

Install and use xorg-server on macOS via Homebrew

  The instructions to install and use xorg-server on macOS via Homebrew: Install Homebrew (if you haven't already): /bin/bash -c ...