ഒക്‌ടോബർ 27, 2011

File Handling with Tables




import java.sql.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane; *//*
SEE  BUTTON
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
try
{
Class.forName("java.sql.DriverManager");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/telephone","root","firoz");
Statement stmt = (Statement) con.createStatement();
String query = "SELECT * from dir" + ";";
ResultSet rs = stmt.executeQuery(query);
while(rs.next())
{
int  a=Integer.parseInt(rs.getString("did"));
String b=rs.getString ("ph");
String c=rs.getString ("dname");
DefaultTableModel m=(DefaultTableModel)jTable1.getModel();
Object [] ob={a,c,b};
m.addRow(ob);
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog (this, e.getMessage());
e.printStackTrace();
}      
    }

netbeans sql












import java.sql.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane; *//*
SEE  BUTTON
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
try
{
Class.forName("java.sql.DriverManager");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/telephone","root","firoz");
Statement stmt = (Statement) con.createStatement();
String query = "SELECT * from dir" + ";";
ResultSet rs = stmt.executeQuery(query);
while(rs.next())
{
int  a=Integer.parseInt(rs.getString("did"));
String b=rs.getString ("ph");
String c=rs.getString ("dname");
DefaultTableModel m=(DefaultTableModel)jTable1.getModel();
Object [] ob={a,c,b};
m.addRow(ob);
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog (this, e.getMessage());
e.printStackTrace();
}      
    }

ഒക്‌ടോബർ 26, 2011

All about File Handling ( CS students text File )


//program to write string to the file
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
void main()
{   char text[100];
    clrscr();
      ofstream ofile("firoz.txt",ios::app);
      cout<<"enter text( press # for exit)\n";
      cin.getline(text,100,'#');
      ofile<<text;
      getch();
}
//program to display the content by line by line
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
void main()
{ char text[100];
clrscr();
ifstream ifile("firoz.txt");
if(!ifile)
{ cout<<"cannot open";
getch();
exit(0);
}
while(!ifile.eof())
{ ifile.getline(text,100);
cout<<text<<"\n";
}
getch();
}
//program to display the content char by char
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
void main()
{   clrscr();
char t;
ifstream ifile("firoz.txt");
if(!ifile)
{ cout<<"can not open";
getch();
exit(0);
}
while(ifile)
{ ifile.get(t); cout<<t;
}
getch();
}
//program to display the content by word by word
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
void main()
{ char text[100];
clrscr();
ifstream ifile("firoz.txt");
if(!ifile)
{ cout<<"cannot open";
getch();
exit(0);
}
while(!ifile.eof())
{ ifile>>text;
cout<<text<<"\t";
}
getch();
}
//for removing a file
#include<stdio.h>
#include<conio.h>
void main()
{ remove("firoz.txt");
getch();
}
//for renaming a file
#include<stdio.h>
#include<conio.h>
void main()
{ rename("firoz.txt","roja.txt");
getch();
}
//data file handiling using class
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
class student
{ char name[20];
int reg;
int mark;
public:
void read();
void disp();
int retreg()
{ return reg;
}
}s;
void student::read()
{
cout<<"enter name";
gets(name);
cout<<"enter regno";
cin>>reg;
cout<<"enter mark";
cin>>mark;
}
void student::disp()
{
cout<<"\nname:"<<name;
cout<<"\nregno:"<<reg;
cout<<"\nmark:"<<mark;
}

