Insert - Update - Delete in JDBC ....

Insert - Update - Delete in JDBC ....

package jdbcpack;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

//import com.mysql.jdbc.PreparedStatement;

public class InsUpdDel
{

    public static void main(String[] args)
    {
        Connection con=null;
        PreparedStatement pstat = null;
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
           
            pstat = con.prepareStatement("Insert into student values(?,?,?)");
            pstat.setInt(1, 2009);
            pstat.setString(2, "Rohit");
            pstat.setInt(3,22);
            int x= pstat.executeUpdate();
            if(x>=1)
                System.out.println("\n successfully inserted !!");
           
            System.out.println("\n........Updating .......");
           
            Scanner sc =  new Scanner(System.in);
            System.out.println("Please enter the sid");
            int i = sc.nextInt();
            pstat = con.prepareStatement("Update student set age=30 where sid=?");
                    pstat.setInt(1, i);
                            x= pstat.executeUpdate();
                            if(x>=1)
                                System.out.println("successfully updated !!");
                           
           
            System.out.println("\n.............Delete Statement........................");
           
           
            System.out.println("Please enter the sid to be deleted !!");
            i=sc.nextInt();
            pstat=con.prepareStatement("delete from student where sid=?");
            pstat.setInt(1,i);
            x= pstat.executeUpdate();
           
            if(x>=1)
                System.out.println("successfully Deleted !!");
            else
                System.out.println("not found !!");
           
           
           
        }
       
        catch(Exception ex)
        {
            System.out.println(ex.toString());
        }
        finally
        {
            try
            {
                con.close();
                pstat.close();
            }
   
            catch (SQLException e)
            {
                e.printStackTrace();
            }
   
        }
    }

}

Simple JDBC Programme

Simple JDBC Programme



package jdbcpack2;

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


public class Test1 {

    public static void main(String[] args)
    {
        Connection con=null;
        Statement stmt=null;
       
        try
        {
           
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
            stmt=con.createStatement();
            ResultSet rs = stmt.executeQuery("select *from student");
            while(rs.next())
            {
                System.out.println("Result :" + rs.getInt(1) + " " + rs.getString(2) + " " + rs.getInt(3));
               
            }
        }
        catch (ClassNotFoundException e)
        {
           
            e.printStackTrace();
        }
       
        catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
   
    }

}


calculate Month from Two Dates

int calculateMonth()
{
int month;

month = this.getOpenDate().getMonth() - this.getCloseDate().getMonth();
int year =  this.getCloseDate().getYear()-this.getOpenDate().getYear();
month = month + year*12;
this.setTime(month);

return month;

}



--------------------------------------------------------------------------------------------------------------------------

public Date getCloseDate()
{
SimpleDateFormat ft = new SimpleDateFormat("dd/MM/yyyy");
Date dt = null;
try
{

dt =ft.parse( this.closeDate);
}

catch (ParseException e)
{

e.printStackTrace();
}
return dt;
}

-------------------------------------------------------------------------------------------------------------------------

public void setCloseDate(String closeDate) 
{
this.closeDate = closeDate;
}

Serialization-with Merge two files without no redundant object

package iopack;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import pkg.Student;

public class SerializationTest {

public static void main(String[] args)
{
Student s = new Student("Ram",5005,"MCA",5);
Student s1 = new Student("Sham",5004,"MCA",4);
Student s2 = new Student("Nanda",5003,"MCA",3);
Student s3 = new Student("Krishna",5002,"MCA",5);
Student s4 = new Student("Krishna",5002,"MCA",5);

Set <Student> stt = new TreeSet<Student>(); // for Merging

ObjectOutputStream oos = null;

try
{
oos = new ObjectOutputStream(new FileOutputStream("D:\\Stud.ser"));
oos.writeObject(s);
oos.writeObject(s1);
oos.writeObject(s2);
oos.writeObject(s3);
oos.writeObject(s4);

System.out.println("Write done");

}

catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

catch (IOException e)
{

e.printStackTrace();
}
finally
{
try
{
oos.close();
}

catch (IOException e)
{

e.printStackTrace();
}
}

ObjectInputStream ois =null;
ObjectInputStream ois1 =null;

try
{
ois = new ObjectInputStream(new FileInputStream("D:\\Stud.ser"));
Student ss =(Student) ois.readObject();
Student ss1 =(Student) ois.readObject();
Student ss2 =(Student) ois.readObject();
Student ss3 =(Student) ois.readObject();
Student ss4 =(Student) ois.readObject();

//storing to TreeSet for Merging to a new file ..............

stt.add(ss);
stt.add(ss1);
stt.add(ss2);
stt.add(ss3);
stt.add(ss4);

System.out.println("Objectas added !");

//System.out.println(ss1+ " " +ss2 + " " + ss3);
}




catch (FileNotFoundException e)
{

e.printStackTrace();
}

catch (IOException e)
{

e.printStackTrace();
}

catch (ClassNotFoundException e)
{

e.printStackTrace();
}

finally
{
try
{
ois.close();
}

catch (IOException e)
{

e.printStackTrace();
}





//------------File Merging -----------------------------------------------------




ObjectOutputStream oosMerge = null;

try
{

oosMerge = new  ObjectOutputStream(new FileOutputStream("D:\\StudMerge.ser"));
Iterator<Student> itr = stt.iterator();
     

while(itr.hasNext())
       {
        Student ss = itr.next();
        oosMerge.writeObject(ss);
         
       }
     
       System.out.println(oosMerge);
     
       //Retrive from oosMerge file and print the data
     

   ois1 = new ObjectInputStream(new FileInputStream("D:\\StudMerge.ser"));
 
Student ss =(Student) ois1.readObject();
Student ss1 =(Student) ois1.readObject();
Student ss2 =(Student) ois1.readObject();
Student ss3 =(Student) ois1.readObject();
Student ss4 =(Student) ois1.readObject();

System.out.println(ss + "  " + ss1 + "  " + ss2 + "  " + ss3 + " " + ss4);


}

catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

catch (ClassNotFoundException e)

{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}


}
}

}

