Found 9321 Articles for Object Oriented Programming

Database operations in Java

karthikeya Boyini
Updated on 19-Jun-2020 13:27:08

1K+ Views

This article provides an example of how to create a simple JDBC application. This will show you how to open a database connection, execute a SQL query, and display the results.Creating JDBC ApplicationThere are following six steps involved in building a JDBC application −Import the packages: Requires that you include the packages containing the JDBC classes needed for database programming. Most often, using import java.sql.* will suffice.Register the JDBC driver: Requires that you initialize a driver so you can open a communication channel with the database.Open a connection: Requires using the DriverManager.getConnection() method to create a Connection object, which represents ... Read More

Date Formatting Using SimpleDateFormat

karthikeya Boyini
Updated on 19-Jun-2020 12:43:03

786 Views

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.ExampleLive Demoimport java.util.*; import java.text.*; public class DateDemo {    public static void main(String args[]) {       Date dNow = new Date( );       SimpleDateFormat ft =               new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");       System.out.println("Current Date: " + ft.format(dNow));    } }This will produce the following result −OutputCurrent Date: Sun 2004.07.18 at 04:14:09 PM PDTSimple DateFormat ... Read More

Date Formatting Using printf

Samual Sam
Updated on 19-Jun-2020 12:26:09

5K+ Views

Date and time formatting can be done very easily using the printf method. You use a two-letter format, starting with t and ending in one of the letters of the table as shown in the following code.ExampleLive Demoimport java.util.Date; public class DateDemo {    public static void main(String args[]) {       // Instantiate a Date object       Date date = new Date();       // display time and date       String str = String.format("Current Date/Time : %tc", date );       System.out.printf(str);    } }This will produce the following ... Read More

Date Parsing using SimpleDateFormat

karthikeya Boyini
Updated on 19-Jun-2020 12:27:22

238 Views

The SimpleDateFormat class has parse() method, which tries to parse a string according to the format stored in the given SimpleDateFormat object.ExampleLive Demoimport java.util.*; import java.text.*;   public class DateDemo {    public static void main(String args[]) {       SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");               String input = args.length == 0 ? "1818-11-11" : args[0];         System.out.print(input + " Parses as ");               Date t;       try {          t = ft.parse(input);                    System.out.println(t);               } catch (ParseException e) {                    System.out.println("Unparseable using " + ft);               }    } }A sample run of the above program would produce the following result −Output1818-11-11 Parses as Wed Nov 11 00:00:00 EST 1818

Deadlock in Java Multithreading

Samual Sam
Updated on 19-Jun-2020 12:30:28

2K+ Views

Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. Deadlock occurs when multiple threads need the same locks but obtain them in a different order. A Java multithreaded program may suffer from the deadlock condition because the synchronized keyword causes the executing thread to block while waiting for the lock, or monitor, associated with the specified object. Here is an example.ExampleLive Demopublic class TestThread {    public static Object Lock1 = new Object();    public static Object Lock2 = new Object();    public static void main(String args[]) {       ThreadDemo1 T1 = ... Read More

GregorianCalendar Class in Java

Samual Sam
Updated on 19-Jun-2020 12:40:18

141 Views

GregorianCalendar is a concrete implementation of a Calendar class that implements the normal Gregorian calendar with which you are familiar. We did not discuss Calendar class in this tutorial, you can look up standard Java documentation for this.The getInstance( ) method of Calendar returns a GregorianCalendar initialized with the current date and time in the default locale and time zone. GregorianCalendar defines two fields: AD and BC. These represent the two eras defined by the Gregorian calendar.There are also several constructors for GregorianCalendar objects −Sr.No.Constructor & Description1GregorianCalendar()Constructs a default GregorianCalendar using the current time in the default time zone with the default ... Read More

Sleeping for a while in Java

Samual Sam
Updated on 19-Jun-2020 12:04:25

674 Views

You can sleep for any period of time from one millisecond up to the lifetime of your computer. For example, the following program would sleep for 3 seconds −Example Live Demoimport java.util.*; public class SleepDemo {    public static void main(String args[]) {       try {                    System.out.println(new Date( ) + "");                    Thread.sleep(5*60*10);                    System.out.println(new Date( ) + "");               } catch (Exception e) {          System.out.println("Got an exception!");             }    } }This will produce the following result −OutputSun May 03 18:04:41 GMT 2009 Sun May 03 18:04:51 GMT 2009

Create translucent windows in Java Swing

Samual Sam
Updated on 19-Jun-2020 12:10:44

253 Views

With JDK 7, we can create a translucent window using swing very easily. With following code, a JFrame can be made translucent.// Set the window to 55% opaque (45% translucent). frame.setOpacity(0.55f);ExampleSee the example below of a window with 55% translucency.import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class Tester {    public static void main(String[] args)  {                     JFrame.setDefaultLookAndFeelDecorated(true);          // Create the GUI on the event-dispatching thread          SwingUtilities.invokeLater(new Runnable() {          @Override          public void ... Read More

Are static local variables allowed in Java?

karthikeya Boyini
Updated on 18-Jun-2020 15:41:40

848 Views

Unlike C, C++, Java does not allow static local variables. The compiler will throw the compilation error.ExampleCreate a java class named Tester.Tester.javaLive Demopublic class Tester {    public static void main(String args[]) {       static int a = 10;    } }OutputCompile and Run the file to verify the result.Tester.java:3: error: illegal start of expression                    static int a = 10;

Assigning values to static final variables in java

Samual Sam
Updated on 18-Jun-2020 15:45:03

722 Views

In java, a non-static final variable can be assigned a value at two places.At the time of declaration.In constructor.ExampleLive Demopublic class Tester {    final int A;    //Scenario 1: assignment at time of declaration    final int B = 2;    public Tester() {       //Scenario 2: assignment in constructor       A = 1;    }    public void display() {       System.out.println(A + ", " + B);    }    public static void main(String[] args) {               Tester tester = new Tester();   ... Read More

Advertisements