Skip to main content

Posts

Showing posts from 2014

Write a program to separate the 0's and 1's in either sides of an array.

An array is filled with only 0's and 1's randomly. Write a program so that all the 0's are on one side and all the 1's are on other side of the array. The sample program is as follows: package com.sample; /*  Write a program to separate 0's and 1's from an array where all the numbers are only 1 or 0.  */ public class ArrayTest { public static void main(String[] args) { int[] ar = { 0, 0, 1, 0, 0, 1, 1, 0, 0, 1 }; System.out.println("The given array before ->"); for (int i = 0; i < ar.length; i++) { System.out.print(ar[i] + " "); } sort(ar); System.out.println("\nThe given array after arrangement->"); for (int i = 0; i < ar.length; i++) { System.out.print(ar[i] + " "); } } private static void sort(int[] ar) { int i = 0; int j = ar.length - 1; int temp; while (i < j) { if (ar[i] == 1) { i++; } if (ar[j] == 0) { j--; } ...

Hibernate Annotation Example with Java Collection Mapping

Hi Guys, Many a times we have data required to be mapped in the collection framework. Like suppose if we talk about a simple example of student table where a student is having having fields such as students(sid, same, dateofbirth, qualification). Now if we want to have some more information to be stored about student such as the number of courses he/she has enrolled, the marks obtained in the different subjects, the reference email IDs so on and so forth, then, these information can't be stored in the single table and corresponding single fields can't also be able to handle all these information. Given the complexity of the situation we are going to separate these information in different tables such as courses, emails, marks, phones and references. The schema of all these tables would probably be as follows: students(sid, sname, dob, qualification); courses(sid, cname, idx); emails(sid, emailid, idx); marks(sid, marks); phones(sid, phoneno); refs(sid, rname,...

Struts2 and Hibernate Example using Annotation

Hi Guys, Today we are going to create an example using Struts2 and Hibernate using Annotation. For the database, we are going to use MySQL. This example will register a record for a user in the mysql database, which later will be used to login to the application. First of all we need to create our database table in MySQL. Log into your mysql database and type the following command to create a database in the mysql prompt. create database mydb; Use the above created database to create table. use mydb; Now we need to create a database table named "users". create table users( uid int primary key auto_increment, uname char(15), password char(20), email char(20), phone long, city char(15)); Now to create this application, we are going to use eclipse. Open your eclipse and create a dynamic web project. Put the following jars related to Struts2 and Hibernate in the WEB-INF/lib folder Now create the following packages in the src folder for the ...