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();
}
}
}
}
This blog is for java beginner with some sample programs to clear you basics doubts and enhance your java skill.
Total Pageviews
Search This Blog
Monday, March 28, 2011
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();
}
}
}
}
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();
}
}
}
};
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 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);
}
}
Labels:
Collection,
Learn Java in Simple way
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);
}
}
/*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);
}
}
Labels:
Collection,
Learn Java in Simple way
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);
}
}
First let create a class Test4 which take a generic type
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
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);
}
}
Labels:
Generics,
Learn Java in Simple way
Subscribe to:
Comments (Atom)