Apache POI (Poor Obfuscation Implementation File System)

Prerequisite
Download all poi jar files
https://poi.apache.org/download.html


  1.  Terms
    • Workbook – The file contains number of sheets
    • Sheet – Matrix contains rows and cell
    • Row – Record of an entity in one row
    • Cell – A single cell which hold value
  2. JFileChooser fileChooser = new JFileChooser();
            int returnValue = fileChooser.showDialog(null, "Select File");
            
            if(returnValue == JFileChooser.APPROVE_OPTION) {
                Workbook workbook = new HSSFWorkbook(new FileInputStream(fileChooser.getSelectedFile()));
                
                Sheet sheet = workbook.getSheetAt(0);
                
                for(Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();){
                    for(Iterator<Cell> cit = rit.next().cellIterator(); cit.hasNext();){
                        System.out.println(cit.next());
                    }
                    System.out.println("");
                }
            } else {
                System.out.println("Invalid output");
            }
  3. JFileChooser fileChooser = new JFileChooser();
            int returnValue = fileChooser.showDialog(null, "Select File");
            
            if(returnValue == JFileChooser.APPROVE_OPTION) {
                FileInputStream excelFile = new FileInputStream(fileChooser.getSelectedFile());
                Workbook workbook = new XSSFWorkbook(excelFile);
                
                Sheet sheet = workbook.getSheetAt(0);
                
                for(Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) {
                    for(Iterator<Cell> cit = rit.next().cellIterator(); cit.hasNext();) {
                        System.out.print(cit.next() + "\t" + cit.next());
                    }
                    System.out.println("");
                }
            }
  4.  Workbook workbook = new HSSFWorkbook();
            
            Sheet sheet1 = workbook.createSheet("movies");
    //        Sheet sheet2 = workbook.createSheet("Test Cases");
    //        Sheet sheet3 = workbook.createSheet(WorkbookUtil.createSafeSheetName("$*(^&?"));
            
            Row row = sheet1.createRow(0);
            
    //        Cell cell = row.createCell(4);
    //        cell.setCellValue("Hello World");
    //        Cell cell2 = row.createCell(3);
    //        cell2.setCellValue("terminator");
    //        System.out.println(cell.getRichStringCellValue().toString());
    //        System.out.println(cell2.getRichStringCellValue().toString());
    
            Cell cell1 = row.createCell(0);
            Cell cell2 = row.createCell(1);
            Cell cell3 = row.createCell(2);
            Cell cell4 = row.createCell(3);
            Cell cell5 = row.createCell(4);
            
            cell1.setCellValue(5);
            cell2.setCellValue("+");
            cell3.setCellValue(6);
            cell4.setCellValue("=");
            cell5.setCellFormula("A1+C1");
            
            try {
                FileOutputStream outputStream = new FileOutputStream("test1.xlsx");
                workbook.write(outputStream);
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
  5.  Complete Program
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Iterator;
    import javax.swing.JFileChooser;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.util.WorkbookUtil;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    /**
     *
     * @author Shailesh Sonare
     */
    public class ApachePOIDemo {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] shailesh) throws IOException {
            
            /*
            
            Workbook workbook = new HSSFWorkbook();
            
            Sheet sheet1 = workbook.createSheet("movies");
    //        Sheet sheet2 = workbook.createSheet("Test Cases");
    //        Sheet sheet3 = workbook.createSheet(WorkbookUtil.createSafeSheetName("$*(^&?"));
            
            Row row = sheet1.createRow(0);
            
    //        Cell cell = row.createCell(4);
    //        cell.setCellValue("Hello World");
    //        Cell cell2 = row.createCell(3);
    //        cell2.setCellValue("terminator");
    //        System.out.println(cell.getRichStringCellValue().toString());
    //        System.out.println(cell2.getRichStringCellValue().toString());
    
            Cell cell1 = row.createCell(0);
            Cell cell2 = row.createCell(1);
            Cell cell3 = row.createCell(2);
            Cell cell4 = row.createCell(3);
            Cell cell5 = row.createCell(4);
            
            cell1.setCellValue(5);
            cell2.setCellValue("+");
            cell3.setCellValue(6);
            cell4.setCellValue("=");
            cell5.setCellFormula("A1+C1");
            
            try {
                FileOutputStream outputStream = new FileOutputStream("test1.xlsx");
                workbook.write(outputStream);
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            */
            /*
            JFileChooser fileChooser = new JFileChooser();
            int returnValue = fileChooser.showDialog(null, "Select File");
            
            if(returnValue == JFileChooser.APPROVE_OPTION) {
                Workbook workbook = new HSSFWorkbook(new FileInputStream(fileChooser.getSelectedFile()));
                
                Sheet sheet = workbook.getSheetAt(0);
                
                for(Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();){
                    for(Iterator<Cell> cit = rit.next().cellIterator(); cit.hasNext();){
                        System.out.println(cit.next());
                    }
                    System.out.println("");
                }
            } else {
                System.out.println("Invalid output");
            }
            */
            
            JFileChooser fileChooser = new JFileChooser();
            int returnValue = fileChooser.showDialog(null, "Select File");
            
            if(returnValue == JFileChooser.APPROVE_OPTION) {
                FileInputStream excelFile = new FileInputStream(fileChooser.getSelectedFile());
                Workbook workbook = new XSSFWorkbook(excelFile);
                
                Sheet sheet = workbook.getSheetAt(0);
                
                for(Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) {
                    for(Iterator<Cell> cit = rit.next().cellIterator(); cit.hasNext();) {
                        System.out.print(cit.next() + "\t" + cit.next());
                    }
                    System.out.println("");
                }
            }
            
        }    
    }
    

     

     

