JAVA GUI : MYSQL and JAVA with LIST, DELETE and Add Action

LIST, DELETE and Add Action on MYSQL in JAVA


● Download MySQL Connector/J from Click Here and place in java project as


cptop


● Create Database and a Table in MySQL

cp2


● Create a Java File

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.*;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
class  Leaders  extends  JFrame implements  ActionListener 
{
     JMenuItem newFile= new JMenuItem("New");
     JMenuItem openFile= new JMenuItem("List Record");
     JMenuItem saveFile= new JMenuItem("Save Record");
     JMenuItem deleteFile= new JMenuItem("Delete Record");
     JMenuItem exitWindow= new JMenuItem("Close it");
     
     private JDesktopPane desktop = new JDesktopPane();
     private JMenuBar  menuBar = new JMenuBar();
     Leaders()
	 {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		setSize(dim.width,dim.height);
        setTitle("MySQL and JAVA ");
        setResizable(true);
        setJMenuBar(menuBar);
        JMenu file = new JMenu("MySQL");     
    
        JMenu window = new JMenu("Window");
        exitWindow.addActionListener(this);
        window.add(exitWindow);
        
        
        newFile.addActionListener(this);
        openFile.addActionListener(this);
        saveFile.addActionListener(this);
        deleteFile.addActionListener(this);
     
        file.add(openFile);
        file.add(saveFile);
        file.add(deleteFile);
        menuBar.add(file);
        menuBar.add(window);
        getContentPane().add (desktop, BorderLayout.CENTER);
        setVisible(true);
	}
@Override
public void actionPerformed(ActionEvent arg) {
	// TODO Auto-generated method stub
	
	  if(arg.getSource()==(JMenuItem)exitWindow)
	  {
	   this.dispose();
	  }
	
	  else if(arg.getSource()==(JMenuItem)openFile)
	  {
	 
		  ListWindow  list= new  ListWindow();
		  desktop.add( list);
		  list.show();
		  
	  }
	  else if(arg.getSource()==(JMenuItem)saveFile)
	  {
		  saveWindow  sw= new  saveWindow();
		  desktop.add(sw);
		  sw.show();
	  }
	  
	  else if(arg.getSource()==(JMenuItem)deleteFile)
	  {

		  DeleteWindow dw= new   DeleteWindow();
		  desktop.add(dw);
		  dw.show();
		  
	  }
	
}
	  
}

				class  ListWindow  extends  JInternalFrame
				{
					JPanel JP = new JPanel ();
					ListWindow()
					{
						super ("List Record",true,true);
				 		setSize (500, 400);
				 		setLocation(100,50);
				 		
				 		DefaultTableModel model = new DefaultTableModel();
				 		model.addColumn("Member Id");
				 		model.addColumn("Name");
				 		model.addColumn("Address");
				 		JTable table = new JTable(model);
				 		
				 		
				 		 try{  
								Class.forName("com.mysql.jdbc.Driver");  
								Connection con=DriverManager.getConnection( "jdbc:mysql://localhost/bjp","root","");
								Statement   st=con.createStatement(); 
				    	
								ResultSet  rs=st.executeQuery("select * from members");
								
									while( rs.next() )
									{
										
									    String id= rs.getString(1);
									    String name= rs.getString(2);
									    String address= rs.getString(3);
									    
									    model.addRow(new Object[]{id,name,address });
									    
									}
									JP.add(new JScrollPane(table));
									getContentPane().add (JP, BorderLayout.CENTER);
								 
								con.close();  
								}catch(Exception e){
									
									//JOptionPane.showMessageDialog(null,e.getMessage());
								 
								}
				 			 		
				 		//------------------------------------------
				 		setVisible(true);
					}
					
					
					
					
					
					
					
				}



class  DeleteWindow extends  JInternalFrame  implements  ActionListener
{
	JLabel  lbl1= new JLabel (" Member ID:");   
	 JTextField   txt1;
	 
	 JLabel  lbl2= new JLabel (" Memeber Name:");   
	 JTextField   txt2;
	 
	 
	 JLabel  lbl3= new JLabel ("Address");   
	 JTextField   txt3; 
	 
	 
	 JLabel  lbl4= new JLabel ("Search Record!");
	 JTextField   txt4;
	 
	 
	 JButton btn= new JButton("Now Delete ? ");
	 JButton clear= new JButton("Clear ");
	 JButton go= new JButton("Go ");

