Java

Java is a set of computer software and specifications developed by Sun Microsystems, which was later acquired by the Oracle Corporation, that provides a system for developing application software and deploying it in a cross-platform computing environment. Java is used in a wide variety of computing platforms from embedded devices and mobile phones to enterprise servers and supercomputers.

Spring Logo

Spring Framework

The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key element of Spring is infrastructural support at the application level: Spring focuses on the "plumbing" of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments.

Hibernate Logo

Hibernate Framework

Hibernate ORM is an object-relational mapping framework for the Java language. It provides a framework for mapping an object-oriented domain model to a relational database.

Friday, June 12, 2015

MySQL Commands

Login via MySql client:
$ mysql -u XXXXX -pXXXXX db_name
Create a new user:
CREATE USER 'user1'@'localhost' IDENTIFIED BY 'pass1';
GRANT ALL ON
*.* TO 'user1'@'localhost';
CREATE USER
'user1'@'%' IDENTIFIED BY 'pass1';
GRANT ALL ON
*.* TO 'user1'@'%';
List users:
select host, user, password, Create_priv from mysql.user;
List databases and tables:
show databases;
show tables
;
Rename column:
ALTER TABLE Article change article_id id bigint(20);
Make column non-null:
ALTER TABLE Person MODIFY firstName varchar(255) NOT NULL;
Make column unique:
ALTER TABLE Person ADD UNIQUE INDEX(memberId);
Calculate the database size:
SELECT table_schema "DB Name", sum(data_length + index_length) / 1024 / 1024  "DB size in MB" FROM information_schema.TABLES GROUP BY table_schema;

Sunday, May 17, 2015

What are all the different ways to create an object in Java?

There are four ways to create a Object in Java. They are

1. Using new keyword - This is the most common way to create an object in java.
Example:
MyObject object = new MyObject();

2. Using Class.forName() - If we know the name of the class and if it has a public default constructor we can create an object in this way.
Example:
MyObject object = (MyObject) Class.forName("com.ranga.MyObject").newInstance();

3. Using clone() - The clone() can be used to create a copy of an existing object.
Example:
MyObject anotherObject = new MyObject();
MyObject object = (MyObject) anotherObject.clone();

4. Using object deserialization - Object deserialization is nothing but creating an object from its serialized form.
Example:
ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();

Write a Program to Implement HashTable in Java Script

<!DOCTYPE html>
<html>
<body>
<script>

function HashTable() {
var hashTableItems = {};
this.setItem = function(key, value) {
hashTableItems[key] = value;
}
this.getItem = function(key) {
return hashTableItems[key];
}
}

var hashTable = new HashTable();
hashTable.setItem('name', 'Ranga Reddy');
console.log(hashTable.getItem('name'));

var hashTable1 = new HashTable();
hashTable1.setItem('age', '27');
console.log(hashTable1.getItem('age'));
</script>
</body>
</html>

Writing our own Collection mechanism to Sort different objects in Java.

SortCollections.java
---------------------------------------
package com.ranga.collections;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortCollections<T> {

private List<T> collectionList;
private String sortingField = null;
private boolean isAscending = true;

public SortCollections(List<T> objectList, String sortField) {
this(objectList, sortField, true);
}

public SortCollections(List<T> collectionList, String sortField, boolean isAscending) {
super();
this.collectionList = collectionList;
this.sortingField = sortField;
this.isAscending = isAscending;
}

@SuppressWarnings("rawtypes")
public List<T> sort( final Class claz) {
final String sortingField = this.sortingField;
final Boolean isAscending = this.isAscending;

Collections.sort(this.collectionList, new Comparator<T>() {
@Override
public int compare(T object1, T object2) {
try {
Field sortField = claz.getDeclaredField(sortingField);
sortField.setAccessible(true);

Object value1 = sortField.get(object1);
Object value2 = sortField.get(object2);

if (value1 instanceof String && value2 instanceof String) { // String field
String string1 = (String) value1;
String string2 = (String) value2;
if (true == isAscending) {
return string1.compareTo(string2);
} else {
return string2.compareTo(string1);
}
} else { // Numeric field
Number number1 = (Number) value1;
Number number2 = (Number) value2;
if (true == isAscending) {
if (number1.floatValue() > number2.floatValue()) {
return 1;
} else {
return -1;
}
} else {
if (number2.floatValue() > number1.floatValue()) {
return 1;
} else {
return -1;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
});
return this.collectionList;
}
}

Employee.java
---------------------------------------
    package com.ranga.collections;
import java.io.Serializable;
public class Employee implements Serializable {

private int id;
private String name;
private int age;
private float salary;

public Employee() {
super();
}

public Employee(int id, String name, int age, float salary) {
super();
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public float getSalary() {
return salary;
}

public void setSalary(float salary) {
this.salary = salary;
}

@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age
+ ", salary=" + salary + "]";
}
}

Application.java
---------------------------------------
package com.ranga.collections;
import java.util.ArrayList;
import java.util.List;
public class Application {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee(1,"Ranga", 26, 25677 ));
employees.add(new Employee(2,"Raja", 50, 2577 ));
employees.add(new Employee(3,"Vasu", 20, 30677 ));
employees.add(new Employee(4,"Mani", 45, 45677 ));
employees.add(new Employee(5,"Yasu", 29, 67677 ));
employees.add(new Employee(6,"Vinod", 80, 5677 ));

System.out.println(employees);
List<Employee> employeeList = new SortCollections<Employee>(employees, "name", false).sort(Employee.class);
System.out.println(employeeList);

employeeList = new SortCollections<Employee>(employees, "salary").sort(Employee.class);
System.out.println(employeeList);
}
}

Write a SQL Program to Swap the values in a single update query.

Given a Student table, Swap all Male to Female value and vice versa with a single update query.

Id Name Sex Salary
----------------------------
1 Ranga Male 2500
2 Vasu Female 1500
3 Raja Male 5500
4 Amma Female 500

Program:
------------------------
UPDATE Gender SET sex = CASE sex WHEN 'Male' THEN 'Female' ELSE 'Male' END