Powered by Blogger.

What is Deserialization in java ? Write a simple program using Deserialization?

>> Monday, April 18, 2011

Deserialization is a process of converting the data from files or database converting to Stream of bytes using class Objects.


The Deserialization can be done after serializing the data only.Then we can read the data from the serialized files.

Convert your Serialized file into file form.

File fromFile = new File("Emp.ser");

By using the below two classes we can do the process of deserialization.
#1. FileInputStream
#2. ObjectInputStream
A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment.
FileInputStream fis = new FileInputStream(fromFile);

An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream. This can be Converts the Serialized file into the Object form.
ObjectInputStream ois = new ObjectInputStream(fis);

To get the object of a serialized file we have to typecast into our class Employee to read the values in a file using variable names.
Employee emp = (Employee) ois.readObject();

After that close all the object connections.
You should write the  Employee class already written in Serialization.(check it there) 
Program for Deserialization:
package javabynataraj.iopack;

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

public class Deserial {
    public static void main(String arg[]) {
        File fromFile = new File("Emp.ser");
        try {
            FileInputStream fis = new FileInputStream(fromFile);
            ObjectInputStream ois = new ObjectInputStream(fis);
            Employee emp = (Employee) ois.readObject();
            System.out.println("Deserialized data: \n"+ emp.eno + " "+ emp.ename +" "+ emp.esal+" "+emp.eaddr+ "  from Emp.ser");
            ois.close();
        } catch(IOException e) {
            System.out.println("Deserialization failed");
            System.out.println(e);
            System.exit(1);
        } catch(ClassNotFoundException e) {
            System.out.println("Deserialization failed");
            System.out.println(e);
            System.exit(1);
        }
    }
}

Read more..

Program for User defined Exception in java

To create user defined exception first you should create a class by extending Exception class.

class BadString extends RuntimeException{

}

Create  another class  named as "UserExcep" to throw our own exception.

public class UserExcep


Write some logical conditions to throw your exception and the exception caught by catch block.

package javabynataraj.exceptions;

import java.io.BufferedReader;
import java.io.InputStreamReader;

class BadString extends RuntimeException{

}
public class UserExcep{
    public static void main(String[] args) {
        try{
            System.out.println("Enter the String: ");
            BufferedReader sb=new BufferedReader(new InputStreamReader(System.in));
            String s=sb.readLine();
            if(s.equals("hateyou")){
                throw new BadString();
            }
            System.out.println("I Accept your String");
        }catch(Exception e){
            System.out.println("Please enter a good String");
            //e.printStackTrace();
        }
    }
}


Introduction to Java Programming, Comprehensive (8th Edition)

Read more..

What is Serialization? Write a Simple program in java using Serialization?

>> Saturday, April 16, 2011

"Serialization is the process of converting the data(Objects) into stream of bytes and storing in to the files or database."

Serialization in Java with Simple program_JavabynataraJ

We can do the Serialization process by implementing Serializable interface.

class SerialDemo implements java.io.Serializable.

The above step tells the compiler that you are going to implement serialization.
Which basically means that the object is going to be saved or persisted.

Read more..

Upload CSV file into MySql Database based on columns using Servlets and Java

>> Tuesday, April 12, 2011

The CSV file is having multiple values to insert in perticular columns and rows in mysql database.

Here we have two files so we have to insert in two tables .

one is Header part file

another is Detail part file

Basically these two are to upload the questions and answers into the examination portal to upload question papers and answer paper in (Merit Tracking System project).

First we have to do the things are Uploaded file has to save in perticular folder and then they have to insert the values into the database table.This is our main task to do.

Read more..

Types of JDBC Drivers

>> Saturday, April 9, 2011

JDBC drivers are divided into four types or levels. The different types of jdbc drivers are:

Type 1: JDBC-ODBC Bridge driver (Bridge)
Type 2: Native-API/partly Java driver (Native)
Type 3: AllJava/Net-protocol driver (Middleware)
Type 4: All Java/Native-protocol driver (Pure)

4 types of jdbc drivers are elaborated in detail as shown below:

Type 1 JDBC Driver

JDBC-ODBC Bridge driver
The Type 1 driver translates all JDBC calls into ODBC calls and sends them to the ODBC driver. ODBC is a generic API. The JDBC-ODBC Bridge driver is recommended only for experimental use or when no other alternative is available.
JDBC Type Driver 1

Read more..

JDBC - CallableStatement IN, OUT, INOUT parameters

>> Friday, April 8, 2011

Callable Statement in Brief_JavabynataraJThe CallableStatement interface allows the use of SQL statements to call stored procedures. Stored procedures are programs that have a database interface.
These programs possess the following:
1)    They can have input and output parameters, or parameters that    are   both input and output.
2)    They can have a return value.
3)    They have the ability to return multiple ResultSets.

Conceptually in JDBC, a stored procedure call is a single call to the database, but the program associated with the stored procedure may process hundreds of database requests. The stored procedure program may also perform a number of other programmatic tasks not typically done with SQL statements.

Read more..

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2022. All rights reserved.