void main()
{
clrscr();
int z,sreg;char se;
cout<<"\n\t\tmenu";
cout<<"\n\t\t1. read";
cout<<"\n\t\t2. disp";
cout<<"\n\t\t3. search by reg";
do{
cout<<"\n enter UR selection";
cin>>z;
switch(z)
{   case 1:
        ofstream ifile("stud.dat",ios::app|ios::binary);
if(!ifile)
{
cout<<"cannot open";
getch();
exit(0);
}
cout<<"\nenter details of student\n";
s.read();
ifile.write((char*)&s,sizeof(s));
ifile.close();
break;
case 2:
cout<<"all details\n";
    ifstream ofile("stud.dat",ios::binary|ios::out);
ofile.read((char*)&s,sizeof(s));
while(!ofile.eof())
{  s.disp();
ofile.read((char*)&s,sizeof(s));
}               ofile.close();
break;
case 3:
cout<<"enter rg no";
cin>>sreg;
ifstream sfile("stud.dat",ios::binary|ios::out);
sfile.read((char*)&s,sizeof(s));
while(!sfile.eof())
{
if(sreg==s.retreg())
s.disp();
sfile.read((char*)&s,sizeof(s));
}   sfile.close();
break;
}
cout<<"do U want to continue";
cin>>se;
}while(se=='y');
getch();
}
//data file handiling using structure

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
struct student
{ char name[20];
int reg;
int mark;
}s;
void main()
{ clrscr();
int z,sreg;char se;
cout<<"\n\t\tmenu";
cout<<"\n\t\t1. read";
cout<<"\n\t\t2. disp";
cout<<"\n\t\t3. search by reg";
do{
cout<<"\n enter UR selection";
cin>>z;
switch(z)
{   case 1:
ofstream ifile("stud.dat",ios::app|ios::binary);
if(!ifile)
{cout<<"cannot open";
getch();
exit(0);
}
cout<<"\nenter details of student\n";
cout<<"enter name";
gets(s.name);
cout<<"enter regno";
cin>>s.reg;
cout<<"enter mark";
cin>>s.mark;
ifile.write((char*)&s,sizeof(s));
ifile.close();
break;
case 2:
cout<<"all details\n";
ifstream ofile("stud.dat",ios::binary|ios::out);
ofile.read((char*)&s,sizeof(s));
while(!ofile.eof())
{  cout<<"\nname:"<<s.name;
  cout<<"\nregno:"<<s.reg;
  cout<<"\nmark:"<<s.mark;
ofile.read((char*)&s,sizeof(s));
}               ofile.close();
break;
case 3:
cout<<"enter rg no";
cin>>sreg;
ifstream sfile("stud.dat",ios::binary|ios::out);
sfile.read((char*)&s,sizeof(s));
while(!sfile.eof())
{
if(sreg==s.reg)
{   cout<<"\nname:"<<s.name;
  cout<<"\nregno:"<<s.reg;
  cout<<"\nmark:"<<s.mark;}
sfile.read((char*)&s,sizeof(s));
}   sfile.close();
break;
}
cout<<"do U want to continue";
cin>>se;
}while(se=='y');
getch();
}
//file handiling with edit & delete

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
class student
{ char name[20];
int reg, mark;
public:
void read();
void disp();
void edit();
int retreg()
{ return reg;
}
}s;
void student::read()
{ cout<<"enter name";
gets(name);
cout<<"enter regno";
cin>>reg;
cout<<"enter mark";
cin>>mark;
}
void student::disp()
{ cout<<"\nname:"<<name;
cout<<"\nregno:"<<reg;
cout<<"\nmark:"<<mark;
}
void student::edit()
{   char nname[20];
     int nreg,nmark;
cout<<"\nenter new name(if no change pressenter key)";
gets(nname);
cout<<"\nenter new reg no(if no change press -1)";
cin>>nreg;
cout<<"\nenter new mark no(if no change press -1)";
cin>>nmark;
if (strlen(nname)!=0)
strcpy(name,nname);
if(nreg!=-1)
reg=nreg;
if(nmark!=-1)
mark=nmark;
}
void main()
{
clrscr();
int z,sreg;char se;
do{
cout<<"\n\t\tmenu";
cout<<"\n\t\t1. read";
cout<<"\n\t\t2. disp";
cout<<"\n\t\t3. search by reg";
cout<<"\n\t\t4. edit";
cout<<"\n\t\t5. delete";
cout<<"\n enter UR selection";
cin>>z;
switch(z)
{   case 1:
ofstream ifile("stud.dat",ios::app|ios::binary);
if(!ifile)
{cout<<"cannot open";
getch();
exit(0);
}
cout<<"\nenter details of student\n";
s.read();
ifile.write((char*)&s,sizeof(s));
ifile.close();
cout<<"\nrecord saved\n";
break;
case 2:
cout<<"\nall details\n";
ifstream ofile("stud.dat",ios::binary|ios::out);
ofile.read((char*)&s,sizeof(s));
while(!ofile.eof())
{  s.disp();
ofile.read((char*)&s,sizeof(s));
}               ofile.close();
break;
case 3:
cout<<"\nenter reg no:";
cin>>sreg;
ifstream           sfile("stud.dat",ios::binary|ios::out);
sfile.read((char*)&s,sizeof(s));
while(!sfile.eof())
{
if(sreg==s.retreg())

s.disp();

sfile.read((char*)&s,sizeof(s));
}   sfile.close();
break;
case 4:
cout<<"enter rg no";
cin>>sreg;
ifstream efile("stud.dat",ios::binary|ios::out);
if(!efile)
{ cout<<"can not";
getch();
exit(0);
}
         ofstream tfile("tstud.dat",ios::binary|ios::app);
if(!tfile)
{ cout<<"can not";
getch();
exit(0);
}
efile.read((char*)&s,sizeof(s));
while(!efile.eof())
{
if(sreg==s.retreg())
{
s.disp();
cout<<"enter new details";
s.edit();
tfile.write((char*)&s,sizeof(s));
}
else
tfile.write((char*)&s,sizeof(s));
efile.read((char*)&s,sizeof(s));
}   efile.close();tfile.close();
remove("stud.dat");
rename("tstud.dat","stud.dat");
remove("tstud.dat");
cout<<"\nrecord edited\n";
break;
case 5:
cout<<"enter rg no";
cin>>sreg;
ifstream dfile("stud.dat",ios::binary|ios::out);
if(!dfile)
{ cout<<"can not";
getch();
exit(0);
}
       ofstream dtfile("tstud.dat",ios::binary|ios::app);
if(!dtfile)
{ cout<<"can not";
getch();
exit(0);
}
dfile.read((char*)&s,sizeof(s));
while(!dfile.eof())
{
if(sreg==s.retreg())
{
s.disp();
cout<<"\nrecord deleted\n";
}
else
{
dtfile.write((char*)&s,sizeof(s));
}
dfile.read((char*)&s,sizeof(s));
}   dfile.close();dtfile.close();
remove("stud.dat");
rename("tstud.dat","stud.dat");
remove("tstud.dat");
break;
}
cout<<"\ndo U want to continue(y/n)";
cin>>se;
clrscr();
}while(se=='y');
getch();
}
// for writing by using seekp
s.read();
file.seekp(((count-1)*sizeof(s)),ios::beg);
file.write((char*)&s,sizeof(s));
cout<<"\nrecord saved\n";
// for tellp tell the position in writing time
             cout<<file.tellp();
