Vous êtes sur la page 1sur 22

CSE2004 Database Management Systems

Digital Assignment - 2
USING THEN COMPARING, FLAT FILE PROCESSING AND
DBMS APPROACH TO ACCESS AND MANIPULATE DATA

GAURI TYAGI
15BCE0126

COURSE INSTRUCTOR
Dr. Anthoniraj A

Honor Code
I certify that I have properly cited any material taken from other sources and have obtained
permission for any copyrighted material included in this report. I take full responsibility for any code
submitted as part of this assignment and the contents of this report.

Gauri Tyagi (15BCE0126)

Page 2 of 22

Abstract
This assignment is an attempt to compare the DBMS approach head-to-head with Flat-File
Processing by using them to access and manipulate a data set. The goal was to understand the
workings of data retrieval and manipulation by the two methods.

1. Introduction
Resource Software Specifications
o Data Set

Data Set Title: Women and currently married women by present age, number of
births last year by sex and birth order - Tamil Nadu
Link: http://tinyurl.com/da2-dataset
Released under: National Data Sharing and Accessibility Policy (NDSAP)
Contributor: Ministry of Home Affairs, Registrar General and Census
Commissioner
Published on: September 07,2015

o Programming Language

Programming Language: Java


Version:7
Paradigm: Object-Oriented

o DBMS Software

DBMS Software: MySQL


Version: 5.1
Type: RDBMS
Author(s): MySQL AB
Developer(s): Oracle Corporation
Written in: C, C++

Note: Source Code has been commented in black bold.


SQL Queries are given in red bold.

Page 3 of 22

2. Flat File Processing


3.
GUI Design view

User can
access choose
records in the
input range

Accesses
complete data
set by reading
open file

Clears the
input/output
data fields
Accesses
complete data
set through
arrays.

The csv file is read line by line.


Each line is split around the ,
delimiter. Each token is stored in
a 2D array

SOURCE CODE

Page 4 of 22

import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;

//imports

public class DA2_f extends javax.swing.JFrame {


/** Creates new form DA2_f */
public DA2_f() {
initComponents();
}

public static void main(String args[]) {


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DA2_f().setVisible(true);
}
});
}

//code for View complete data file #1 button


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
BufferedReader in = new BufferedReader
(new FileReader("C:\\Users\\TYAGI\\Downloads\\a1.csv"));
String str;
while ((str = in.readLine()) != null)
{

Page 5 of 22

jTextArea1.append(str);
//reads lines until no line==null
jTextArea1.append("\n");
}
System.out.println(str);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

Page 6 of 22

//code for View complete data file #2 button

