Contenu connexe Similaire à CRUD(IN_UP_DEL) IN JAVA (20) CRUD(IN_UP_DEL) IN JAVA 1. -Payal Umraliya
DESCLAIMER : THIS CODE IS SIMPLE JAVA CODE WITH APPLET
package ACTUALDATA;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import javax.swing.JOptionPane;
import java.util.*;
/**
*
* @author PAYAL
*/
public class retaildetail extends JPanel implements ActionListener
{
JLabel logo,name,resiadd,contct,businessadd,bcontct,faxno,mob,status,children,income,pic,pgr;
JButton in,up,del,exit,f,n,l,p,srch,clr;
JTextField nm,radd,cnt,badd,photo,bcnt,fx,pc,pg,ms,noc,incm;
JTextArea radd1,badd1;
String t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,na;
Connection cn;
Statement s;
PreparedStatement insrt,dlt,serch,fst,nxt,prv,lst,ext,rst;
Font fnt;
JFrame jf;
//JTable jt;
ResultSet rs;
2. retaildetail r;
Vector cnames;
String[] rwdt;
public retaildetail()
{
//all labels
fnt=new Font("COMIC SANS SERIF",Font.BOLD,18);
logo=new JLabel("CHECKMATE RETAILERS (YEAR 2014)");
name=new JLabel("
NAME : ");
resiadd=new JLabel(" RESI. ADDRESS :");
contct=new JLabel("
CONTACT :");
businessadd=new JLabel("BUSI.ADDRESS :");
bcontct=new JLabel(" BUSI. CONTACT :");
faxno=new JLabel("
FAX NO :");
mob=new JLabel("
MOBILE :");
pgr=new JLabel("
PAGER :");
status=new JLabel("MARITALSTATUS :");
children=new JLabel("NO.OFCHILDREN :");
income=new JLabel("ANNUALINCOME :");
pic=new JLabel(" PHOTOGRAPH :");
//all textfields nm,radd,cnt,badd,photo,bcnt,fx,pc,pg,ms,noc,incm;
nm=new JTextField(15);
radd=new JTextField(15);
cnt=new JTextField(15);
badd=new JTextField(15);
bcnt=new JTextField(15);
3. fx=new JTextField(15);
pc=new JTextField(15);
pg=new JTextField(15);
ms=new JTextField(15);
noc=new JTextField(15);
incm=new JTextField(15);
photo=new JTextField(15);
// all btns in,up,del,exit,f,n,l,p,srch;
in=new JButton("INSERT");
up=new JButton("UPDATE");
del=new JButton("DELETE");
exit=new JButton("EXIT");
f=new JButton("FIRST");
n=new JButton("NEXT");
l=new JButton("LAST");
p=new JButton("PREV");
srch=new JButton("CLICK TO SEARCH CUSTOMER");
clr=new JButton("RESET");
// adding all logo,name,resiadd,contct,businessadd,bcontct,faxno,mob,children,income,pic;
//nm,radd,cnt,badd,photo,bcnt,fx,pc,pg,ms,noc,incm;
//in,up,del,exit,f,n,l,p,srch;
JLabel jl = new JLabel("================================================================");
add(logo);
add(jl);
add(srch);
JLabel jl3 = new JLabel("
");
5. JLabel jl2 = new JLabel("================================================================");
add(jl2);
add(in);
add(up);
add(del);
add(f);
add(n);
add(p);
add(l);
add(clr);
add(exit);
//end of adding
in.addActionListener(this);
up.addActionListener(this);
del.addActionListener(this);
srch.addActionListener(this);
f.addActionListener(this);
n.addActionListener(this);
l.addActionListener(this);
p.addActionListener(this);
exit.addActionListener(this);
clr.addActionListener(this);
}
//nm,radd,cnt,badd,photo,bcnt,fx,pc,pg,ms,noc,incm;
public void connection()
{
try {
6. Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection("jdbc:mysql://localhost/try", "root", "checkmate");
s = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
System.out.println("connected");
JOptionPane.showMessageDialog(jf,"!!!...WELCOME CHECKMATE(96)... !!!n ...YOUR DATABASE
CONNECTION IS ESTABLISHED....n ...YOU CAN ABLE NAVIGATE AND MANAGE ALL DATA OF YOUR CUSTOMERS
NOW...");
viewcheckmatedata();
} catch (SQLException e) {
System.out.println("sorry!!! some problems are occured in connectionn see details " + e);
} catch (ClassNotFoundException ce) {
System.out.println("class not found see detailsn " + ce);
} catch (Exception e) {
System.out.println("some problems are found see detailsn " + e);
}
}
void viewcheckmatedata()
{
try
{
rs=s.executeQuery("select * from cust");
rs.next();
nm.setText(rs.getString(1));
radd.setText(rs.getString(2));
cnt.setText(rs.getString(3));
badd.setText(rs.getString(4));
bcnt.setText(rs.getString(5));
fx.setText(rs.getString(6));
9. } catch (Exception e) {
System.out.println("some problems occured in either searching or viewing or navigation or resettingn " +
e);
}
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
try
{
if(str.equals("INSERT"))
{
String insql="insert into cust values(?,?,?,?,?,?,?,?,?,?,?,?)";
insrt=cn.prepareStatement(insql);
insrt.setString(1,nm.getText());//
insrt.setString(2,radd.getText());//
insrt.setInt(3,Integer.parseInt(cnt.getText()));
insrt.setString(4,badd.getText());//
insrt.setInt(5,Integer.parseInt(bcnt.getText()));
insrt.setInt(6,Integer.parseInt(fx.getText()));
insrt.setInt(7,Integer.parseInt(pc.getText()));
insrt.setString(8,pg.getText());//
insrt.setString(9,ms.getText());
insrt.setInt(10,Integer.parseInt(noc.getText()));
insrt.setInt(11,Integer.parseInt(incm.getText()));
insrt.setString(12,photo.getText());//
insrt.executeUpdate();
10. JOptionPane.showMessageDialog(jf,"1 ROW INSERTED");
System.out.println("Data insereted");
viewcheckmatedata();
}
else if(str.equals("UPDATE"))
{
String na = JOptionPane.showInputDialog("plz enter name which u want to edit");
nm.setText(na);
String insql="update cust set
address=?,contact=?,businessAddress=?,businessContact=?,fax=?,mobile=?,pager=?,maritalstatus=?,children=?,an
nualincome=?,photos=?where name=?";
insrt = cn.prepareStatement(insql);
insrt.setString(1, radd.getText());//
insrt.setInt(2, Integer.parseInt(cnt.getText()));
insrt.setString(3, badd.getText());//
insrt.setInt(4, Integer.parseInt(bcnt.getText()));
insrt.setInt(5, Integer.parseInt(fx.getText()));
insrt.setInt(6, Integer.parseInt(pc.getText()));
insrt.setInt(7, Integer.parseInt(pg.getText()));//
insrt.setString(8, ms.getText());
insrt.setInt(9, Integer.parseInt(noc.getText()));
insrt.setInt(10, Integer.parseInt(incm.getText()));
insrt.setString(11, photo.getText());//
insrt.setString(12,nm.getText());
insrt.executeUpdate();
JOptionPane.showMessageDialog(jf,"1 ROW UPDATED");
System.out.println("DATA UPDATED");
viewcheckmatedata();
11. }
else if(str.equals("DELETE"))
{
String na = JOptionPane.showInputDialog("enter the name for delete");
nm.setText(na);
System.out.println("query reading");
String insql = "delete from cust where name=?";
dlt = cn.prepareStatement(insql);
System.out.println("preparing");
dlt.setString(1,nm.getText());
System.out.println("deleting " + na);
System.out.println("exeecutes update statement");
dlt.executeUpdate();
JOptionPane.showMessageDialog(jf, "1 ROW DELETED");
System.out.println("DATA DELETED");
viewcheckmatedata();
}
else if(str.equals("CLICK TO SEARCH CUSTOMER"))//nm,radd,cnt,badd,photo,bcnt,fx,pc,pg,ms,noc,incm;
{
String na = JOptionPane.showInputDialog("enter the name for delete");
nm.setText(na);
String insql="select * from cust where name=?";
serch=cn.prepareStatement(insql);
serch.setString(1, nm.getText());
rs=serch.executeQuery();
rs.next();
JOptionPane.showMessageDialog(jf, "DATA FOUND");
14. System.out.println("sqlexception check your sqlstatement see detail n " + e);
}
catch(NullPointerException ne)
{
System.out.println("null object referenses are set see detail n " + ne);
}
catch(Exception e)
{
System.out.println("some problems are found see detailsn " + e);
return;
}
}
public static void main(String[] args)
{
// TODO code application logic here
retaildetail r=new retaildetail();
r.connection();
JFrame jf=new JFrame("..........CheckMate(96)............");
jf.setContentPane(r);
jf.setVisible(true);
jf.setSize(300,600);
jf.setResizable(false);
}
}
END OF CODING
15. -------------------------------------------------------------------------------------------------------------------------------------------------------Database:
-- Database: `checkmate`
-- -------------------------------------------------------hello there my DATABASE name is :--------->checkmate
CREATE TABLE IF NOT EXISTS `cust` (
`name` varchar(20) DEFAULT NULL,
`address` varchar(200) DEFAULT NULL,
`contact` int(20) DEFAULT NULL,
`businessAddress` varchar(200) DEFAULT NULL,
`businessContact` int(20) DEFAULT NULL,
`fax` int(20) DEFAULT NULL,
`mobile` int(20) DEFAULT NULL,
`pager` int(20) DEFAULT NULL,
`maritalstatus` varchar(20) DEFAULT NULL,
`children` int(20) DEFAULT NULL,
`annualincome` int(50) DEFAULT NULL,
`photos` varchar(10000) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- Dumping data for table `cust`
INSERT INTO `cust` (`name`, `address`, `contact`, `businessAddress`, `businessContact`, `fax`, `mobile`, `pager`,
`maritalstatus`, `children`, `annualincome`, `photos`) VALUES
('payal', '38sagar', 1234567890, 'hyderabad', 678905, 56, 1234567890, 1, 'merried', 2, 99000000, 'not available'),
('dhaval', '84shinagar', 1234567890, 'hyderabad', 678905, 56, 1234567890, 1, 'merried', 2, 99000000, 'not
available'),
('parth', '3santos', 1234567890, 'hyderabad', 678905, 56, 1234567890, 1, 'merried', 2, 999000000, 'not available');