Total Pageviews

Search This Blog

Saturday, June 25, 2016

ng-include example


Explanation:

  1.  Download angular.js from https://angularjs.org/
  2. Keep and the files in same folder. i.e. angular.js, MyAngular,java, EmployeeTable,html, EmployeeList.html, script19.js and css9.css
  3. Below is the HTML code of MyAngular.java (mainfile) which we will be executing. It contains reference of 2 files. EmployeeTable.html and EmployeeList.html.
  4. On selection of Table value from the dropdown will display Value in Table format(giving call to EmployeeTable.html) and if selecting List from dropdown will display value in list format(as per EmployeeList.html)



-----MyAngular.java (Main File) ---------

<html ng-app="myModule">
<head>

<title>This is example of ng-include. We will be including EmployeeTable.html in this page & EmployeeList.html </title>
<script src="angular.js"></script>
<script src="script19.js"></script>
<link href="css9.css" rel="stylesheet"></link>
</head>
<body ng-controller="myController">
select View <select ng-model="employeeView">
<option value="EmployeeTable.html">Table</option>
<option value="EmployeeList.html">List</option>
</select>
<br />

<div ng-include="employeeView">
</div>
</body>
</html>

-----------EmployeeTable.html-------

<br />
<table>
<thead>
<tr>
<th>Name </th>
<th>Gender </th>
<th>Salary </th>
</tr>
</thead>
<tbody>
<tr ng-repeat="employee in employees">

<td>{{employee.name}}
</td>

<td>{{employee.gender}}
</td>

<td>{{employee.salary}}
</td>

</tr>
</tbody>

</table>
-----------EmployeeList.html-------


<br />
<ul ng-repeat="employee in employees">
<li>{{employee.name}}
<ul>
<li>{{employee.gender}} </li>
<li>{{employee.salary}} </li>
</ul>
</li>
</ul>
------------Script19.js-------------

  • By Default we setting EmployeeTable.html as employeeView as Table will be displayed by default


var myApp = angular.module("myModule",[]).
controller("myController",function($scope){

var employees = [
                {name:"Arshim",gender:"Male",salary:1000 },
                {name:"Ram",gender:"Male",salary:2000 },
                {name:"Shayam",gender:"Male",salary:4000 },
                {name:"Penos",gender:"Female",salary:6000 },
                {name:"David",gender:"Female",salary:7000 }
                ];

$scope.employees = employees;
$scope.employeeView = "EmployeeTable.html";


});




Initial when page will be loaded.





















When list is selected from dropdown.




Monday, March 28, 2011

How to deSearialized Java Objects

package com.safiuz;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class Manager1 {

public static void main(String[] args) {
//
FileInputStream fin = null;
ObjectInputStream in = null;
try{
File f1 = new File("text.txt");
fin = new FileInputStream(f1);
in = new ObjectInputStream(fin);
Person p1 = (Person)in.readObject();
System.out.println(p1);
}
catch(IOException ex){
ex.printStackTrace();
}
catch(ClassNotFoundException ex){
ex.printStackTrace();
}
finally
{
try{
if(in!= null){
in.close();
in = null;
}
}
catch(IOException ex){
ex.printStackTrace();
}
try
{
if(fin!= null)
{
fin.close();
fin = null;
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
}
}

How to Searialized Java Objects

package com.safiuz;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Person implements Serializable {
String name;
int age;
Double weight;
public Person(String name, int age, Double weight){
this.name = name;
this.age= age;
this.weight = weight;
}

public String toString(){
return name+","+ age+","+ weight;
}

}

public class Manager{
public static void main(String[] args) {
Person p1 = new Person("abc",22,50.090);
File f1 = new File("text.txt");

FileOutputStream fout = null;
ObjectOutputStream out = null;

try{
fout = new FileOutputStream(f1);
out = new ObjectOutputStream(fout);
out.writeObject(p1);
System.out.println("done");
}
catch(IOException ex){
ex.printStackTrace();

}
finally
{
try
{
if(out!= null){
out.flush();
out.close();
out = null;
}
}
catch(IOException ex){
ex.printStackTrace();
}
try{
if (fout!= null){
fout.close();
fout=null;
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
}
}

Sunday, March 27, 2011

How to connect your java program with Oracle Database

package com.safiuz;

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

public class Manager4 {

public static void main(String[] args) {
//1. register driver
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex){
ex.printStackTrace();
}
Connection con = null;
Statement stmt = null;
try
{
//2. Establish Database connection
con = DriverManager.getConnection("jdbc:odbc:oracle-datasource","system","great123");

//3. Get the Statement
stmt = con.createStatement();

//4. Compose Sql Query
String sql = "insert into tab1 values(4,'Safiuz')";

//5. trigger Sql query
int i = stmt.executeUpdate(sql);
System.out.println(i);
}
catch(SQLException ex){
ex.printStackTrace();
}
finally{
try{
//6. Close Database resources
if(stmt!= null){
stmt.close();
stmt= null;
}
}
catch(SQLException ex){
ex.printStackTrace();
}
try{
if(con!= null){
con.close();
con = null;
}
}
catch(SQLException ex){
ex.printStackTrace();
}
}
}
};

Tuesday, March 15, 2011

To Develop your own Queue in Java

import java.util.LinkedList;

/*to develop my own Queue from removeFromQueue()*/

class MyQueue {

private LinkedList list = new LinkedList();

public void addToQueue(Object obj){
list.add(obj);
}
public Object removeFromQueue(){
return list.removeFirst();
}
public String toString(){
return list.toString();
}
}

public class Manager4 {

public static void main(String[] args) {
MyQueue st = new MyQueue();
st.addToQueue(100);
st.addToQueue(200);
st.addToQueue(30);
st.addToQueue(10);
System.out.println(st);
System.out.println(st.removeFromQueue());
System.out.println(st);
}
}

To Develop your own Stack in Java

import java.util.LinkedList;

/*to develop my own Stack */

class MYStack {
@SuppressWarnings("unchecked")
private LinkedList list = new LinkedList();
@SuppressWarnings("unchecked")
public void pushToStack(Object obj){
list.add(obj);
}
public Object popFromStack(){
return list.removeLast();
}
public String toString(){
return list.toString();
}
}

public class Manager {
public static void main(String[] args) {
MYStack st = new MYStack();
st.pushToStack(100);
st.pushToStack(200);
st.pushToStack(30);
st.pushToStack(10);
System.out.println(st);
System.out.println(st.popFromStack());
System.out.println(st);
}
}

Generics in java

Lets learn how Generic works in java:
First let create a class Test4 which take a generic type, right now we dnt know anything about E. It will be decided at object creation time:

public class Test4 {
E param1;
Test4(){}

void setParam1(E param1){
this.param1 = param1;
}
E getParam1(){
return param1;
}


}


Now after developing Test4 class now use this class in our Manager4 class.
Lets create two instance of Test4 class one taking String type and other taking Integer type.

public class Manager4 {
@SuppressWarnings("unchecked")

public static void main(String[] args) {
Test4 t1 = new Test4();
Test4 t2= new Test4();
t1.setParam1("hello");
t2.setParam1(4000);

System.out.println(t1.getParam1());
System.out.println(t2.getParam1());

Test4 t3 = new Test4();
t3.setParam1("hello");
// String s2 = t3.getParam1(); //we hav to downcast it to String
String s2 = (String) t3.getParam1();
System.out.println(s2);
}

}