Try
{
BufferedReader br = new BufferedReader(new FileReader(new File("F:\\Study\\SEM
3\\DBMS\\DA2\\a.csv")));
String line = "";
int rows=0;
String data[][] = new String[1000][13];
while ((line = br.readLine()) != null)
{
String value[] = line.split(",", 13); //the line must be splited and stored in a 1D array
for (int j=0; j<13; j++)
data[rows][j] = value[j];
//transfer of our array of splited elements one by
one to our 2D array
rows++;
}
for(int i=0;i<rows;i++)
{
for(int j=0;j<13;j++)
{
jTextArea1.append(data[i][j]+'\t');
}
jTextArea1.append("\n");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e.getMessage());
}

Page 7 of 22

//code for View n records button


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
int m=Integer.parseInt(jTextField2.getText());
int n=Integer.parseInt(jTextField1.getText());

//from record m
//to record n

BufferedReader br = new BufferedReader(new FileReader(new File("F:\\Study\\SEM


3\\DBMS\\DA2\\a.csv")));
String line = "";
int rows=0;

Page 8 of 22

String data[][] = new String[1000][13];

while ((line = br.readLine()) != null)


{
String value[] = line.split(",", 13); //the line must be split and stored in 1Darray
for (int j=0; j<13; j++)
data[rows][j] = value[j];
//transfer of our array of split elements
one by one to our 2D array
rows++;
}
for(int i=0;i<1;i++)
//prints the column headings irrespective of the range
selected by the user
{
//i= row counter
for(int j=0;j<13;j++)
//j=column counter
{
jTextArea1.append(data[i][j]+'\t');
}
jTextArea1.append("\n");
}
for(int i=m;i<n;i++)
//prints the records in the range input by the user
{
//i=row counter
for(int j=0;j<13;j++)
//j=column counter
{
jTextArea1.append(data[i][j]+'\t');
}
jTextArea1.append("\n");
}

}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e.getMessage());
}

Page 9 of 22

Page 10 of 22

//code for reset button


private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.setText("");
jTextField1.setText("");
jTextField2.setText("");

//clears input data

Page 11 of 22

4. DBMS Manipulation
<<Processing your DBMS with same Programming Language, Add necessary source code,
SQL Queries and output/screenshots>>
NOTE: All SQL queries have been marked in red

//viewing complete data


GUI Design

Table
Component

Outputs complete data from


table da2 in database test
using Select * query and
adds to the table component
with a while loop

Page 12 of 22

Source Code
//imports
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import javax.swing.JOptionPane;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


DefaultTableModel t1=(DefaultTableModel)jTable1.getModel();
try
{
Class.forName("java.sql.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
Page 13 of 22

Statement stmt=con.createStatement();
String q1="Select * from da2;";
ResultSet rs=stmt.executeQuery(q1);
while(rs.next())
{
String a=rs.getString(1);
String b=rs.getString(2);
String c=rs.getString(3);
String d=rs.getString(4);
String e=rs.getString(5);
String f=rs.getString(6);
String g=rs.getString(7);
String h=rs.getString(8);
String i=rs.getString(9);
String j=rs.getString(10);
String k=rs.getString(11);
String l=rs.getString(12);
String m=rs.getString(13);
t1.addRow(new Object[]{a,b,c,d,e,f,g,h,i,j,k,l,m});
}
rs.close();
stmt.close();
con.close();
}
catch(Exception e)
{}
}

Page 14 of 22

//inserting new record


GUI design

Source Code
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("java.sql.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
Statement stmt=con.createStatement();
String a_Name=jTextField1.getText();
String a_Type=jTextField2.getText();
String age_Group=jTextField3.getText();
String t_Women=jTextField4.getText();
String t_CMWomen=jTextField5.getText();
String ly_Male=jTextField6.getText();
String ly_Female=jTextField7.getText();
String ly_O1=jTextField8.getText();
String ly_O2=jTextField9.getText();
Page 15 of 22

String ly_O3=jTextField10.getText();
String ly_O4=jTextField11.getText();
String district_Code=jTextField12.getText();
String state_Code="33";

String q1="insert into da2


values('"+(state_Code)+"','"+(district_Code)+"','"+(a_Name)+"','"+(a_Type)+"','"+(age_Group)
+"','"+(t_Women)+"','"+(t_CMWomen)+"','"+(ly_Male)+"','"+(ly_Female)+"','"+(ly_O1)+"','"
+(ly_O2)+"','"+(ly_O3)+"','"+(ly_O4)+"');";
stmt.executeUpdate(q1);
JOptionPane.showMessageDialog(this, "Record inserted");
} catch(Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
Result

Page 16 of 22

//viewing custom record

On the mouse click of the value in List 3, the


selected values from the lists are put in a
where clause to retrieve correct data from the
table da2.

GUI Design

Fetches data from the


table da2 and puts in
the corresponding
lists.

Clears all lists and text fields

The user selects one value


from each list. Once all lists
have a selected value, the
corresponding data is
displayed in the appropriate
text fields

Source code
//code for populate list button
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

DefaultListModel m1=(DefaultListModel)jList1.getModel();
DefaultListModel m2=(DefaultListModel)jList2.getModel();
DefaultListModel m3=(DefaultListModel)jList3.getModel();

Page 17 of 22

try {
Class.forName("java.sql.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
Statement stmt=con.createStatement();

//creates connection with MySQL server

String q1="select distinct Area_Name from da2;";


ResultSet rs=stmt.executeQuery(q1);
while(rs.next())
{
m1.addElement(rs.getString("Area_Name"));
}
String q2="select distinct Total_Rural_Urban from da2;";
rs=stmt.executeQuery(q2);
while(rs.next())
{
m2.addElement(rs.getString("Total_Rural_Urban"));
}

String q3="select distinct Present_Age from da2;";


rs=stmt.executeQuery(q3);
while(rs.next())
{
m3.addElement(rs.getString("Present_Age"));
}
} catch(Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
Page 18 of 22

private void jList3MouseClicked(java.awt.event.MouseEvent evt) {


DefaultListModel m1=(DefaultListModel)jList1.getModel();
DefaultListModel m2=(DefaultListModel)jList2.getModel();
DefaultListModel m3=(DefaultListModel)jList3.getModel();

try {
Class.forName("java.sql.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
Statement stmt=con.createStatement();

//selected values are stored in a string variable.


String Area_Name= (String)jList1.getSelectedValue();
String Total_Rural_Urban= (String)jList2.getSelectedValue();
String Present_Age= (String)jList3.getSelectedValue();

String query="Select * from da2 where (Area_Name='"+(Area_Name)+"' and


Total_Rural_Urban='"+(Total_Rural_Urban)+"' and Present_Age='"+(Present_Age)+"');";

ResultSet rs=stmt.executeQuery(query);

//text fields are filled with the appropriate attribute values

while (rs.next()) {
jTextField1.setText(rs.getString("Total_Women"));
jTextField2.setText(rs.getString("Currently_Married_Women"));
jTextField3.setText(rs.getString("Births_LY_Male"));
jTextField4.setText(rs.getString("Births_LY_Female"));
jTextField5.setText(rs.getString("Births_LY_Order_1"));
jTextField6.setText(rs.getString("Biths_LY_Order_2"));
Page 19 of 22

jTextField7.setText(rs.getString("Births_LY_Order_3"));
jTextField8.setText(rs.getString("Births_LY_Order_4"));
}

} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
}

//code on reset button


DefaultListModel m1=(DefaultListModel)jList1.getModel();
DefaultListModel m2=(DefaultListModel)jList2.getModel();
DefaultListModel m3=(DefaultListModel)jList3.getModel();

jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
jTextField7.setText("");
jTextField8.setText("");
m1.removeAllElements();
m2.removeAllElements();
m3.removeAllElements();

Page 20 of 22

Output

Page 21 of 22

5. Conclusion

(Personal Experience during this assignment)

Flat File Processing is cumbersome. Records in the CSV file had to be split, and
each token had to be transferred to a 2D string array.
Type conversion to be able to work with data is another problem
Although not implemented in this assignment, deletion will be a tedious process as
the huge data records will have to be shifted up and down the array to maintain
consistency.
There is no mapping between any two files. i.e.; any two dependent files are not
linked.
Data Dependence: . If the format of any of the file is changed, then the program for
processing this file needs to be changed. That is smallest change in the file affect all
the programs and need changes in all them.

If our requirement involves appending data, sequential access and little/no


concurrency, flat file processing is the way to go are the way to go.
On the other hand, when we require concurrency, non-sequential reading/writing,
atomicity, atomic permissions, our data is relational by nature, It is a better idea to go
with the DBMS approach.

6. References
>NA
---------------------x----------------------

Page 22 of 22

Vous aimerez peut-être aussi