Java GUI : Add New Record and Show All Record using JFrame, JInternalFrame, JMenu and JTable

1. Here is Table in Oracle 11G

java code

2. Create System DSN

n2

3. Java Code

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import javax.swing.table.DefaultTableModel;

class  myWindow extends  JFrame  implements ActionListener
{ 
    
	private JMenuItem StuOpen;
	private JMenuItem StuSearch;
	private JDesktopPane desktop = new JDesktopPane();
	private JMenuBar  menuBar = new JMenuBar();
	 static public  int MainWindowHeight=600;
	 static  public  int MainWindowWidth=700;
	public myWindow()
	{
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("SOFTAID PUBLIC SCHOOL");
		 setSize(700,600);
		setLocation((Toolkit.getDefaultToolkit().getScreenSize().width  - getWidth()) / 2,
					(Toolkit.getDefaultToolkit().getScreenSize().height - getHeight()) / 2);
        setJMenuBar(menuBar);
		        
		  JMenu StuountMenu = new JMenu("Exam-Marks-Entry");
		           
		  menuBar.add(StuountMenu);
		           
		  StuOpen = new JMenuItem("New Student");
		  StuSearch = new JMenuItem("Show All Students");
		  
		  StuountMenu.add(StuOpen);
		  StuountMenu.add(StuSearch);
	        
		  StuOpen.addActionListener(this);
		  StuSearch.addActionListener(this);
		  //getContentPane().add (desktop, BorderLayout.CENTER);
		  getContentPane().add (desktop);
	      setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
		Object obj = e.getSource();

		if (obj == StuOpen) {
			
			OA  o= new  OA();
			desktop.add(o);
			o.show();
			
			
		}
		if (obj == StuSearch) {
			
			showAll s= new showAll();
			desktop.add(s);
			s.show();
			//JOptionPane.showMessageDialog (this,  "Form not created!","Message",JOptionPane.INFORMATION_MESSAGE);
		}
	}

}


class  showAll extends JInternalFrame 
{
	private JPanel JP = new JPanel ();
	Connection conn;
	showAll()
	{
		super ("List of students",true, true, true, true);
		setSize (500, 400);
		setLocation((myWindow.MainWindowWidth-this.getSize().width)/2,(myWindow.MainWindowHeight-this.getSize().height)/2);
		try
		{
		 
			String url="jdbc:odbc:myOra"; 
	  	    conn = DriverManager.getConnection (url, "scott", "tiger");
	  	   
	  	     
				 PreparedStatement preStatement = conn.prepareStatement("select sname,hindi,eng,maths from students");
			   	 ResultSet result =preStatement.executeQuery();

   		     
   		  DefaultTableModel model = new DefaultTableModel();
       
          model.addColumn("Name");
          model.addColumn("Hindi");
          model.addColumn("English");
          model.addColumn("Math");
                   
          JTable table = new JTable(model);
        
          while( result.next())
          {
        	  
        	  String  n= result.getString(1);
        	  int  h= result.getInt(2);
        	  int  e= result.getInt(3);
        	  int  m= result.getInt(4);
      	  
        	  model.addRow(new Object[]{n, h, e,m });
          }
          
           
          	//add the table to the frame
		    JP.add(new JScrollPane(table));
		    conn.close();
		     getContentPane().add (JP, BorderLayout.CENTER);
			setVisible (true);	 
 	  
			 }
		
			 catch(Exception ex)
			 {
			JOptionPane.showMessageDialog (this, ex.getMessage() ,"Message",JOptionPane.ERROR_MESSAGE );
			 }
			
		
	 }
}


class OA extends JInternalFrame implements ActionListener
{
	private JPanel JP = new JPanel ();
	private JLabel l1,l2,l3,l4,l5;
   
	private JButton btnOk;
    
    private  JTextField t1,t2,t3,t4,t5;
    
    
    