polymorphism

class A {
    public void test(){
        System.out.println("hello");
    }
    
    public void foo(){
        System.out.println("fooo");
    }
}

class B extends A {
    @Override
    public void test(){
        System.out.println("world");
    }
    public void bar(){
        System.out.println("baar");
    }
}


public class PolymorphismDemo {

    public static void main(String[] args) {
        
        A a = new A();
        a.test(); // allowed: will print hello
        a.foo(); // allowed: will print fooo
        //a.bar(); // not allowed: compile time error
        
        B b = new B();
        b.test(); // allowed: will print world
        b.foo(); // allowed: will print fooo
        b.bar(); // allowed: will print baar
        
        A c = new B();
        c.test(); // allowed: will print world
        c.foo(); // allowed: will print fooo
        //c.bar(); // not allowed: compile time error
        
        //B d = new A(); // not allowed: compile time error
        
        A e = new B();
        A f = (A)e;
        f.test(); // allowed: will print world
        f.foo(); // allowed: will print fooo
        //f.bar(); // not allowed: compile time error
        
        B g = new B();
        A h = (A)g;
        h.test(); // allowed: will print world
        h.foo(); // allowed: will print fooo
        //h.bar(); // not allowed: compile time error
    }    
}

 

programs on pointers

  1. WAP to exchange values of 2 variables by calling a function exchange
  2. WAP to find length of a string by calling a function lenght
  3. WAP to copy contents of one string into another by calling a function copy.
  4. WAP to reverse contents of a string
  5. WAP to find sum of 10 int values by calling a function sum (passing array as an argument)
  6. WAP to find mean of 10 in values by calling a function MEAN (passing array as an argument)
  7. WAP to dynamically read n values and find their sum
  8. WAP to read information about n students and display them
  9. WAP to find area and circumference of a circle by calling a single function circle
  10. WAP to call functions area and circumference of circle by using function pointer
  11. WAP to count total number of words in a string by calling a function wcount
  12. WAP to read a String and coutn total vowels in that string
  13. WAP to read a string and count total no of digits in that string
  14. WAP to read a string and copy it into another array
  15. WAP to rad a string and reverse that string

Hibernate Native SQL

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package corehibernatedemo;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import model.Person;
import model.Student;
import model.Users;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Shailesh Sonare
 */
public class CoreHibernateDemo {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws ParseException {
        // TODO code application logic here
        /*
        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml");
        
        StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());
        
        SessionFactory factory = cfg.buildSessionFactory(ssrb.build());
        
        Session session = factory.openSession();
        
        Transaction t = session.beginTransaction();
        
        List users = session.createQuery("from Users").list();
        System.out.println("done...");
        
        for (Iterator iterator = users.iterator(); iterator.hasNext();) {
            Users e = (Users) iterator.next();
            System.out.println(e.getFirstName());
        }
        
        
        
        Date bdate = new SimpleDateFormat("yyyy-MM-dd").parse("1992-11-15");
        
        Users  usr = new Users("Sunita", "Sonare", bdate);
        
        String hql = "UPDATE Users SET dob = :dob WHERE id = :id";
        
        Query query = session.createQuery(hql);
        query.setParameter("dob", bdate);
        query.setParameter("id", 4);
        query.executeUpdate();
        
        //session.save(usr);
        t.commit();
        session.close();
        System.out.println("DOne....");
        */
        
        //Student s = new Student();
        
        Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
        
        StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());
        
        SessionFactory factory = cfg.buildSessionFactory(ssrb.build());
        
        Session session = factory.openSession();
        
//        String hql = "FROM Users";
//        List<Users> list = session.createQuery(hql).list();

        String sql = "SELECT first_name, last_name, dob FROM users";
        
        SQLQuery query = session.createSQLQuery(sql);
        query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP); // important when you are using alias <key><value> pair
//        List<Object[]> rows = query.list();
//        
//        for(Object[] row : rows){
//            
//            Users u = new Users(row[0].toString(), row[1].toString(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(row[2].toString()));
//            System.out.println(u);
//            System.out.println("--------------");
//        }

        List list = query.list();
        
        for(Object obj : list){
            Map map = (Map)obj;
            System.out.println("" + map.get("first_name"));
            System.out.println("" + map.get("last_name"));
            System.out.println("" + map.get("dob"));
            System.out.println("-------------------------");
        }
    }
    
}

 

 

Users.java

package model;
// Generated 30 May, 2017 5:20:27 PM by Hibernate Tools 4.3.1


import java.util.Date;

/**
 * Users generated by hbm2java
 */
public class Users  implements java.io.Serializable {


     private Integer id;
     private String firstName;
     private String lastName;
     private Date dob;

    public Users() {
    }

    public Users(String firstName, String lastName, Date dob) {
       this.firstName = firstName;
       this.lastName = lastName;
       this.dob = dob;
    }
   
    public Integer getId() {
        return this.id;
    }
    
    public void setId(Integer id) {
        this.id = id;
    }
    public String getFirstName() {
        return this.firstName;
    }
    
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return this.lastName;
    }
    
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public Date getDob() {
        return this.dob;
    }
    
    public void setDob(Date dob) {
        this.dob = dob;
    }

    @Override
    public String toString() {
        return "Users{" + "id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", dob=" + dob + '}';
    }

}