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.

123

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 are1. 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 =...

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',...

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,...

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' ...

Write a SQL Program to Select every Nth record.

CREATE TABLE student(id int, name varchar(30), age int, gender char(6));INSERT INTO student VALUES (1 ,'Ranga', 27, 'Male'), (2 ,'Reddy', 26, 'Male'), (3 ,'Vasu', 50, 'Female'), (4 ,'Ranga', 27, 'Male'), (5 ,'Raja', 10, 'Male'), (6 ,'Pavi', 52, 'Female'), (7 ,'Vinod', 27, 'Male'), (8 ,'Vasu', 50, 'Female'), (9 ,'Ranga', 27, 'Male'), (10 ,null, 27, 'Male');Program:-----------------------------------------------SELECT * FROM ( SELECT @row := @row +1 AS Rownum, name as Name FROM (SELECT @row :=0) r, student) studentsWHERE...

Write a SQL Program to get the Sum value of same column with different conditions.

CREATE TABLE student(id int, name varchar(30), age int, gender char(6));INSERT INTO student VALUES (1 ,'Ranga', 27, 'Male'), (2 ,'Reddy', 26, 'Male'), (3 ,'Vasu', 50, 'Female'), (5 ,'Raja', 10, 'Male'), (6 ,'Pavi', 52, 'Female'), (7 ,'Vinod', 27, 'Male');Query:-----------------------------------SELECT SUM(CASE WHEN s.gender = 'Male' THEN 1 ELSE 0 END) AS MaleCount, SUM(CASE WHEN s.gender = 'Female' THEN 1 ELSE 0 END) AS FemaleCount FROM student s;Output:------------------------------MaleCount FemaleCount4...

Write a SQL Program to Get the Next and Previous values based on Current value?

CREATE TABLE student (id int, name varchar(30), age int, gender char(6));INSERT INTO student VALUES (1 ,'Ranga', 27, 'Male'), (2 ,'Reddy', 26, 'Male'), (3 ,'Vasu', 50, 'Female'), (4 ,'Ranga', 27, 'Male'), (5 ,'Raja', 10, 'Male'), (6 ,'Pavi', 52, 'Female'), (7 ,'Vinod', 27, 'Male'), (8 ,'Vasu', 50, 'Female'), (9 ,'Ranga', 27, 'Male');Query:----------------------------------- SELECT name as Name, (SELECT name FROM student s1 WHERE s1.id < s.id ORDER BY id DESC LIMIT 1) as Previous_Name,...

How to get the Duplicate and Unique Records by using SQL Query?

CREATE TABLE student (id int, name varchar(30), age int, gender char(6));INSERT INTO student VALUES (1 ,'Ranga', 27, 'Male'), (2 ,'Reddy', 26, 'Male'), (3 ,'Vasu', 50, 'Female'), (4 ,'Ranga', 27, 'Male'), (5 ,'Raja', 10, 'Male'), (6 ,'Pavi', 52, 'Female'), (7 ,'Vinod', 27, 'Male'), (8 ,'Vasu', 50, 'Female'), (9 ,'Ranga', 27, 'Male');Getting the duplicate records:---------------------------------------SELECT DISTINCT name AS Name, COUNT(name) as Count FROM student GROUP BY name HAVING COUNT(name) > 1;Output:---------------------------------------Name...

How to delete the Child records while updating Parent Record.

If we use javax.persistence.CascadeType.All for deleting child records it will delete only collection of strings. Other than collection of strings in order to delete we need to useorg.hibernate.annotations.CascadeType.DELETE_ORPHAN to delete the child records.For Example,public class Employee { @OneToMany(cascade = {CascadeType.ALL}) @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN) List<Department> department...

What is the output of the following SQL Program. SELECT CASE WHEN null = null THEN 'I LOVE YOU RANGA' ELSE 'I HATE YOU RANGA' end as Message;

SELECT CASE WHEN null = null THEN 'I LOVE YOU RANGA' ELSE 'I HATE YOU RANGA' end as Message;Output: 'I HATE YOU RANGA'The reason for this is that the proper way to compare a value to null in SQL is with the is operator, not with =.SELECT CASE WHEN null IS null THEN 'I LOVE YOU RANGA' ELSE 'I HATE YOU RANGA' end as Message;Output: 'I LOVE YOU RAN...

Write a SQL Program to generate the following output?

Input:Employee:Department:Output:Creating tables and inserting Data:CREATE TABLE Employee ( e_id INT NOT NULL AUTO_INCREMENT, e_name VARCHAR(100) NOT NULL, age tinyint NOT NULL, PRIMARY KEY (e_id));CREATE TABLE Department ( d_id INT NOT NULL AUTO_INCREMENT, d_name VARCHAR(100) NOT NULL, e_id INT NOT NULL, PRIMARY KEY (d_id), FOREIGN KEY (e_id) REFERENCES Employee(e_id) ON DELETE CASCADE);INSERT INTO Employee VALUES...

Sunday, May 3, 2015

Hibernate4 One-to-One Relationship Example Using Annotations

In this article we are going to create a project to implement step by step one-to-one association example using Annotations with Maven project.OneToOne Relationship:A one-to-one relationship occurs when one entity is related to exactly one occurrence in another entity. For example, we have a Employee and Address tables, Employee has its single Address and each Address belongs to unique Employee. We can associate entities through a one-to-one...