Java GUI : Search and Show Record from Oracle 11G

Following example  uses JInternalFrame, JDesktopPane,  JTable to search and show  the record  from Oracle 11G Table

* Find The Table

j2

* Write  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  myWin extends  JFrame  implements ActionListener
{ 
    private JMenuItem StuSearch;
	private JMenuItem StuShow;
	public JDesktopPane desktop = new JDesktopPane();
	private JMenuBar  menuBar = new JMenuBar();
	static public  int MainWindowHeight=600;
	static  public  int MainWindowWidth=700;
	
	public myWin()
	{
		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("Search and Show Marks");
		           
		  menuBar.add(StuountMenu);
		           
		  StuSearch = new JMenuItem("Search Record");
		  StuShow = new JMenuItem("Show Record");
		  
		  StuountMenu.add(StuSearch);
		  StuountMenu.add(StuShow);
	        
		  StuSearch.addActionListener(this);
		  StuShow.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 == StuSearch) {
			
			OE  o= new  OE();
			desktop.add(o);
			o.show();
			
			
		}
		if (obj == StuShow) {
			
			Show s= new Show();
			desktop.add(s);
			s.show();
			//JOptionPane.showMessageDialog (this,  "Form not created!","Message",JOptionPane.INFORMATION_MESSAGE);
		}
	}

}


class  Show extends JInternalFrame 
{
	private JPanel JP = new JPanel ();
	Connection conn;
	Show()
	{
		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 OE extends JInternalFrame implements ActionListener
{
	private JPanel JP = new JPanel ();
	private JLabel l1;
   	private JButton btnOk;
    private  JTextField t1;
    
	OE()
	{
		super ("Search the record",true, true, true, true);
		setSize (500, 200);
		setLocation((myWindow.MainWindowWidth-this.getSize().width)/2,((myWindow.MainWindowHeight-this.getSize().height)/2)-100 );
		
		
		btnOk = new JButton("Search now!");
	    
		l1= new JLabel("Enter Roll Number");
		 
		t1= new JTextField(10);
		
		 JP.setLayout(null);
					
   	  l1.setBounds(50,10,200,20);
	  JP.add(l1);
	  t1.setBounds(250,10,100,20);
	  JP.add(t1);
		
	    btnOk.setBounds(200,60,120,20);
	  		
	  
	  //JP.setLayout( new FlowLayout());
	
	    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 ();
				}
			}
		}
		);
	    
	  	    
		
		btnOk.addActionListener(this);
		JP.add (btnOk);
		getContentPane().add (JP, BorderLayout.CENTER);
		setVisible (true);
	}

	
	
	public  void SearchData(String rollno)throws  Exception
	{
	
	 String[] columnNames = {"Name", "Hindi", "English", "Math"};
	 String url="jdbc:odbc:myOra"; 
  	 Connection conn = DriverManager.getConnection (url, "scott", "tiger");

  	 JInternalFrame frame1 = new JInternalFrame("Search Result", true, true, true, true);
     frame1.setLayout(new BorderLayout());
     DefaultTableModel model = new DefaultTableModel();
     model.setColumnIdentifiers(columnNames);
     JTable table = new JTable();
     table.setModel(model);
     table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
     table.setFillsViewportHeight(true);
     JScrollPane scroll = new JScrollPane(table);
     scroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
     scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		 try
		 {
			 
		   	 
			PreparedStatement preStatement = conn.prepareStatement("Select * from students  where roll=?");
		   	preStatement.setInt(1, Integer.parseInt(rollno));
		    ResultSet rs = preStatement.executeQuery();
            int i = 0;
            if (rs.next()) {
                String n = rs.getString("sname");
                String  h = rs.getString("hindi");
                String  e = rs.getString("eng");
                String  m = rs.getString("maths");
                model.addRow(new Object[]{n, h, e, m});
                i++;
        
                
                frame1.add(scroll);
                getParent().add(frame1, new Integer(1));
                frame1.setSize(500, 100);
                frame1.setVisible(true);
                
                
                
            }
            if (i < 1) {
                JOptionPane.showMessageDialog(null, "No Record Found", "Error", JOptionPane.ERROR_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
			{
						 try
						 {
							 
							/* JInternalFrame internalFrame = new JInternalFrame("codetpoint.com", true, true, true, true);
				             internalFrame.setBounds(50, 50, 200, 100);
			                 getParent().add(internalFrame, new Integer(1));
				             internalFrame.setVisible(true);*/
							 
							 
							 SearchData( t1.getText());
						 }
						 catch(Exception ex)
						 {
							 JOptionPane.showMessageDialog (this,  "Somewhere error  in the operation!","Message",JOptionPane.ERROR_MESSAGE );
						 }
		 
						 
			}
		}
		
	}
}

class  EditData
{

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

	}
	
}

 

* Run the code ...

j1