DataInputTest

package iopack;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataInputTest {

public static void main(String[] args)
{
int x= 20;
float f = 10.5f;
double d = 25.4;
DataOutputStream dos = null;
DataInputStream dis = null;


try
{
dos = new DataOutputStream(new FileOutputStream("D:\\mailto.txt"));
dos.writeInt(x);
dos.writeFloat(f);
dos.writeDouble(d);
System.out.println("Write Completo !");


}



catch (FileNotFoundException e)
{
e.printStackTrace();
}

catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

finally
{
try
{
dos.close();
}

catch (IOException e)
{
e.printStackTrace();
}
}

try
{
dis =new DataInputStream(new FileInputStream("D:\\mailto.txt"));
int x1= dis.readInt();
float f1 = dis.readFloat();
double d1 = dis.readDouble();

System.out.println(x1);
System.out.println(f1);
System.out.println(d1);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

finally
{
try
{
dis.close();
System.out.println("dis closed !");
}

catch (IOException e)
{

e.printStackTrace();
}
}


}

}

FileReadWriteTest - File IO

package iopack;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileReadWriteTest
{

public static void main(String[] args)
{
File f = new File("D:\\exp.txt");
File f1 = new File("D:\\mailto.txt");


System.out.println(f.exists());
FileInputStream fis = null;
FileOutputStream fos = null;

try
{
fos = new FileOutputStream(f1);
fis= new FileInputStream(f);
int ch = 0;
char chr = 'm' ;
while ((ch = fis.read()) !=-1)
{

fos.write(chr);
chr= (char)ch;
System.out.print((char)ch);
}


}

catch (FileNotFoundException e)
{
e.printStackTrace();
}


catch(NullPointerException n)
{
n.printStackTrace();
}

catch (IOException e)
{
e.printStackTrace();
}

finally
{
try
{
fis.close();
System.out.println("Closed");

}
catch (IOException e)
{
e.printStackTrace();
}
}

}

}

File IO - SerializationTest

package iopack;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import pkg.Student;

public class SerializationTest {

public static void main(String[] args)
{
Student s = new Student("Ram",5005,"MCA",5);
ObjectOutputStream oos = null;

try
{
oos = new ObjectOutputStream(new FileOutputStream("D:\\Stud.ser"));
oos.writeObject(s);

System.out.println("Write done");

}

catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
oos.close();
}

catch (IOException e)
{

e.printStackTrace();
}
}

ObjectInputStream ois =null;

try
{
ois = new ObjectInputStream(new FileInputStream("D:\\Stud.ser"));
Student ss =(Student) ois.readObject();

System.out.println(ss);
}

catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

finally
{
try
{
ois.close();
}

catch (IOException e)
{

e.printStackTrace();
}

}
}

}

Comma Separated File

 Comma Separated File.

package exercise;

import java.util.Arrays;
import java.util.StringTokenizer;
//Input Format
//JAVA 14, JEE 10, JME 12

public class CommaSepFile {

    public static void main(String[] args) {
        int i=0,j=0,k=0,tot=0,d=1,c=0,remai=0;
        boolean p=false;
        String s1[]=new String[6];
        Integer a[]=new Integer[10];
        String s3=Arrays.toString(args);
    StringTokenizer s=new StringTokenizer(s3,",");
   
    while(s.hasMoreElements())
    {
         s1[i]  = (String) s.nextElement();
        //System.out.println(x);
        i++;
    }
    s1[0]=s1[0].substring(1);
    //System.out.println(s1.length);
   
    for(String s2:s1)
    {
       
        if(i%2==0)
        {
            s1[j]=s2;
            System.out.print(s1[j]+" ");
            j++;
        }else
        {
            String q=s2.substring(1,3);
            a[k]=Integer.parseInt(q);
            System.out.print(a[k]+" ");
            tot=tot+a[k];
            k++;
       
           
        }
        a[k]=0;
        //System.out.print(s2+" ");
        //System.out.println("");
        i++;
        }
    System.out.println("\n\nSubject Time  Day");
k=0;
    while(tot>0)
    {
        if(p==false&&a[k]!=0)
        {
            if(a[k]>=8)
            {
                a[k]=a[k]-8;
            System.out.println(s1[k]+"     8     " +d);
            d++;
            tot=tot-8;
           
        }else
        {
            c=a[k];
            System.out.println(s1[k]+"     "+c+"     "+d);
            k++;
            tot=tot-c;
            p=true;
            }
        }else if(a[k]!=0&&p==true)
        {
            remai=8-c;
            System.out.println(s1[k]+"     "+remai+"     "+d);
            a[k]=a[k]-remai;
            tot=tot-remai;
            d++;
            p=false;
        }else
        {
            k++;
        }
    }
    }

}