Saturday, 20 December 2014

Running a Java Application

When a program is compiled (C or C++ program), it is directly translated into machine code that is specific to a platform/processor. But running a java program is a two-step process. In java translation from source code to the executable code is achieved using two translators:

Java Compiler: A java program is first compiled by the java compiler to generate bytecode. Bytecode resemble machine code but it is not specific to any platform i.e. it can not be executed on a computer without further translation.


Java Interpreter: Java interpreter executes the byte code after interpreting and converting into machine code.

Example: the following java program just displays the message "Hello World" on the monitor/console.

Step1: Write the following code into file "Hello.java" using any text editor:

public class Hello{
    public static void main(String args[]){
        System.out.println("Hello World");
    }
}

Note: Java is a case sensitive language


Step 2: Compile the above written java program using the command:
javac Hello.java

Step 3: Run the java program
java Hello


Output: Hello World



Java: Type of applications

  1. Standalone applications
  2. Applets
  3. Web Applications
  4. Distributed Applications

1. Standalone applications: A standalone application is a program that runs on your computer. It is more or less like a C or C++ program.

2.  Applets: An applet is a application designed to travel over the internet and to be executed on the client machine by Java compatible web browser like Firefox or google chrome. Applets are also java programs but they reside on the servers. An applet can not be executed like standalone application. Applet can be executed only by embedding it into an HTML page like an image or sound file. To run an applet you need to access an HTML page which has applet embedded into it. When the web browser downloads such an HTML page, It subsequently loads the executable file, which contains Applet and then executes it on the local machine.

3. Web Applications: Web applications run on the web server. Web applications are accessed through web clients i.e. web browsers like firefox and google chrome. Whenever you access some web site by specifying the url, you are accessing some web application written in java.

Java servlets
Java server pages
HTML

Introduction to Java

Java is an object-oriented programming language developed by Sun Microsystems Inc in 1991.

Java has become the widely used programming language for the internet. Although while designing the language, the primary objective was to develop a programming language that is platform independent.

Sunday, 16 November 2014

Web SQL DB create, read, update and delete operations example

Here is the simplified example for web sql db create, read, update and delete operations:

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var db;
function createDatabase(dbName){
db = openDatabase(dbName, '1.0', 'Test DB', 2 * 1024 * 1024);
}

function createTable(tableName, arrFields){
db.transaction(function (tx) {
var fields = "";
for(f=0;f<arrFields.length;f++){
fields += arrFields[f] + ',';
}

fields = fields.substring(0, fields.length-1);

tx.executeSql('CREATE TABLE IF NOT EXISTS '+ tableName + ' (' + fields + ')');
});
}

function insertRecord(query){
db.transaction(function (tx) {
tx.executeSql(query);
});
}

function selectRecord(query, callBack){
var empArr = new Array();
db.transaction(function (tx) {
tx.executeSql(query, [], function (tx, results) {
var len = results.rows.length;

// Create the object of emp
for(i=0;i<len;i++){
var emp = {
id:results.rows.item(i).id,
name:results.rows.item(i).name,
salary:results.rows.item(i).salary
};

// Add objects to array
empArr[i] = emp;
}
callBack(empArr); // This is the callback
});
});
}

createDatabase('db_emp');
createTable('emp', ["id", "name", "salary"]);
insertRecord('INSERT INTO emp (id, name, salary) VALUES (1, "Rahul", 5000)');

// We are using callback in this method
selectRecord('select * from emp', function (results){
console.log(results);
});
</script>
</head>
<body>
</body>
</html>

Friday, 14 November 2014

Order by filter in js controller

Below code will sort the list listLeaves in the descending order of date field .

$scope.listLeaves = $filter('orderBy')($scope.listLeaves, "-date")