How to create autoincrement id in oracle with full example



Here i am telling you How to create autoincrement id in oracle with full example
  • First of all you need to open your oracle database and login with your given username and password.
  •  And then click on Object Browser and create a table (name which you want) here i use s2ptech.
  • Here in s2ptech table i use two field id and name.
  • Click on next button and then select not populated option in radio button.
  • Select primary key (id which you want to autoincrement) and click next->next->finish->create.
  • Now your table is created.
  • Now click on Sequence and fill the sequence name(seq_s2ptech).

  • And click on next, click on create and now your sequence is created
  • Now click on Trigger and fill the table name(s2ptech) and click on next.
  • Now do the action as shown in below image.
  • And click on next and finish.
  • At last you'll get trigger detail where if object status is valid and trigger status is enable will be shown then all works are done good.
And Yahooooooooo !!!!!
its time to insert your data in your table with auto-increment id in oracle

Read More

How to calculate date and time difference in java

Converts Date in milliseconds (ms) and calculate the differences between two dates, with following rules :

1000 milliseconds = 1 second
60 seconds = 1 minute
60 minutes = 1 hour
24 hours = 1 day
DateDifferentExample.java
package com.blogspot.s2ptech.date;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateDifferentExample {

public static void main(String[] args) {

String dateStart = "01/14/2012 09:29:58";
String dateStop = "01/15/2012 10:31:48";

//HH converts hour in 24 hours format (0-23), day calculation
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

Date d1 = null;
Date d2 = null;

try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);

//in milliseconds
long diff = d2.getTime() - d1.getTime();

long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);

System.out.print(diffDays + " days, ");
System.out.print(diffHours + " hours, ");
System.out.print(diffMinutes + " minutes, ");
System.out.print(diffSeconds + " seconds.");

} catch (Exception e) {
e.printStackTrace();
}

}

}
Result

1 days, 1 hours, 1 minutes, 50 seconds.
Read More

How to get current date and time in Java

Sometimes java developers need to find current date and time in their program.
In Java, you can get the current date time via following two classes – Date and Calendar. And, later use SimpleDateFormat class to convert the date into user friendly format.

1. Date() + SimpleDateFormat()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
2. Calender() + SimpleDateFormat()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));
Full example
Here is the full source code to show you the use of Date() and Calender() class to get and display the current date time.
package com.blogspot.s2ptech;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;


public class GetCurrentDateTime {
  public static void main(String[] args) {

  DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  //get current date time with Date()
  Date date = new Date();
  System.out.println(dateFormat.format(date));

  //get current date time with Calendar()
  Calendar cal = Calendar.getInstance();
  System.out.println(dateFormat.format(cal.getTime()));

  }
}
Read More