Monday, January 27, 2020

Java Applet

Java Applet, Oracle Java Study Materials, Oracle Java Prep

Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side.

Advantage of Applet


There are many advantages of applet. They are as follows:

- It works at client side so less response time.

- Secured

- It can be executed by browsers running under many plateforms, including Linux, Windows, Mac Os etc.


Drawback of Applet



- Plugin is required at client browser to execute applet.

Hierarchy of Applet


Java Applet, Oracle Java Study Materials, Oracle Java Prep

As displayed in the above diagram, Applet class extends Panel. Panel class extends Container which is the subclass of Component.


Lifecycle of Java Applet


1.Applet is initialized.

2. Applet is started.

3. Applet is painted.

4. Applet is stopped.

5. Applet is destroyed.

Java Applet, Oracle Java Study Materials, Oracle Java Prep

Lifecycle methods for Applet:


The java.applet.Applet class 4 life cycle methods and java.awt.Component class provides 1 life cycle methods for an applet.

java.applet.Applet class


For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle methods of applet.

1. public void init(): is used to initialized the Applet. It is invoked only once.
2. public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet.
3. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is minimized.
4. public void destroy(): is used to destroy the Applet. It is invoked only once.


java.awt.Component class


The Component class provides 1 life cycle method of applet.

1. public void paint(Graphics g): is used to paint the Applet. It provides Graphics class object that can be used for drawing oval, rectangle, arc etc.


Who is responsible to manage the life cycle of an applet?


Java Plug-in software.

How to run an Applet?


There are two ways to run an applet

1. By html file.
2. By appletViewer tool (for testing purpose).


Simple example of Applet by html file:


To execute the applet by html file, create an applet and compile it. After that create an html file and place the applet code in html file. Now click the html file.

//First.java  
import java.applet.Applet;  
import java.awt.Graphics;  
public class First extends Applet{  
  
public void paint(Graphics g){  
g.drawString("welcome",150,150);  
}  
  
}  

Note: class must be public because its object is created by Java Plugin software that resides on the browser.

myapplet.html

<html>  
<body>  
<applet code="First.class" width="300" height="300">  
</applet>  
</body>  
</html>  

Simple example of Applet by appletviewer tool:


To execute the applet by appletviewer tool, create an applet that contains applet tag in comment and compile it. After that run it by: appletviewer First.java. Now Html file is not required but it is for testing purpose only.

//First.java  
import java.applet.Applet;  
import java.awt.Graphics;  
public class First extends Applet{  
  
public void paint(Graphics g){  
g.drawString("welcome to applet",150,150);  
}  
  
}  
/* 
<applet code="First.class" width="300" height="300"> 
</applet> 
*/  

To execute the applet by appletviewer tool, write in command prompt:

c:\>javac First.java
c:\>appletviewer First.java

Sunday, January 26, 2020

What are the major features of Java programming?

Following are the notable features of Java:

Oracle Java Programming, Oracle Java Tutorial and Material, Oracle Java Study Materials, Oracle Java Prep

Object Oriented


In Java, everything is an Object. Java can be easily extended since it is based on the Object model.

Platform Independent


Unlike many other programming languages including C and C++, when Java is compiled, it is not compiled into platform specific machine, rather into platform-independent byte code. This byte code is distributed over the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.

Simple


Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.

Secure


With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption.

Architecture-neutral


Java compiler generates an architecture-neutral object file format, which makes the compiled code executable on many processors, with the presence of Java runtime system.

Portable


Being architecture-neutral and having no implementation dependent aspects of the specification makes Java portable. The compiler in Java is written in ANSI C with a clean portability boundary, which is a POSIX subset.

Robust


Java makes an effort to eliminate error-prone situations by emphasizing mainly on compile time error checking and runtime checking.

Multithreaded


With Java's multithreaded feature it is possible to write programs that can perform many tasks simultaneously. This design feature allows the developers to construct interactive applications that can run smoothly.

Interpreted


Java byte code is translated on the fly to native machine instructions and is not stored anywhere. The development process is more rapid and analytical since the linking is an incremental and light-weight process.

High Performance


With the use of Just-In-Time compilers, Java enables high performance.

Distributed


Java is designed for the distributed environment of the internet.

Dynamic


Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment. Java programs can carry an extensive amount of run-time information that can be used to verify and resolve accesses to objects at run-time.

Thursday, January 23, 2020

Difference between Sun (Oracle) JVM and IBM JVM

Sun (Oracle) JVM, IBM JVM, Oracle Java Study Materials, Oracle Java Learning, Oracle Java Online Exam