				DeleteWindow()
				{
					super ("Delete Record",true,true);
			 		setSize (500, 400);
			 		setLocation(100,50);
			 		setLayout(null);
			 		lbl1.setBounds(30, 20, 100,30);
			 		add(lbl1);
			 		txt1= new JTextField();
			 		txt1.setBounds(140, 20, 200, 30);
			 		txt1.setEditable(false);
			 		add(txt1);
			 		
			 		
			 		lbl2.setBounds(30, 60, 100,30);
			 		add(lbl2);
			 		txt2= new JTextField();
			 		txt2.setEditable(false);
			 		txt2.setBounds(140, 60, 200, 30);
			 		add(txt2);

			 		
			 		lbl3.setBounds(30, 100, 100,30);
			 		add(lbl3);
			 		txt3= new JTextField();
			 		txt3.setEditable(false);
			 		txt3.setBounds(140, 100, 200, 30);
			 		add(txt3);
			 		
			 		
			 		
			 		btn.setBounds(210, 150, 120, 30);
			  		btn.addActionListener(this);
			  		btn.setVisible(false);
			  		add(btn);
			 		

			  		clear.setBounds(100, 150, 100, 30);
			  		clear.addActionListener(this);
			  		add(clear);

			  		lbl4.setBounds(30, 200, 100,30);
			 		add(lbl4);
			 		txt4= new JTextField();
			 		txt4.setEditable(true);
			 		txt4.setBounds(140, 200, 200, 30);
			 		add(txt4);
			  		
			 		go.setBounds(350, 200, 70, 30);
			  		go.addActionListener(this);
			  		go.setVisible(true);
			  		add(go);
			  		
			  		
			 		setVisible(true);
				}

				@Override
				public void actionPerformed(ActionEvent a) {
					if( a.getSource()== go)
					 {
						
						 
						SearchRecord( txt4.getText());
						
					 }
					
					if( a.getSource()== btn)
					 {
						
						 
						DeleteRecord(txt1.getText());
						
					 }
					if( a.getSource()== clear)
					 {
					       txt1.setText("");
					       txt2.setText("");
					       txt3.setText("");
					       txt1.requestFocus();
			   
					 }
					
				}
				
				
				
