Friday 21 December 2018

Java Tutorial : Text File As Database Table | Episode 3 | Update Data Fr...



Code :-
import java.io.*;
import java.util.*;
class update_file
{
    public static void main(String ar[])
    {
        update_file uf=new update_file();
        uf.update();       
    }
    public void update()
    {
        /////////phase 1
        System.out.println("Enter the ID of employee that you want to update.....");
        Scanner sc=new Scanner(System.in);
        int id_to_change=sc.nextInt();
        String to_change_column_names[]={"first name:","last name:","Salary:","Location:"};
        int yes_no[]=new int[4];
        String to_update[]=new String[4];

        System.out.println("Select the columns that you want to update (0: No/1: Yes)");
        for(int i=0;i<4;i++)
        {
            System.out.println(to_change_column_names[i]+"");
            int temp=sc.nextInt();
            yes_no[i]=temp;
        }

        ////////////////phase2
        System.out.println("_______________________________________________________");
        System.out.println("Add new values to the columns");
        for(int i=0;i<4;i++)
        {
            System.out.println(to_change_column_names[i]+"");
            if(yes_no[i]==1)
            {
                Scanner sc1=new Scanner(System.in);
                String temp_val=sc1.nextLine();
                to_update[i]=temp_val;   
                System.out.println();
            }
            else
            {
                System.out.println("Cannot be changed");
            }
        }

        //////////////////phase 3
        StringBuffer sb=new StringBuffer();
        try
        {
            BufferedReader br=new BufferedReader(new FileReader("Database.txt"));
            String s="";
            while((s=br.readLine())!=null)
            {
                String data[]=new String[6];
                data=s.split(",");
                if(id_to_change==Integer.parseInt(data[0]))
                {
                    String row=data[0]+",";
                    for(int i=0;i<4;i++)
                    {
                        if(yes_no[i]==1)
                        {
                            row=row+to_update[i]+",";
                        }
                        else
                        {
                            row=row+data[i+1]+",";
                        }

                    }
                    sb.append(row);
                    sb.append("\n");
                }
                else
                {
                    sb.append(s);
                    sb.append("\n");
                }
            }
            //System.out.println(sb.toString());

        ////////////////// phase 4
        File f=new File("Database.txt");
        PrintWriter pw=new PrintWriter(new FileOutputStream(f,false));
        pw.print(sb.toString());
        pw.close();
        }
        catch(Exception e)
        {

        }
    }
}

Hello guys, this is unpossible pog and and on youtube, this guy requested
me to make a code in java to perform update operation.
If
you want to skip intro, then jump to this time.
For
lot of you guys who don’t know, i made tutorials on how to use text
file as database using java.

I only made videos of inserting and
displaying data but not on how to update data.
When
I was trying to write a program however, I found out that it is vary
complex.
Let
me explain you.
SO
this is my table structure with 5 columns.
And
here as you can see, that there can be multiple “SET” keywords
that can be applied to multiple columns, and same goes to “WHERE”
clouse, where there can be multiple condition and other complications
like “AND-OR” and “IN” and “EQUALS TO”.
Now
I shall explain the simplest one, in that there is only unique ID in
condition, but you can select multiple columns.
I
solved the problem of selecting which columns will be updated by
flagging is either as 0 or 1.
If
u didn’t understand it, dont worry, you will understand when I
start writing a code.



Now,
in previous tutorials, we wrote a code on now to insert and display
data from text file. So let me just run it.
As
you can see, the data is entered in text file.



Because
the udate code is huge, I am going to create a new .java file, save
it as the same location where the text file is.
Make
a class and main function.
Now,
there are 4 phases,
In
first phase, we will get which ID will be in where condition and
create some arrays.
Here,
we are not updating ID, because in live projects, we don’t often do
that.
The
Array yes_no are made to give the flag to columns that is O or 1.
In
the for loop, we are asking users to select columns to be updated
Lets
check it, it should be an array.



First,we
will enter ID, then we will type either 0 or 1.
So
far so good.



Now
in second phase,we shall ask user to enter the value of columns which
user want to update.
We
shall store that in “to_change_clumn_names” array.
If
the flag is 1, then , we shall create scanner variable, and insert
values.
In
else, we shall tell user that you can not change this value.



As
you can see, the program is denying user because of the 0 flag he
gave.



In
phase three, we will modify data but won’t upload it in text
file. It will be in phase 4.
But
now in phase 3, we shall get data from text file,now from here, it
gets more complex.
We
shall need, a stringbuffer variable the store the modified value.
In
abstract word, in phase it, we go to each row, and find out whether
it’s ID matches with ID that we enter, if no, then we skip the
change, but if YES, then we find which columns should be updated and
swap their values.
We
append those value in StringBuffer.



Now
lets run it,
Actually
I forgeot to append, when flag is 1.



Now
in phase 3, we are writing the data in text file, it is vary easy.
Make
sure to close the printwriter, otherwise the file won’t be updated.
Lets
run the program and check in text file, whether or not the data has
been modified.
We
shall the value of employee where his ID is 1.



To
confirm it again, we shall the information of ID equals 2.



Thnak
you for watching, Dont forget to like share and subscribe.