//output is current count*size of class
// for display by using seekg
file.seekg(((rec-1)*sizeof(s)),ios::beg);
file.read((char*)&s,sizeof(s));
s.disp();
// for tellp tell the position in reading time
             cout<<file.tellg();
//output is current count*size of class
                dos shel commands
//dos commands
 copy con <filename>         //for creating
                                             (^ z for saving)
 type <filename>               // for displaying
del <filename>                  // for deleting
exit                                   // for closing
dir *.*                               // for all files


ഒക്‌ടോബർ 20, 2011

Front End Back End Connectivity Netbeans Java -mysql (For IP Students Text file )



















import java.sql.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;
ADD BUTTON
 private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)
{
int x=JOptionPane.showConfirmDialog(null,"Do You Wnat to Save This Record");
  if (x==JOptionPane.YES_OPTION)
  {
      try{
Class.forName("java.sql.DriverManager");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/db1",
"root","firoz");
Statement stmt = (Statement) con.createStatement();
int a=Integer.parseInt(jTextField1.getText());
String b=jTextField2.getText();
String c=jTextField3.getText();
String query = "insert into directory values (" + a +",'"+ b +"','" + c+"')" + " ;";
  stmt.executeUpdate(query);
JOptionPane.showMessageDialog (this, "Record Saved");
}
catch (Exception e)
{
JOptionPane.showMessageDialog (this, e.getMessage());
e.printStackTrace();
}
}

 }


UPDATE BUTTON
 private void jButton6ActionPerformed(java.awt.event.ActionEvent evt)
{
int x=JOptionPane.showConfirmDialog(null,"Do You Wnat to Update This Record");
  if (x==JOptionPane.YES_OPTION)
  {
      try
{
Class.forName("java.sql.DriverManager");
Connection con = (Connection)
DriverManager.getConnection
("jdbc:mysql://localhost:3306/db1",
"root","firoz");
Statement stmt = (Statement) con.createStatement();
int a=Integer.parseInt(jTextField1.getText());
String b=jTextField2.getText();
String c=jTextField3.getText();
String query = "update   directory set dname='" +b + "',ph='" +  c + " ' WHERE did=" + a + ";";
  stmt.executeUpdate(query);

JOptionPane.showMessageDialog (this, "Record Saved");
}
catch (Exception e)
{
JOptionPane.showMessageDialog (this, e.getMessage());
e.printStackTrace();
}
  }    
 }