	OA()
	{
		super ("Marks  Entry Form",true, true, true, true);
		setSize (500, 400);
		setLocation((myWindow.MainWindowWidth-this.getSize().width)/2,(myWindow.MainWindowHeight-this.getSize().height)/2);
		
		
		btnOk = new JButton("Save now!");
	    
		l1= new JLabel("Enter Roll Number");
		l2= new JLabel("Enter Name");
		l3= new JLabel("Enter Marks-Hindi");
		l4= new JLabel("Enter Marks-English");
		l5= new JLabel("Enter Marks-Math");
	     
		t1= new JTextField(10);
		t2= new JTextField(10);
		t3= new JTextField(10);
		t4= new JTextField(10);
		t5= new JTextField(10);
		
		  JP.setLayout(null);
					
   	  l1.setBounds(50,10,200,20);
	  JP.add(l1);
	  t1.setBounds(250,10,100,20);
	  JP.add(t1);
		

	  l2.setBounds(50,40,200,20);
	  JP.add(l2);
	  
	  t2.setBounds(250,40,150,20);
      JP.add(t2);

	  
      l3.setBounds(50,70,200,20);
	  JP.add(l3);
	  t3.setBounds(250,70,100,20);
	  JP.add(t3);

	  l4.setBounds(50,100,200,20);
	  JP.add(l4);
	  t4.setBounds(250,100,100,20);
	  JP.add(t4);

	  l5.setBounds(50,130,200,20);
	  JP.add(l5);
	  t5.setBounds(250,130,100,20);
	  JP.add(t5);
		
	  
	  //JP.setLayout( new FlowLayout());
	    btnOk.setBounds(200,160,100,20);
	    t1.addKeyListener (new KeyAdapter () {
			public void keyTyped (KeyEvent ke) {
				char c = ke.getKeyChar ();
				if (! ((Character.isDigit (c)) || (c == KeyEvent.VK_BACK_SPACE))) {
					getToolkit().beep ();
					ke.consume ();
				}
			}
		}
		);
	    
	    t3.addKeyListener (new KeyAdapter () {
			public void keyTyped (KeyEvent ke) {
				char c = ke.getKeyChar ();
				if (! ((Character.isDigit (c)) || (c == KeyEvent.VK_BACK_SPACE))) {
					getToolkit().beep ();
					ke.consume ();
				}
			}
		}
		);
	    t4.addKeyListener (new KeyAdapter () {
			public void keyTyped (KeyEvent ke) {
				char c = ke.getKeyChar ();
				if (! ((Character.isDigit (c)) || (c == KeyEvent.VK_BACK_SPACE))) {
					getToolkit().beep ();
					ke.consume ();
				}
			}
		}
		);
	    t5.addKeyListener (new KeyAdapter () {
			public void keyTyped (KeyEvent ke) {
				char c = ke.getKeyChar ();
				if (! ((Character.isDigit (c)) || (c == KeyEvent.VK_BACK_SPACE))) {
					getToolkit().beep ();
					ke.consume ();
				}
			}
		}
		);
	    
    
		btnOk.addActionListener(this);
		JP.add (btnOk);
	
	
		getContentPane().add (JP, BorderLayout.CENTER);
	
		setVisible (true);
	}

	
	
	public  void saveData(String rollno,String  Name,String H,String E,String M)throws  Exception
	{
		
	 String url="jdbc:odbc:myOra"; 
  	 Connection conn = DriverManager.getConnection (url, "scott", "tiger");

		 try
		 {
			 
		   	 
			PreparedStatement preStatement = conn.prepareStatement("insert  into students(roll,sname,hindi,eng,maths)  values(?,?,?,?,?)");
		   	preStatement.setInt(1, Integer.parseInt(rollno));
		   	preStatement.setString(2,Name);
		   	preStatement.setInt(3, Integer.parseInt(H));
		   	preStatement.setInt(4, Integer.parseInt(E));
		   	preStatement.setInt(5, Integer.parseInt(M));
		   	
		   		int row=	preStatement.executeUpdate();
		   		if( row!=0)
		   		{
		   			JOptionPane.showMessageDialog (this,  "Yahoo ! Data Stored Sucessfully","Message",JOptionPane.INFORMATION_MESSAGE);
		   			conn.commit();
		   		}
		   		else
		   		{
		   			JOptionPane.showMessageDialog (this,  "Oops ! Data  not stored ?","Message",JOptionPane.WARNING_MESSAGE);
		   		}
		   			
		   			
		   	 
		    }
		 
		 catch(Exception ex)
		 {
			 JOptionPane.showMessageDialog (this,  "Somewhere error  in the operation!","Message",JOptionPane.ERROR_MESSAGE );
		 }
		
  finally
  {
   
     conn.close();
  
  }
	
	}
	
	@Override
	public void actionPerformed(ActionEvent arg) {
		Object obj = arg.getSource();

		if (obj == btnOk) {	
			
	
			if (t1.getText().equals ("")) {
				JOptionPane.showMessageDialog (this, "Roll Number not entered!.");
				t1.requestFocus ();
			}
			
			else if(t2.getText().equals (""))
			{
				JOptionPane.showMessageDialog (this, "Name not entered!.");
				t2.requestFocus ();
			}
			else if(t3.getText().equals (""))
			{
				JOptionPane.showMessageDialog (this, "Marks  Hindi  not entered!.");
				t3.requestFocus ();
			}
			else if(t4.getText().equals (""))
			{
				JOptionPane.showMessageDialog (this, "Marks English  not entered!.");
				t4.requestFocus ();
			}
			else if(t5.getText().equals (""))
			{
				JOptionPane.showMessageDialog (this, "Marks Math not entered!.");
				t5.requestFocus ();
			}
			else
			{
						 try
						 {
						 saveData( t1.getText(),t2.getText(),t3.getText(),t4.getText(),t5.getText());
						 }
						 catch(Exception ex)
						 {
							 JOptionPane.showMessageDialog (this,  "Somewhere error  in the operation!","Message",JOptionPane.ERROR_MESSAGE );
						 }
		 
						 
			}
		}
		
	}
}

class codetpoint
{

	public static void main(String args[])
	{
		
	myWindow  w= new  myWindow();

	}
	
}

4. Output Window

nn4

nn3