Tuesday, July 22, 2014

Get Table name and Primarykey using java Program

package com.sampleproject;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class GetallTableNames {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("now we are going to connect with a database ");

 try {

  Class.forName("com.mysql.jdbc.Driver");

  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
  DatabaseMetaData md = con.getMetaData();
  ResultSet rs = md.getTables(null, null, "%", null);
  String   catalog   = null;
  String   schema    = null;
  String   tableName = null ;


/*while(result.next()){
   String columnName = result.getString(4);
}*/
while (rs.next()) {
 System.out.println(rs.getString(3));
 tableName = rs.getString(3);
 String columnName = null;
 ResultSet result = md.getPrimaryKeys(
   catalog, schema, tableName);
 while(result.next()){
 columnName  = result.getString(4);
 System.out.println("primary key :" +columnName);

}

}
}
 catch (Exception e) {

  System.out.println(e.getMessage());
 }
}
}

Monday, May 6, 2013

Java Versions SE7

Java 7 (Code Name Dolphine ) is a major update to Java that was launched on July 7, 2011 and was made available for developers on July 28 2011.

New Features Java 7:
1. Underscores in Numeric Literals
2. String in switch Statement
3. The try-with-resources Statement
4. Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking
5. Binary Literals
6. Type Interface for Generic Instance Creation
7. Improved Compiler Warnings and Errors When Using Non-Reifiable Formal Parameters with Varargs      Methods

1. Underscores in Numeric Literals : Any number of underscore character(_) can appear anywhere between digits in a numerical literal.
Example : To separate groups of digits in numeric literals, which can improve the readability of your code.
              long creditCardNumber = 1234_5678_9012_3456L;
              float  pi  = 3.14_15F;
You can place underscore only between digits; you cannot place underscore in the following places;

    1. At the beginning or end of a number
  Example: int x1 = _ 52;   int x2 = 52_   // Invalid
                int x3 = 5________6 // ok (decimal literal)

    2. Adjacent to a decimal point in a floating point literal
  Example: float pi = 3_.1415F ; float pi1 = 3._1415F;  // Invalid

    3. Prior to an F or L suffix
  Example: long socialSecurityNumber = 999_99_9999_L;  // Invalid

    4. In positions where a string of digits is expected

2. String in switch Statement

The switch statement compares the String object in its expression with the expressions associated with each case label as if it were using the String.equals method;
The comparison of String objects in switch statements is case sensitive


Ex: 

public class Example

public static void main(String[] args) {

        String dayOfWeekArg = "Thursday" ;

        switch (dayOfWeekArg){

               case "Monday":

                      System.out.println("Monday");

                      break;

              case "Tuesday":

                      System.out.println("Tuesday");

                      break;

               case  "Wednesday":

                      System.out.println( "Wednesday"");

                      break;

               case "Thursday":

                      System.out.println("Thursday");

                      break;

               case "Friday":

                      System.out.println("Friday");

                      break;
               case "Saturday":
System.out.println("Saturday");

                      break;
               case "Sunday":
  System.out.println("Sunday");

                      break;
               default:

                      System.out.println("Invalid day of the week");

                      break;

        }

    }

}



Wednesday, April 17, 2013

Factorial Example in Java


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class FactorialEx {


public static void main(String[] args) throws NumberFormatException,
IOException {

InputStreamReader input = new InputStreamReader(System.in);
BufferedReader bu = new BufferedReader(input);
System.out.println("Enter Integer value:");
int n = Integer.parseInt(bu.readLine());
int fact = 1;
for (int i = n; i >= 1; i--) {
fact = fact * i;
}
System.out.println("Factorial of " + n + " is : " + fact);
}

}

/*
Output:
Enter Integer value:
5
Factorial of 5 is : 120

*/

Tuesday, April 16, 2013

To find the first repeated character in a string

Example: 
java
The repeated character in java is : a



public class FirstRepeatedCharacter {

/**
* @param args
*/
public static void main(String[] args) {


String str ="welcome";

for(int i=0;i<=str.length()-1;i++){
int count = 0;
for(int j=i+1;j<str.length();j++){

if( str.charAt(i) == str.charAt(j)){
count= count + 1;
break;
}
}
if(count != 0){
System.out.println("The first repeated character in a welcome is : " + str.charAt(i));
break;
}
}
}

}

/*
Output:
The first repeated character in a welcome  is: e
*/

Monday, April 15, 2013

Find the first non repeated character in a string



To find the first non repeated character in a string
Example:  madam
The non repeated character in madam is : d



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class NonRepeatCharacter {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub


InputStreamReader input = new InputStreamReader(System.in);
BufferedReader br  = new BufferedReader(input);

System.out.println("Enter String value :");
String str = br.readLine();

for(int i=0;i<str.length();i++){
int count = 0;
for(int j=0;j<str.length();j++){

if(i!=j && str.charAt(i) == str.charAt(j)){
count = count +1 ;
break;
}
}
if(count == 0){
System.out.println("The first non repeated character is : " +str.charAt(i));
break;
}
}

}

}

/*
Output:

Enter String value :
character
The first non repeated character is : h

*/

Sunday, April 14, 2013

Floyd's Triangle Examples

<data:blog.pageName/> | <data:blog.title/> <data:blog.pageTitle/>
/*
1. Example:
1
01
101
0101

*/


public class FloydsTriangle{

/**
* @param args
*/
public static void main(String[] args) {

for(int i=1;i<5;i++){
for(int j=1;j<=i;j++){


if( i == j){
System.out.print("1");
} else if( (i+j)%2 == 0){
System.out.print("1");
}else{
System.out.print("0");
}
}
System.out.println(" ");

}

}

}

/*
Output:

1
01
101
0101
*/

---------------------------------------------------------------------------------------------------
2.Example :


1
22
333
4444
55555
55555
4444
333
22
1


public class FloydsTriangle2{

/**
 * @param args
 */
public static void main(String[] args) {

for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print(i);
}
System.out.println("");
}

for(int i=5;i>0;i--){
for(int j=1;j<=i;j++){
System.out.print(i);
}
System.out.println("");
}

}
}

---------------------------------------------------------------------------------------------------------

3.Example


22 
333 
4444 
55555 

public class FloydsTriangle2{

/**
 * @param args
 */
public static void main(String[] args) {

for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print(i);
}
System.out.println(" ");
}
}
}

--------------------------------------------------------------------------------------
4. Example


1
12
123
1234
12345


for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print(j);
}
System.out.println(" ");
}

----------------------------------------------------------------------------------------------