DELETE BUTTON
 private void jButton7ActionPerformed(java.awt.event.ActionEvent evt)
{
int x=JOptionPane.showConfirmDialog(null,"Do You Wnat to Delete This Record");
  if (x==JOptionPane.YES_OPTION)
  {
  try
{
    Class.forName("java.sql.DriverManager");
      Connection con = (Connection)
    DriverManager.getConnection
    ("jdbc:mysql://localhost:3306/db1",
    "root","firoz");
    Statement stmt = (Statement) con.createStatement();
    int a=Integer.parseInt(jTextField1.getText());
    String query = " delete from directory where did = " +a +";";
    stmt.executeUpdate(query);
    JOptionPane.showMessageDialog (this, "Record Deleted");
    jButton5.doClick();
    }

    catch (Exception e)
    {
    JOptionPane.showMessageDialog (this, e.getMessage());
    e.printStackTrace();
    }
  }
    }
SEARCH NAME
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{                                      
try
{
Class.forName("java.sql.DriverManager");
Connection con = (Connection)
DriverManager.getConnection
("jdbc:mysql://localhost:3306/db1","root","firoz");
Statement stmt = (Statement) con.createStatement();
String cId=JOptionPane.showInputDialog("Enter PHONE NO");
String query = "SELECT * FROM directory WHERE ph="+ cId + ";";
ResultSet rs = stmt.executeQuery(query);
if (!rs.first())
{
JOptionPane.showMessageDialog(this,"Sorry! No such Customer");
}
else
{
jTextField1.setText(rs.getString("did"));
jTextField2.setText(rs.getString ("ph"));
jTextField3.setText(rs.getString ("dname"));

}
}
catch (Exception e)
{
JOptionPane.showMessageDialog (this, e.getMessage());
e.printStackTrace();
}
  }                                      
SEARCH PHONE
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{                                      
try
{
Class.forName("java.sql.DriverManager");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/db1","root","firoz");
Statement stmt = (Statement) con.createStatement();
String c= JOptionPane.showInputDialog("Enter NAME");
        String query = "SELECT * FROM directory WHERE dname ='"+ c + "';";
ResultSet rs = stmt.executeQuery(query);
if (!rs.first())
{
JOptionPane.showMessageDialog(this,"Sorry! No such Customer");
}
else
{
jTextField1.setText(rs.getString("did"));
jTextField2.setText(rs.getString ("dname"));
jTextField3.setText(rs.getString ("ph"));

}
}
catch (Exception e)
{
JOptionPane.showMessageDialog (this, e.getMessage());
e.printStackTrace();
}      
    }
MOVE FIRST
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt)
{
try
{
Class.forName("java.sql.DriverManager");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/db1","root","firoz");
Statement stmt = (Statement) con.createStatement();
String query = "SELECT * from directory " + ";";
ResultSet rs = stmt.executeQuery(query);
if (!rs.first())
{
JOptionPane.showMessageDialog(this,"Sorry! No such Customer");
}
else
{
    rs.first();
jTextField1.setText(rs.getString("did"));
jTextField2.setText(rs.getString ("ph"));
jTextField3.setText(rs.getString ("dname"));
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog (this, e.getMessage());
e.printStackTrace();
}    
    }
MOVE  LAST
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt)
{
try
{
Class.forName("java.sql.DriverManager");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/db1","root","firoz");
Statement stmt = (Statement) con.createStatement();
String query = "SELECT * from directory " + ";";
ResultSet rs = stmt.executeQuery(query);
if (!rs.first())
{
JOptionPane.showMessageDialog(this,"Sorry! No such Customer");
}
else
{
    rs.last();
jTextField1.setText(rs.getString("did"));
jTextField2.setText(rs.getString ("ph"));
jTextField3.setText(rs.getString ("dname"));

}
}
catch (Exception e)
{
JOptionPane.showMessageDialog (this, e.getMessage());
e.printStackTrace();
}          
}
EXIT
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{                                      
          System.exit(0);
 }                                      

CLEAR  FIELDS
 private void jButton5ActionPerformed(java.awt.event.ActionEvent evt)
{
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");      
  }