Monday, February 13, 2017

Jdbc Program to retrieve all data from 'emp' table of oracle database type-4 driver.

import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Jdbc10 { public static void main(String[] args) { // Establishing the connection try(Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:xe","system","manager")) { // Creating statement to...

Jdbc Program to update column data of 'emp' table .

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class Jdbc9 { public static void main(String[] args) { // Establishing the connection try(Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:xe","system","manager")) { // Creating statement to execute query Statement...

Jdbc Program to insert data in 'emp' table in Oracle database using type-4 driver.

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class Jdbc8 { public static void main(String[] args) { try { // Loading & Register the Type-4 Driver Class(OracleDriver). But this line is optional ...

Jdbc Program to drop 'emp' table by using type-4 driver of oracle database .

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class Jdbc7 { public static void main(String[] args) { try { Class.forName("oracle.jdbc.OracleDriver"); } catch(ClassNotFoundException cn) { cn.printStackTrace(); } // Establishing the connection try(Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:xe","system","manager")) ...

Jdbc Program to create table 'emp' in oracle database by using type-4 driver.

To use type-4 driver of oracle database we have to set classpath(if we are using normal  editor like editplus,notepad++) and we have to import that jar file if we are using IDE(like Eclipse, NetBeans) of that jar file(ojdbc6.jar) provided by database vendors(Oracle Corporation) which is available in  C:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib\ojdbc6.jar. How...

Jdbc Program to retrieve all data from 'emp' table of oracle database type-1 driver.

import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Jdbc5 { public static void main(String[] args)throws ClassNotFoundException { // Loading & Register the Type-1 Driver Class(JdbcOdbcDriver). But this line is optional Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); ...

Jdbc Program to update column data on 'emp' table.

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class Jdbc4 { public static void main(String[] args)throws ClassNotFoundException { // Loading & Register the Type-1 Driver Class(JdbcOdbcDriver). But this line is optional Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Establishing the connection ...

Jdbc Program to insert data in 'emp' table in Oracle database using type-1 driver.

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class Jdbc3 { public static void main(String[] args)throws ClassNotFoundException { // Loading & Register the Type-1 Driver Class(JdbcOdbcDriver). But this...

Jdbc Program to drop the 'emp' table.

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class Jdbc2 { public static void main(String[] args) throws ClassNotFoundException { // Loading & Register the Type-1 Driver Class(JdbcOdbcDriver). But this line is optional Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Establishing the...

Jdbc Program to create a table 'emp' in Oracle database.

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class Jdbc1 { public static void main(String[] args)throws ClassNotFoundException { // Loading & Register the Type-1 Driver Class(JdbcOdbcDriver). But this line is optional Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Establishing the...

Sunday, February 12, 2017

Java Program to sort List(LinkedList) Elements in Descending order(customized sorting order).

import java.util.LinkedList; import java.util.Collections; import java.util.Comparator; import static java.lang.System.out; class SortList2 { public static void main(String...alt) { LinkedList ll=new LinkedList(); ll.add("ravi"); ll.add("altaf"); ll.add("kanta"); ll.add("zahid"); ll.add("nani"); out.println("Before Sorting : "+ll); Collections.sort(ll,new MyComparator()); ...

Java Program to sort List(ArrayList) Elements in ascending order(default sorting order).

import java.util.ArrayList; import java.util.Collections; import static java.lang.System.out; class SortList { public static void main(String...alt) { ArrayList al=new ArrayList(); al.add("z"); al.add("a"); al.add("k"); al.add("z"); al.add("n"); out.println("Before Sorting : "+al); Collections.sort(al); out.println("After Sorting : "+al); }...

Java program to sort in descending order of user defined Object(comapre by name).

import java.util.*; class Emp implements Comparable { int id; String name; float sal; Emp(int id,String name,float sal) { this.id=id; this.name=name; this.sal=sal; } public String toString() { return "["+this.id+","+this.name+","+this.sal+"]"; } public int compareTo(Object obj) { Emp e=(Emp)obj; int val=this.name.compareTo(e.name); return -val; } } class...

Java program to sort in ascending order of user defined Object(comapre by name).

import java.util.*; class Emp implements Comparable { int id; String name; float sal; Emp(int id,String name,float sal) { this.id=id; this.name=name; this.sal=sal; } public String toString() { return "["+this.id+","+this.name+","+this.sal+"]"; } public int compareTo(Object obj) { Emp e=(Emp)obj; int val=this.name.compareTo(e.name); return val; } } class...

Java Program to sort Object/Double Array Elements in descending order with customized sorting order in a particular range .

import static java.lang.System.out; import java.util.Arrays; import java.util.Comparator; class SortArray6 { static public void main(String...alt) { Double[] a={10.6,15.3,10.5,2.3,6.4,1.7,9.78,2.34}; out.println("Before Sorting : "); for(double x:a) out.print(x+"   "); Arrays.sort(a,1,5,new MyComparator()); out.println("\n\nAfter Sorting : "); for(double...

Java Program to sort Object/Integer Array Elements in descending order with customized sorting order.

import static java.lang.System.out; import java.util.Arrays; import java.util.Comparator; class SortArray5 { static public void main(String...alt) { Integer[] a={12,3,5,48,1,6,47,9,2}; //Autoboxing[Integer.valueOf(int a)] out.println("Before Sorting : "); for(int x:a) //Auto Unboxing[Integer.intValue()] out.print(x+" "); Arrays.sort(a,new MyComparator()); out.println("\n\nAfter...

Java Program to sort primitive Array Elements in Ascending order in a particular range.

Ex:- we have 9 array elements index will be(0-8) like : 12,3,5,48,1,6,47,9,2 and if we want to sort in the range like 2-7 then sorted elements will be 12,3,1,5,6,47,48,9,2 import static java.lang.System.out; import java.util.Arrays; class SortArray4 { static public void main(String...alt) { int[] a={12,3,5,48,1,6,47,9,2}; out.println("Before Sorting : "); for(int x:a) ...

Java Program to sort Object/String class elements with customized sorting order.

This program will sort in descending order. import static java.lang.System.out; import java.util.Arrays; import java.util.Comparator; class SortArray3 { static public void main(String...alt) { String[] s={"zoo","apple","cat","banana","lock"}; out.println("Before Sorting : "); for(String x:s) out.print(x+" "); out.println("\n\nAfter Sorting : "); Arrays.sort(s,new...

Java Program to sort Object/String class elements with default natural sorting order.

import static java.lang.System.out; import java.util.Arrays; class SortArray2 { static public void main(String...alt) { String[] s={"zoo","apple","cat","banana","lock"}; out.println("Before Sorting : "); for(String x:s) out.print(x+" "); out.println("\n\nAfter Sorting : "); Arrays.sort(s); // public static void sort(java.lang.Object[]) for(String x:s) out.print(x+"...

Java Program to sort primitive Array Elements with default natural sorting order.

import static java.lang.System.out; import java.util.Arrays; class SortArray { static public void main(String...alt) { int[] a={12,3,5,48,1,6,47}; out.println("Before Sorting : "); for(int x:a) out.print(x+" "); Arrays.sort(a); out.println("\n\nAfter Sorting : "); for(int x:a) out.print(x+" "); }...

Java Program to push StringBuffer object elements in Stack(List) and search an element.

import java.util.Stack; import java.util.Collections; import java.util.Comparator; import static java.lang.System.out; class SearchList { public static void main(String...alt) { Stack v=new Stack(); v.push(new StringBuffer("ravi")); v.push(new StringBuffer("altaf")); v.push(new StringBuffer("kanta")); v.push(new StringBuffer("zahid")); v.push(new StringBuffer("nani")); ...

Java Program to search an element in List(Vector).

import java.util.Vector; import java.util.Collections; import static java.lang.System.out; class SearchList { public static void main(String...alt) { Vector v=new Vector(); v.add("ravi"); v.add("altaf"); v.add("kanta"); v.add("zahid"); v.add("nani"); out.println("Before Sorting : "+v); Collections.sort(v); out.println("After Sorting : "+v); int result1=Collections.binarySearch(v,"nani"); ...

Java Program to search an element in an Object/StringBuffer Array within a range.

import static java.lang.System.out; import java.util.Arrays; import java.util.Comparator; class Test { static public void main(String...alt) { StringBuffer[] sb={new StringBuffer("Anil"),new StringBuffer("Ravi"),new StringBuffer("Ali"),new StringBuffer("Ram"),new StringBuffer("Mohan"),new StringBuffer("Anthony")}; out.println("Before Sorting : "); for(StringBuffer x:sb) ...

Java Program to search an element in an Object/StringBuffer Array.

import static java.lang.System.out; import java.util.Arrays; import java.util.Comparator; class SearchArray5 { static public void main(String...alt) { StringBuffer[] sb={new StringBuffer("Anil"),new StringBuffer("Ravi"),new StringBuffer("Ali"),new StringBuffer("Ram"),new StringBuffer("Mohan"),new StringBuffer("Anthony")}; out.println("Before Sorting : "); for(StringBuffer...

Java Program to search an element in a primitive Array within a range.

import static java.lang.System.out; import java.util.Arrays; class SearchArray4 { static public void main(String...alt) { int[] f={5,12,36,14,20,1,3,30}; out.println("Before Sorting : "); for(int x:f) out.print(x+"  "); Arrays.sort(f); // Default natural sorting(Ascending Order) out.println("\nAfter Sorting : "); for(int x:f) out.print(x+"  ");...

Java Program to search an element in a primitive Array.

Note:- To search an element in an array elements must be sorted before search otherwise we will get unpredictable result. because java.util.Arrays uses binary search mechanism. If element is found then it will return its index value and if element is not found then it will return insertion point(Insertion point is a place where we can place target element in sorted list. Note:-...

Java Program to search an element in an String/Object Array.

Note:- To search an element in an array elements must be sorted before search otherwise we will get unpredictable result. because java.util.Arrays uses binary search mechanism. If element is found then it will return its index value and if element is not found then it will return insertion point(Insertion point is a place where we can place target element in sorted list. Note:-...

Java Program to search an element in a Double/Object Array.

Note:- To search an element in an array elements must be sorted before search otherwise we will get unpredictable result. because java.util.Arrays uses binary search mechanism. If element is found then it will return its index value and if element is not found then it will return insertion point(Insertion point is a place where we can place target element in sorted list. Note:-...

WAP to enter an string and reverse it.

import java.io.Console; class ReverseString3 { public static void main(String[] args) { Console c=System.console(); String s=c.readLine("Enter String to reverse : "); char[] rev=s.toCharArray(); System.out.println("Original String = "+s); System.out.print("Reversed String = "); for(int i=rev.length-1;i>=0;i--) System.out.print(rev[i]); } } ...

Java program to enter an string and reverse it

import java.io.Console; class ReverseString2 { public static void main(String[] args) { Console c=System.console(); String s=c.readLine("Enter String to reverse : "); // StringBuffer sb=new StringBuffer(s); StringBuilder sb=new StringBuilder(s); System.out.println("Original String = "+sb); System.out.println("Reversed String = "+sb.reverse()); } } ...

Java program to enter an string and reverse it.

import java.io.Console; class ReverseString { public static void main(String[] args) { Console c=System.console(); String s=c.readLine("Enter String to reverse : "); String rev=""; for(int i=s.length()-1;i>=0;i--) rev=rev+s.charAt(i); System.out.println("Original String = "+s); System.out.println("Reversed String = "+rev); } } ...

Java Program to insert element in List and print in reverse order.

import java.util.ArrayList; import java.util.Collections; import static java.lang.System.out; class ReverseList { public static void main(String...alt) { ArrayList al=new ArrayList(); al.add(20); al.add(1); al.add(12); al.add(35); al.add(16); al.add(2); out.println("Before Reverse : "+al); Collections.reverse(al); out.println("After Reverse : "+al); }...

Write a Program in Java to input a number and check whether it is a Unique Number or not.

Note: A Unique number is a positive integer (without leading zeros) with no duplicate digits. For example 7, 135, 214 are all unique numbers whereas 33, 3121, 300 are not. import static java.lang.System.out; import java.io.BufferedReader; import java.io.InputStreamReader; class Unique { public static void main(String...alt)throws Exception { int flag=0; BufferedReader...

Saturday, February 11, 2017

Java Program to insert an image in Oracle database

import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class StoreImage { public static void main(String[] args) { try(Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:xe","system","manager")) ...

Java Program to print & count all prime number between 1 to 100.

Note: A Prime number is a natural number which is greater than 1 and that has no posotive divisor other than 1 & itself. Ex :- 2,3,5,7,11,13,17,19,23...... */ import static java.lang.System.out; import java.util.Scanner; class Prime2 { static public void main(String...alt) { int count=0; for(int j=2;j<=100;j++) { int flag=0; for(int i=2;i<=j/2;i++) ...

Java Program to enter a number & check whether it is prime no or not.

Note: A Prime number is a natural number which is greater than 1 and that has no positive divisor other than 1 & itself. Ex :- 2,3,5,7,11,13,17,19,23...... */ import static java.lang.System.out; import java.util.Scanner; class Prime { static public void main(String...alt) { Scanner sc=new Scanner(System.in); out.print("Enter a Number : "); int n=sc.nextInt(); int...

Write a Java Program to display all Harshad /Niven Number between 1 to 100.

Harshad Number : In recreational mathematics, a Harshad number (or Niven number), is an integer (in base 10) that is divisible by the sum of its digits. Ex1: The number 18 is a Harshad number in base 10, because the sum of the digits 1 and 8 is 9 (1 + 8 = 9) and 18 is divisible by 9 (since 18 % 9 = 0). hence 18 is harshad no. Ex2:- The number 1729 is a Harshad number in base...

Write a Java Program to all Keith Number between 1 to 1000.

Note:A Keith Number is an integer N with ‘d’ digits with the following property: If a Fibonacci-like sequence (in which each term in the sequence is the sum of the ‘d’ previous terms) is formed, with the first ‘d’ terms being the decimal digits of the number N, then N itself occurs as a term in the sequence. For example, 197 is a Keith number since it generates the sequence 1,...

Write a Program in Java to input a number and check whether it is a Keith Number or not.

Note:A Keith Number is an integer N with ‘d’ digits with the following property: If a Fibonacci-like sequence (in which each term in the sequence is the sum of the ‘d’ previous terms) is formed, with the first ‘d’ terms being the decimal digits of the number N, then N itself occurs as a term in the sequence. For example, 197 is a Keith number since it generates the sequence 1,...

Write a Program in Java to show all Kaprekar number between 1 to 1000.

Note: A positive number ‘n’ is squared that has ‘d’ number of digits and split into two pieces. right-hand piece that has ‘r’ digits and a left-hand piece that has remaining  ‘d-r=l’ digits. If the sum of the two pieces(r+l) is equal to the number, then ‘n’ is a Kaprekar number. The first few Kaprekar numbers are: 9, 45, 297 ...... Example 1: 9 Square of 9 = 9*9  = 81,...

Write a Program in Java to input a number and check whether it is a Kaprekar number or not.

Note: A positive number ‘n’ is squared that has ‘d’ number of digits and split into two pieces. right-hand piece that has ‘r’ digits and a left-hand piece that has remaining  ‘d-r=l’ digits. If the sum of the two pieces(r+l) is equal to the number, then ‘n’ is a Kaprekar number. The first few Kaprekar numbers are: 9, 45, 297 ...... Example 1: 9 Square of 9 = 9*9  = 81,...

Friday, February 10, 2017

Write a java program to all valid IMEI no(15 digit) between 999999999990000 to 999999999999999.

import java.util.Scanner; import static java.lang.System.out; class Imei3 { public static int sumDigit(int d) { int s=0; while(d!=0) { s=s+(d%10); d/=10; } return s; } static public void main(String...alt) { long a=999999999990000l,b=999999999999999l; for(long j=a;j<=b;j++) { long n=j; String s=Long.toString(n); int l=s.length(),sum=0;...