Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday, 28 February 2018

Java Program to Check Leap Year or not

Leap Year Program in Java

Leap year program in Java

Now, Here we going to write a java program to check year is leap year or not. This leap year java program is the most important program because it is mostly asked in java programming interviews

Let's write a java program to check whether the entered year is leap year or not.


What is Leap Year?

Normally every year comes with 365 days but when any year comes with 366 days is known as leap year.

Let's understand with an example where we will take year as an input from the user by using Scanner class and then calculate the year is leap year or not.


Java Leap Year Program Example

This is simple java program to find leap year or not with line-by-line.

import java.util.Scanner;
class LeapYearExample
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the year");
int year = sc.nextInt();

boolean b = false;
if(year % 400 == 0)

{

b = true;

}

else if(year % 100 == 0)

{

b = false;

}

else if(year % 4 == 0)

{

b = true;

}

else

{

b = false;

}

if(b)
{
System.out.println(" year "+year+"  is a leap year ");
}
else
{
System.out.println(" year " +year+"  is not a leap year ");
}
}
}

Output : Enter the year
              2000
              year 2000 is a leap year

Note: If a year is divisible by 400 and 4, year is leap year. If year is divisible by 100, not a leap year.

Check some other basic java programs.

Learn how to convert decimal to binary in java and how to find area of triangle in java.

Here we learned how to calculate leap year in java through program.

Share:

Friday, 23 February 2018

Write a Java Program for Linear Search

Linear Search Program in Java

Linear Search Program in Java

Here, We will see how to write a java program for linear search algorithm step-by-step. Linear search algorithm is the most important topic and it is mostly asked in core java interviews.

Linear search is a very simple searching algorithm and it is also known as sequential search algorithm in java. By using linear search algorithm we can easily find particular elements or value in the list.

Linear search is used to search a target element from multiple elements and it is used to check if an element exists in the given list we compare it with every element in the list. If it is found then we print the location of an element at which it occurs.

Linear search algorithm is slower than binary search algorithm in java.

Let's understand, Java Linear search algorithm with a simple example.

Check other Java Programs for Practice.


Java Program for Linear Search

In this linear search program, We will take elements from the user and then we will search particular elements from multiple elements. Also take number of elements.

import java.util.Scanner;
class LinearSearchExample
{
public static void main(String args[])
{
int c, n, search, array[];

Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements");
n = sc.nextInt();
array = new int[n];

System.out.println("Enter " + n + " Integers");

for(c = 0; c < n; c++)
array[c] = sc.nextInt();

System.out.println("Enter value to find");
search = sc.nextInt();

for(c = 0; c < n; c++)
{
if(array[c] == search)
{
sop(search+ " is present at location " + (c + 1)+ " . ");

break;

}
}
if(c == n)
System.out.println(search+ "is not present in array");
}
}

Output: Enter number of elements
             5
             Enter 5 Integers
             47
             56
             30
             15
             89
             Enter value to find
             30
             30 is present at location 3


Java Linear Search Example 2

This is another java linear search example to find target value from the list.

package javatutorial95;
public class Demo2
{
public static int linearSearch(int[] arr, int key)
{
int size = arr.length;

for(int i = 0; i < size; i++)
{
if(arr[i] == key)
{
return i;
}
}
return -1;
}

public static void main(String args[])
{
int arr1[] = {50, 87, 66, 23};
int searchKey = 66;

System.out.println("key " + searchKey + " present at index " +linearSearch(arr1, searchKey));
}
}

Output: key 66 present at index 2

Read More:

Binary Search Program in Java.
Java Program to Check Leap Year or Not.
Java Program to Find Area of Square.
Some Java Basic & Important Programs.
Java String Conversion Examples

Here we saw linear search program in java by 2 simple examples.  
Share:

Friday, 2 February 2018

Java ResultSet Interface in JDBC with Example

What is ResultSet in JDBC?

Java ResultSet Interface in jdbc with example

Java ResultSet is an interface in jdbc which extends Wrapper and AutoCloseable interfaces.

ResultSet is used to retrieve SQL select query result.

A ResultSet object maintains a cursor pointing to its current row of data in the table. Initially, the cursor positioned before the first row. 

ResultSet is not updatable and by default, the object of Resultset can be moved only forward direction.

But we can move the object of ResultSet interface in both direction i.e forward and backward direction by using TYPE_SCROLL_INSENSITIVE and TYPE_SCROLL_SENSITIVE in createStatement method and make the object updatable by using below statement.

Statement stm = con.createStatement(

ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);  


Method of ResultSet Interface in Java

There are some methds of java jdbc ResultSet interface which is mostly used in jdbc programs.

1) public boolean next()

It is used to move the cursor to the one row next from the current position.

2) public boolean previous()

It is used to move the cursor to the one row previous from the current position.

3) public boolean first()

It is used to move the cursor to the first row in result set object.

4) public boolean last()

It is used to move the cursor to the last row in result set object.

5) public boolean absolute(int row)

It is used to move the cursor to the specified row number in the result set object.

6) public boolean relative(int row)

It is used to move the cursor to the relative row number in the result set object and it may be positive and negative.

7) public int getInt(int columnIndex)

It is used to return the data of specified column index of the current row as an int.

8) public int getInt(String columnName)

It is used to return the data of specified column name of the current row as an int.

9) public String getString(int columnIndex)

It is used to return the data of specified column index of the current row as String.

10) public String getString(String columnName)

It is used to return the data of specified column name of the current row as String.

Let's simple example of java jdbc ResultSet interface.

Suppose there is a student table in the oracle database with the rollno and name column.

You can read this post : Oracle Database Connection in Java and Jdbc Statement interface.


Java ResultSet Example

In this ResultSet example, We will fetch all the records from the student table by using select query.

import java.sql.*;
class ResultSetExample
{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","bca");

Statement stm = con.createStatement();
ResultSet rs = stm.executeQuery("select * from student");

while(rs.next());
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
}

catch(Exception e)
{
System.out.println(e);
}
}
}

Read More:

How to Connect Java Program with MySql Database.
Steps to Connect to Database in Java.
Java Interface Concept with Examples.
Some Basic Java Programs for Beginners.
Star Pattern Programs in Java.
Alphabet Pattern Programs in Java.

The above java jdbc ResultSet example will fetch all the records from the student table.

Share:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate