Pages

Basics of Relational Data Model

Edgar Codd proposed Relational Data Model in 1970.


It is a representational or implementation data model.

Using this representational (or implementation) model we represent a database as collection of relations.

The notion of relation here is different from the notion of relationship used in ER modeling.

Relation is the main construct for representing data in relational model.
Every relation consists of a relation schema and Relation instance.



Relation Schema is denoted by  R (A1, A2, A3,……., An),

Customer (Customer ID, Tax ID, Name, Address, City, State, Zip, Phone, Email,Sex)

R--> Relation Name
Ai--> Attributes Name






The number of columns in a relation is known as its degree or arity’.

Relation instance or Relation State (r) of R (thought of as a table)
Each row in the table represents a collection of related data.
Each row contains facts about some entity of same entity-set.

        R = (A1, A2, A3,……., An)
        r(R) is a set of n tuples in R
        r = {t1, t2, t3,…….,tn}

r is an instance of R each t is a tuple and is a ordered list of values.
   
t = (v1  , v2 ,…, vn ) where vi  is an element of domain of Ai   


Characteristics of a  Relation:

Ordering of tuples  is not significant.

Ordering of values in a tuple is  important.

Values in a tuple under each column must be atomic (simple & single).



Using SSH2 in java using jSch



The program SSH (Secure Shell) provides an encrypted channel for logging into another computer over a network, executing commands on a remote computer, and moving files from one computer to another. SSH provides strong host-to-host and user authentication as well as secure encrypted communications over the Internet.


SSH2 is a more secure, efficient, and portable version of SSH that includes SFTP, which is functionally similar to FTP, but is SSH2 encrypted. At Indiana University, UITS has upgraded its central systems to SSH2 (usually the OpenSSH version), and encourages those concerned with secure communications to connect using an SSH2 client.

For more information on SSH2 visit.

JSch is a pure Java implementation of SSH2.

JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs.


For more information on JSch visit

To use SFTP in Java you have to include the jar  file in your classpath.
Below is a code snippet of SFTP in Java using JSch


package com.xyz.utilities.scp;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class SFTP {

    private String hostName;
    private String userName;
    private String password;

    public SFTP(String hostName, String userName, String password) {
        super();
        if (hostName != null && !hostName.isEmpty() && userName != null
                && !userName.isEmpty() && password != null
                && !password.isEmpty()) {
            this.hostName = hostName;
            this.userName = userName;
            this.password = password;
            System.out.println("caleed");
        }

        else {
            System.out.println("caleed");
            throw new NullPointerException("Argument can not be null or empty...");
        }
    }

    public void copy(String source, String destination) {

        try {

            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");

            JSch jsch = new JSch();

            Session session = jsch.getSession(this.userName, this.hostName, 22);
            session.setPassword(password);
            session.setConfig(config);
            session.connect();

            System.out.println("Connected");

            String copyFrom = source;
            String copyTo = destination;

            Channel channel = session.openChannel("sftp");

            channel.connect();

            ChannelSftp sftpChannel = (ChannelSftp) channel;
            sftpChannel.put(copyFrom, copyTo);

            sftpChannel.exit();
            channel.disconnect();
            session.disconnect();
            System.out.println("DONE");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    

}

Serialize and De-serialize java objects

In this post I will tell you about Java serialization and de-serialization.

Serialization is the process of storing the objects state (parameters) in a file, that can later be used.

De-serialization is the reverse process of serialization, means getting the saved state of an object.

Points to remember-

  1. Java provides an serializable (java.io.Serializable) interface. which is a marker interface that not contain any method declaration.  
  2. If you want to serialize an object of a class you must implements Serializable interface.
  3. use transient keyword to serialize only some part of the object. Suppose you does not want to some properties to be serialize during serialization process then mark these properties as transient.
  4. As static variable are the part of class they not take part in serialization

Here is an example how to serialize an object in Java -

package com.esc.test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TestSerilizable implements Serializable {

    private static final long serialVersionUID = 1L;
    private int age;
    private String name;

    public TestSerilizable(int age, String name) {

        this.age = age;
        this.name = name;

    }

    @Override
    public String toString() {

        return age + " " + name;

    }

    public static void main(String[] args) throws IOException {

        TestSerilizable test = new TestSerilizable(11, "abhi");
        System.out.println(test.toString());

        TestSerilizable test2 = new TestSerilizable(12, "rani");
        System.out.println(test2.toString());

        FileOutputStream fileStream = new FileOutputStream("resources/test.ser");
        
        ObjectOutputStream objectStream = new ObjectOutputStream(fileStream);
        
        objectStream.writeObject(test);
        objectStream.close();
    }
}


de-serializing the serialized object
        FileInputStream inputStream = new FileInputStream("resources/test.ser");
        ObjectInputStream objInputStream = new ObjectInputStream(inputStream);
     
        TestSerilizable deSerializeValue =(TestSerilizable)objInputStream.readObject();
        System.out.println("Deserialize value  age  = " + deSerializeValue.age);
        System.out.println("Deserialize value  name  = " + deSerializeValue.name);

Now you get the value of your object after running the above program

Deserialize value  age  = 11
Deserialize value  name  = abhi

Mark some properties as transient that you do not want take part in serializtion

public class TestSerilizable implements Serializable {

    private static final long serialVersionUID = 1L;
    private int age;
    private transient String name; // not save in serialization

    public TestSerilizable(int age, String name) {

        this.age = age;
        this.name = name;

    }

    @Override
    public String toString() {

        return age + " " + name;

    }
}

Now run the de-serilization porogrma again

FileInputStream inputStream = new FileInputStream("resources/test.ser");
        ObjectInputStream objInputStream = new ObjectInputStream(inputStream);
     
        TestSerilizable deSerializeValue =(TestSerilizable)objInputStream.readObject();
        System.out.println("Deserialize value  age  = " + deSerializeValue.age);
        System.out.println("Deserialize value  name  = " + deSerializeValue.name);
        objInputStream.close();

output of the program. in this out put you can see value of name property is null because we mark it as transient, so during serialization its value not persist in the serialization. null is the default value of name as it is a string,

Deserialize value  age  = 11
Deserialize value  name  = null