Friday, 2 January 2015

JAVA DATABASE CONNECTIVITY Program to connect with Oracle for faculty 

import java.awt.*;

import javax.swing.*;
import javax.swing.JFrame;
import java.awt.event.*;
import javax.swing.event.*;
import java.sql.*;


public class Lab7 extends JFrame implements ActionListener
{
JLabel l1,l2,l3;
JTextField t1,t2,t3;
JButton b1,b2,b3;

Lab7()
        {
        super("DataBases");
        setSize(500,450);
        setResizable(false);
        setLayout(null);


        l1=new JLabel("Name           :");
        l2=new JLabel("Department :");
        l3=new JLabel("Experience  :");

        t1=new JTextField(20);
        t2=new JTextField(20);
        t3=new JTextField(20);
       
        b1=new JButton("Next");       
        b2=new JButton("Prev");
        b3=new JButton("Close");
       
       add(l1).setBounds(10,60,150,20);
       add(t1).setBounds(150,60,200,20);
  
      add(l2).setBounds(10,90,150,20);
      add(t2).setBounds(150,90,200,20);
 
  add(l3).setBounds(10,110,150,20);
  add(t3).setBounds(150,130,200,20);

   b1.setMnemonic('N');
   b2.setMnemonic('P');
   b3.setMnemonic('x');               
  
       add(b1).setBounds(100,180,120,20);       
  add(b2).setBounds(240,180,120,20);      
  add(b3).setBounds(380,180,120,20);

   b1.addActionListener(this);   
   b2.addActionListener(this);
   b3.addActionListener(this);
       
  addWindowListener(new WindowAdapter(){public void  windowClosing(WindowEvent e){System.exit(0);}});
   }

public void actionPerformed(ActionEvent ae)
        {   
try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");     
   Connection   dbcon=DriverManager.getConnection ("jdbc:odbc:ram","scott","tiger");
   
 String query="select  name,dept,exp   from   faculty";
     Statement stmt=dbcon.createStatement();
     ResultSet rs=stmt.executeQuery(query);
   
     if(ae.getSource()==b1)
        {
            rs.next();
            t1.setText(rs.getString("name"));
            t2.setText(rs.getString("dept"));
            t3.setText(rs.getString("exp"));
        }
   
if(ae.getSource()==b2)
        {
            rs.previous();
            t1.setText(rs.getString("name"));
            t2.setText(rs.getString("dept"));
            t3.setText(rs.getString("exp"));
        }


     if(ae.getSource()==b3)
{
            System.exit(0);
        }
    }
    catch(ClassNotFoundException e)
    {
    System.out.println(e);
    }
    catch(Exception e)
    {
    System.out.println(e);
    }
        }

public static void main(String str[])
        {
        Lab7  l=new Lab7();
        l.setVisible(true);
        }
}

No comments:

Post a Comment