				void   DeleteRecord(String  code)
				{
				
					
					try{  
						Class.forName("com.mysql.jdbc.Driver");  
						Connection con=DriverManager.getConnection( "jdbc:mysql://localhost/bjp","root","");
						PreparedStatement st=con.prepareStatement("delete  from  members  where  mem_id=?");
						st.setString(1,code);
						
						int dialogResult = JOptionPane.showConfirmDialog (null, "Would You Like Delete?","Warning",JOptionPane.YES_NO_OPTION);
						if(dialogResult == JOptionPane.YES_OPTION){
			
							 int r=   st.executeUpdate();
							 if( r>0)
							 {
								 JOptionPane.showMessageDialog(null,"Record Deleted Sucessfully" );
								 
								 txt1.setText("");
							       txt2.setText("");
							       txt3.setText("");
							       txt4.requestFocus();
								 
							 }
							 else
							 {
								 JOptionPane.showMessageDialog(null,"Not Deleted Sucessfully" );
								 txt4.setText("");
								 txt4.requestFocus();
							 }
							
						}
					con.close();
							
					}
					catch(Exception e){
						
						//JOptionPane.showMessageDialog(null,e.getMessage());
					 
					}
					
					
				}
						
						
				void   SearchRecord(String  code)
				{
				
					
					try{  
						Class.forName("com.mysql.jdbc.Driver");  
						Connection con=DriverManager.getConnection( "jdbc:mysql://localhost/bjp","root","");
						Statement   st=con.createStatement(); 
						
						ResultSet  rs=st.executeQuery("select * from members where  mem_id  like '"+code+"'");
						   rs.next();
						   if( rs.getRow()>0)
						   {
						 
						   String id= rs.getString(1);
						   String name= rs.getString(2);
						   String address= rs.getString(3);
						    
						   txt1.setText(id);
						   txt2.setText(name);
						   txt3.setText(address);
						   
						   btn.setVisible(true);
						   }
						   else
						   {
							   btn.setVisible(false);
							   txt1.setText("");
						       txt2.setText("");
						       txt3.setText("");
						       txt4.requestFocus();
				   
							   JOptionPane.showMessageDialog(null,"No Record Found");
							   
							   
						   }
						   
						   
						   
						con.close();  
						}catch(Exception e){
							
							//JOptionPane.showMessageDialog(null,e.getMessage());
						 
						}
				}
				
				
				
				
				
				
				
	
}


 

 
 
 class  saveWindow  extends  JInternalFrame implements  ActionListener
 {
	 
	 
	 JLabel  lbl1= new JLabel (" Member ID:");   
	 JTextField   txt1;
	 
	 JLabel  lbl2= new JLabel (" Memeber Name:");   
	 JTextField   txt2;
	 
	 
	 JLabel  lbl3= new JLabel ("Address");   
	 JTextField   txt3; 
	 JButton btn= new JButton(" Save Record ");
	 JButton clear= new JButton("Clear ");
	 saveWindow()
	 {
	 
		 		super ("New Memeber Registration ",false,true);
		 		setSize (500, 300);
		 		setLayout(null);
		 		lbl1.setBounds(30, 20, 100,30);
		 		add(lbl1);
		 		txt1= new JTextField();
		 		txt1.setBounds(140, 20, 200, 30);
		 		add(txt1);
		 		
		 		
		 		lbl2.setBounds(30, 60, 100,30);
		 		add(lbl2);
		 		txt2= new JTextField();
		 		txt2.setBounds(140, 60, 200, 30);
		 		add(txt2);

		 		
		 		lbl3.setBounds(30, 100, 100,30);
		 		add(lbl3);
		 		txt3= new JTextField();
		 		txt3.setBounds(140, 100, 200, 30);
		 		add(txt3);
		 		
		 		
		 		
		 		btn.setBounds(210, 150, 120, 30);
		  		btn.addActionListener(this);
		  	
		  		add(btn);
		 		

		  		clear.setBounds(100, 150, 100, 30);
		  		clear.addActionListener(this);
		  		add(clear);

		 		
		 		
		 		
		 		setVisible(true);
 	 }
	@Override
	public void actionPerformed(ActionEvent a) {
		
		if( a.getSource()== btn)
		 {
		     
			int r=SaveData(txt1.getText(), txt2.getText(), txt3.getText() );
			if( r>0)
			{
				JOptionPane.showMessageDialog(null,"Saved Sucessfully");
			}
			else
			{
				JOptionPane.showMessageDialog(null,"Sorry! Not Saved ");
			}
			
		 }
		
		if( a.getSource()== clear)
		 {
		       txt1.setText("");
		       txt2.setText("");
		       txt3.setText("");
		       txt1.requestFocus();
   
		 }

		
		// TODO Auto-generated method stub
		
	}
	

	 int  SaveData(String id, String name,String  add  )
	 {
		 int   r=0;
		 
		 try{  
				Class.forName("com.mysql.jdbc.Driver");  
				Connection con=DriverManager.getConnection( "jdbc:mysql://localhost/bjp","root","");  
    			PreparedStatement st=con.prepareStatement("insert into members(mem_id,mem_name,mem_address)  values(?,?,?)");
				st.setString(1,id);
				st.setString(2,name);
				st.setString(3,add);
				r=   st.executeUpdate();
				con.close();  
				}catch(Exception e){
					
					//JOptionPane.showMessageDialog(null,e.getMessage());
					 r=0; 
				}
	 	  return r;
	 
	 }

 }

public class interenFrame {
	
	
	  public static void   main(String args[])
	  {
	    new    Leaders();
	  }

}

● Run Your project ! Click on MySQL -> List Record

cp1

● Here is record stored in MySQL TABle

cp3


● Add another record click MySQL->Save Record

cp4


● To See Saved record click on MySQL->List Record

cp5


● You can delete any record after search ,click on MySQL->Delete Record

cp6


● List again click on MySQL->List Record

cp3