How to display an animated gif in java swing

The type ImageIcon (sub-type of Icon) allows to display an animated GIF. Example :

// We suppose you have already set your JFrame 
Icon imgIcon = new ImageIcon(this.getClass().getResource("ajax-loader.gif"));
JLabel label = new JLabel(imgIcon);
label.setBounds(668, 43, 46, 14); // for example, you can use your own values
frame.getContentPane().add(label);

 

Java native keyword

When applied to a method declaration, the Java « native » keyword indicates to the jvm that the method is implemented in the platform code .
A method marked as native cannot have a body.

Oracle’s definition of the native keyword :

A Java keyword that is used in method declarations to specify that the method is not implemented in the same Java source file, but rather in another language.

 

How to display all system properties in Java

This code displays all system (jvm) properties in java :

import java.util.Iterator;
import java.util.Properties;

public class Test {

 public static void main(String[] args) {
   Properties properties = System.getProperties();
   Iterator<Object> iter = properties.keySet().iterator();
   while(iter.hasNext()){
    String key = (String) iter.next();
    System.out.println(key + " = " + properties.getProperty(key));
   }
  }
 }

 

Here is a set of default system properties you can access:

java.runtime.name
sun.boot.library.path
java.vm.version
user.country.format
java.vm.vendor
java.vendor.url
path.separator
java.vm.name
file.encoding.pkg
user.country
user.script
sun.java.launcher
sun.os.patch.level
java.vm.specification.name
user.dir
java.runtime.version
java.awt.graphicsenv
java.endorsed.dirs
os.arch
java.io.tmpdir
line.separator
java.vm.specification.vendor
user.variant
os.name
sun.jnu.encoding
java.library.path
java.specification.name
java.class.version
sun.management.compiler
os.version
user.home
user.timezone
java.awt.printerjob
file.encoding
java.specification.version
java.class.path
user.name
java.vm.specification.version
sun.java.command
java.home
sun.arch.data.model
user.language
java.specification.vendor
user.language.format
awt.toolkit
java.vm.info
java.version
java.ext.dirs
sun.boot.class.path
java.vendor
file.separator
java.vendor.url.bug
sun.io.unicode.encoding
sun.cpu.endian
sun.desktop
sun.cpu.isalist

How to get current TimeStamp in Java

There are several ways to get the current timestamp in Java. This example works in Java6, 7 and 8.

Please note that Java 8 contains now a simpler and more efficient date and time API.

// Using the java.sql.Timestamp class
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
System.out.println(timestamp);

// Using the Date object
Date date = new Date();
System.out.println(new Timestamp(date.getTime()));

 

How to connect to a database using JDBC

This example shows how to connect to a database using JDBC. (In this example we will connect to Oracle database) .

You will need to add the odbc{x}.jar library to your classpath, downloadable from Oracle’s website.

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class JdbcExample {

 public static void main(String[] args) {
  
  try{
  	// register the driver
  	Class.forName("oracle.jdbc.driver.OracleDriver");
  	// create the connexion
  	Connection connection
            =DriverManager.getConnection(
               "jdbc:oracle:thin:@localhost:1521:xe", "system", "pwd"
                );
  } catch (ClassNotFoundException e) {
    // the driver was not found
    e.printStackTrace();
  } catch (SQLException e) {
    // Error when connecting to database
    e.printStackTrace();
  }
 }

}

 

What is new in Java 8

Java 8 is a major release of the java programming language. It introduces new features and enhancements. The most significant ones are the following.

Lambda expressions

New Date and Time API

Method references

Default methods

Repeating annotations

Type annotations

Stream API

Optional

Narshon

new tools and utilities