There are different JVM implementation available apart from popular Sun's (now Oracle) hotspot JVM like IBM's JVM. Now the question comes, what makes two JVM different to each other? Quite a lot of thing can be different even if two JVMs are implemented by following Java virtual machine specification. IBM has its own JVM implementation which is different than Oracle's JVM in many things like JIT(Just in Time) Compiler, structure of Java heap, garbage collectors and support for monitoring and management. Though I have mostly used Sun’s hotspot JVM recently got a chance to run some Java program on IBM JVM on AIX platform and In this article I will shared some differences between Oracle and IBM JVM which is worth remembering. IBM's JVM is particularly useful on a platform which is not supported by Oracle like AIX systems.

IBM vs Oracle JVM


1) The first difference between Oracle and IBM JVM is that IBM uses different JIT and Garbage collectors than Oracle's JVM.

2) Another difference between IBM JVM and Oracle JVM is that IBM's JVM has different heap structure than Oracle's JVM. By default, IBM JVM does not use Generational Garbage Collector which can be enabled by JVM option (-Xgcpolicy:gencon)IBM JVM doesn't have PermGen space for storing class related metadata so free from PermGen OutOfMemoryError.

Difference between Sun (Oracle) JVM and IBM JVM
3) IBM JVM has a different kind of compiler known as Ahead Of time Compiler (AOT compiler) which work to reduce some overhead from JITing code whenever JVM starts. AOT compiler memory maps pre-compiled class file so that they don't need to go JIT compilation when IBM's JVM runs.

4) IBM JVM also supports class sharing which can be used to store classes which are shared across different JVM instances. This feature may available to Oracle JVM as well.

5) Earlier there was another different related to CompressOOPS but from Java6 update releases Oracle's JVM also support CompressedOOPS which is quite a handy JVM option to get better performance in 64 bit JVM.

6) IBM also provide some tools to monitor and analyze thread dumps which can be used to find deadlocks, resource contention, and bottleneck.

That's all on the difference between IBM and Oracle JVM. You won't notice much difference until you go real deep to get better performance and scalability stuff, for most of Java programs it just runs fine because both JVM support standard Java libraries.

Wednesday, January 22, 2020

Transaction Management in JDBC

Transaction represents a single unit of work.

Oracle Java Tutorials and Materials, Oracle Java Certifications, Oracle Java Study Materials, Oracle JDBC

The ACID properties describes the transaction management well. ACID stands for Atomicity, Consistency, isolation and durability.

Atomicity means either all successful or none.

Consistency ensures bringing the database from one consistent state to another consistent state.

Isolation ensures that transaction is isolated from other transaction.

Durability means once a transaction has been committed, it will remain so, even in the event of errors, power loss etc.

Advantage of Transaction Mangaement

fast performance It makes the performance fast because database is hit at the time of commit.

Oracle Java Tutorials and Materials, Oracle Java Certifications, Oracle Java Study Materials, Oracle JDBC

In JDBC, Connection interface provides methods to manage transaction.

Method Description 
void setAutoCommit(boolean status)   It is true bydefault means each transaction is committed bydefault.
void commit()   commits the transaction. 
void rollback()   cancels the transaction. 

Simple example of transaction management in jdbc using Statement


Let's see the simple example of transaction management using Statement.

import java.sql.*;  
class FetchRecords{  
public static void main(String args[])throws Exception{  
Class.forName("oracle.jdbc.driver.OracleDriver");  
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
con.setAutoCommit(false);  
  
Statement stmt=con.createStatement();  
stmt.executeUpdate("insert into user420 values(190,'abhi',40000)");  
stmt.executeUpdate("insert into user420 values(191,'umesh',50000)");  
  
con.commit();  
con.close();  
}}  

If you see the table emp400, you will see that 2 records has been added.

Example of transaction management in jdbc using PreparedStatement


Let's see the simple example of transaction management using PreparedStatement.

import java.sql.*;  
import java.io.*;  
class TM{  
public static void main(String args[]){  
try{  
  
Class.forName("oracle.jdbc.driver.OracleDriver");  
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
con.setAutoCommit(false);  
  
PreparedStatement ps=con.prepareStatement("insert into user420 values(?,?,?)");  
  
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));  
while(true){  
  
System.out.println("enter id");  
String s1=br.readLine();  
int id=Integer.parseInt(s1);  
  
System.out.println("enter name");  
String name=br.readLine();  
  
System.out.println("enter salary");  
String s3=br.readLine();  
int salary=Integer.parseInt(s3);  
  
ps.setInt(1,id);  
ps.setString(2,name);  
ps.setInt(3,salary);  
ps.executeUpdate();  
  
System.out.println("commit/rollback");  
String answer=br.readLine();  
if(answer.equals("commit")){  
con.commit();  
}  
if(answer.equals("rollback")){  
con.rollback();  
}  
  
  
System.out.println("Want to add more records y/n");  
String ans=br.readLine();  
if(ans.equals("n")){  
break;  
}  
  
}  
con.commit();  
System.out.println("record successfully saved");  
  
con.close();//before closing connection commit() is called  
}catch(Exception e){System.out.println(e);}  
  
}}  

