Monthly Archives: November 2014

What is Queue?

Queues are dynamic collections which have some concept of order. This can be either based on order of entry into the queue - giving us First-In-First-Out (FIFO) or Last-In-First-Out (LIFO) queues. Both of these can be built with linked lists: the simplest "add-to-head" implementation of a linked list gives LIFO behaviour. A minor modification - adding a tail pointer and adjusting the addition method implementation - will produce a FIFO queue.

Queue Implementation

package queue

import java.util.Scanner;

public class Queue {

int[] arr = new int[10];
int a = -1;
int b=0;
public void enqueue(int i){

if(a==9){

System.out.println("Queue is full");

}

else
arr[++a]=i;

}
public void dequeue(){

if(a>b){

for(b=0; b

arr[b]=arr[b+1];

}
a--;
b=0;
}

}

public void display(){

for(int i=0; i<=9; i++){

System.out.println("values at index "+i+":"+arr[i]);

}

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
Queue q = new Queue();
int var = -1;
while( var !=0 ){

System.out.println("1. Insert item in queue");
System.out.println("2. Delete item from queue");
System.out.println("3. Display Queue");
System.out.println("0. Exit");
var = sc.nextInt();
switch(var){

case 1:
System.out.print("Enter item in Queue: ");
int pushItem = sc.nextInt();
q.enqueue(pushItem);
break;
case 2:

q.dequeue();
break;
case 3:
q.display();
break;
}
}

}
}

In this tutorial , i will teach you about a database connectivity with mysql database using java programming language.

As java is a object oriented programming language so the best approach to follow is :

1. Make a individual package for a database.

1.1 In this package create a class for database connectivity.

2. Make a individual packages for each of tables in a database.

2.1 In each of package make a bean class of a particular table.

2.2 Make a class for a task which you want to do. (e.g Selection , insertion etc ).

The following code is for a database connectivity with mysql refer to step 1 and 1.1

 DatabaseConnection.java

package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class DatabaseConnection {

public static Connection con;

public static void init() throws Exception{
String JDBC_DRIVER   = "com.mysql.jdbc.Driver";
String JDBC_URL      = "jdbc:mysql://localhost:3306/test";
String JDBC_USER     = "root";
String JDBC_PASSWORD = "";

Class.forName(JDBC_DRIVER);
con = (Connection)
DriverManager.getConnection(JDBC_URL,JDBC_USER,JDBC_PASSWORD);
System.out.println("Driver Loaded Sucessfully");

}//end init
}

now i want to fetch record so i follow the step no 2.

for example create EmployeeBean.java class. In a bean class we have only setters and getter of a properties.

package employee;
public class EmployeeBean {
private int emp_id;
private String emp_name;
private String emp_fname;
private String emp_surname;

public int getEmp_id() {
return emp_id;
}
public void setEmp_id(int emp_id) {
this.emp_id = emp_id;
}
public String getEmp_name() {
return emp_name;
}
public void setEmp_name(String emp_name) {
this.emp_name = emp_name;
}
public String getEmp_fname() {
return emp_fname;
}
public void setEmp_fname(String emp_fname) {
this.emp_fname = emp_fname;
}
public String getEmp_surname() {
return emp_surname;
}
public void setEmp_surname(String emp_surname) {
this.emp_surname = emp_surname;
}

@Override
public String toString(){
return emp_name;
}

}

use following where you want to perform database operations.

class EmpData{

public  Vector<EmployeeBean> getAllEmployees() throws SQLException{
String query = "SELECT emp_id, emp_name, emp_fname, emp_surname FROM employee";

Statement ps = null;
ResultSet rs = null;
Vector<EmployeeBean> vector = new Vector<EmployeeBean>();
try{
ps = (Statement)database.Mysqlstudent.con.createStatement();
rs = ps.executeQuery(query);
while(rs.next()){
EmployeeBean bean = new EmployeeBean();
bean.setEmp_id(rs.getInt("emp_id"));
bean.setEmp_name(rs.getString("emp_name"));
bean.setEmp_fname(rs.getString("emp_fname"));
bean.setEmp_surname(rs.getString("emp_surname"));
vector.add(bean);
}//end while loop
}finally{
if(ps != null) ps.close();
if(rs != null) rs.close();
}

return vector;

}//end getEmployees

}

and use it by creating a object of Empdata where we requires it. Like,

Empdata data = new Empdata();

Vector<EmployeeBean> vect = data.getAllEmployees();

so in vect variable you get all record of employees.

Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

So we are going to learn that , how we doing serialization in Java.

Lets start...

i m going to create a class person named as Person which implements Serilizable interface.

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

Person.java
package serialization;

/**
*
* @author Maria
*/
import java.io.Serializable;
public class Person implements Serializable {

private String firstname;
private String lastname;
person(String firstname,String lastname){

this.firstname = firstname;
this.lastname = lastname;

}

/**
* @return the firstName
*/
public String getFirstname() {
return firstname;
}

/**
* @param firstname the firstName to set
*/
public void setFirstname(String firstname) {
this.firstname = firstname;
}

/**
* @return the lastName
*/
public String getLastname() {
return lastname;
}

/**
* @param lastname the lastName to set
*/
public void setLastname(String lastname) {
this.lastname = lastname;
}

}

In above class we have a setters and a getters of a serialiable class.

And now we have a main class in which we create the objects of Person class so we can use our serialized class.

Main.java 

package serialization;

/**
*
* @author  Maria
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Main {

public static void main(String args[]) throws FileNotFoundException, IOException, ClassNotFoundException{

FileInputStream fis;
FileOutputStream fos;
ObjectInputStream ois;
ObjectOutputStream oos;

String filename = "p.ser"; // creating a file named as p and .ser is extension for serlizable file
person p = new person("maria","soomro");

fos = new FileOutputStream(filename);
oos = new ObjectOutputStream(fos);
oos.writeObject(p);

}
}

In Above Main.java class ,  the main methods throws some exceptions. ObjectOutputStream used for writing  object through writeObject  method.

When i run this main class the p.ser class is created

Serialization

Serialization

and for deserialization you have to write following code in calling class like in our example a Main class inside main method.

fis = new FileInputStream(filename);
ois = new ObjectInputStream(fis);
person pe = (person)ois.readObject();

System.out.println(pe.getFirstname());
System.out.println(pe.getLastname());
ObjectInputStream used for reading object through readObject method.

Output is :

Serialization

Serialization