It will ask to add more records until you press n. If you press n, transaction is committed.

Tuesday, January 21, 2020

Java Database Connectivity with Oracle

Java Database Connectivity with Oracle, Oracle Java Study Materials, Oracle Java Online Exam, Oracle Java Prep

To connect java application with the oracle database, we need to follow 5 following steps. In this example, we are using Oracle 10g as the database. So we need to know following information for the oracle database:

1. Driver class: The driver class for the oracle database is oracle.jdbc.driver.OracleDriver.

2. Connection URL: The connection URL for the oracle10G database is jdbc:oracle:thin:@localhost:1521:xe where jdbc is the API, oracle is the database, thin is the driver, localhost is the server name on which oracle is running, we may also use IP address, 1521 is the port number and XE is the Oracle service name. You may get all these information from the tnsnames.ora file.

3. Username: The default username for the oracle database is system.

4. Password: It is the password given by the user at the time of installing the oracle database.

Create a Table

Before establishing connection, let's first create a table in oracle database. Following is the SQL query to create a table.

create table emp(id number(10),name varchar2(40),age number(3)); 

Example to Connect Java Application with Oracle database


In this example, we are connecting to an Oracle database and getting data from emp table. Here, system and oracle are the username and password of the Oracle database.

import java.sql.*; 
class OracleCon{ 
public static void main(String args[]){ 
try{ 
//step1 load the driver class 
Class.forName("oracle.jdbc.driver.OracleDriver"); 
 
//step2 create  the connection object 
Connection con=DriverManager.getConnection( 
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle"); 
 
//step3 create the statement object 
Statement stmt=con.createStatement(); 
 
//step4 execute query 
ResultSet rs=stmt.executeQuery("select * from emp"); 
while(rs.next()) 
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3)); 
 
//step5 close the connection object 
con.close(); 
 
}catch(Exception e){ System.out.println(e);} 
 
} 
} 

download this example

The above example will fetch all the records of emp table.

To connect java application with the Oracle database ojdbc14.jar file is required to be loaded.

download the jar file ojdbc14.jar

Two ways to load the jar file:

1. paste the ojdbc14.jar file in jre/lib/ext folder
2. set classpath

1) paste the ojdbc14.jar file in JRE/lib/ext folder:

Firstly, search the ojdbc14.jar file then go to JRE/lib/ext folder and paste the jar file here.

2) set classpath:

There are two ways to set the classpath:

◉ temporary
◉ permanent

Java Database Connectivity with Oracle, Oracle Java Study Materials, Oracle Java Online Exam, Oracle Java Prep
How to set the temporary classpath:

Firstly, search the ojdbc14.jar file then open command prompt and write:

C:>set classpath=c:\folder\ojdbc14.jar;.; 

How to set the permanent classpath:

Go to environment variable then click on new tab. In variable name write classpath and in variable value paste the path to ojdbc14.jar by appending ojdbc14.jar;.; as C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar;.;

Source: javatpoint.com

Monday, January 20, 2020

Difference between JDK, JRE, and JVM

We must understand the differences between JDK, JRE, and JVM before proceeding further to Java. See the brief overview of JVM here.

If you want to get the detailed knowledge of Java Virtual Machine, move to the next page. Firstly, let's see the differences between the JDK, JRE, and JVM.

JVM


JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine because it doesn't physically exist. It is a specification that provides a runtime environment in which Java bytecode can be executed. It can also run those programs which are written in other languages and compiled to Java bytecode.

JVMs are available for many hardware and software platforms. JVM, JRE, and JDK are platform dependent because the configuration of each OS is different from each other. However, Java is platform independent. There are three notions of the JVM: specification, implementation, and instance.

The JVM performs the following main tasks:

◉ Loads code
◉ Verifies code
◉ Executes code
◉ Provides runtime environment

JRE


JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The Java Runtime Environment is a set of software tools which are used for developing Java applications. It is used to provide the runtime environment. It is the implementation of JVM. It physically exists. It contains a set of libraries + other files that JVM uses at runtime.

The implementation of JVM is also actively released by other companies besides Sun Micro Systems.

JDK, JRE, JVM, Oracle Java Study Material, Oracle Java Guides, Oracle Java Tutorial and Material, Oracle Java Prep

JDK


JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software development environment which is used to develop Java applications and applets. It physically exists. It contains JRE + development tools.

JDK is an implementation of any one of the below given Java Platforms released by Oracle Corporation:

◉ Standard Edition Java Platform

◉ Enterprise Edition Java Platform

◉ Micro Edition Java Platform

The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc), etc. to complete the development of a Java Application.

JDK, JRE, JVM, Oracle Java Study Material, Oracle Java Guides, Oracle Java Tutorial and Material, Oracle Java Prep