import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.awt.*;
class Myframe extends JFrame implements ActionListener
{
JLabel lno,lname;
JTextField tno,tname;
JButton insert,view,update,search,nxt,pre,lst,frt,clr,dlt,exit;
Connection cn;
Statement st;
ResultSet rs;
Container conpane;
Myframe()
{
lno=new JLabel("Eno");
lname=new JLabel("Ename");
tno=new JTextField(6);
tname=new JTextField(15);
insert=new JButton("Insert");
view=new JButton("View");
nxt=new JButton("Next");
pre=new JButton("Previous");
lst=new JButton("Last");
frt=new JButton("First");
clr=new JButton("Clear");
dlt=new JButton("Delete");
update=new JButton("Update");
search=new JButton("Search");
exit=new JButton("Exit");
conpane = getContentPane();
JPanel txtPanel,btnPanel;
txtPanel = new JPanel();
btnPanel = new JPanel();
//setLayout(new GridLayout(2,2));
conpane.add(txtPanel,BorderLayout.NORTH);
conpane.add(btnPanel,BorderLayout.CENTER);
txtPanel.add(lno);
txtPanel.add(tno);
txtPanel.add(lname);
txtPanel.add(tname);
btnPanel.add(insert);
btnPanel.add(view);
btnPanel.add(nxt);
btnPanel.add(pre);
btnPanel.add(lst);
btnPanel.add(frt);
btnPanel.add(clr);
btnPanel.add(dlt);
btnPanel.add(update);
btnPanel.add(search);
btnPanel.add(exit);
insert.addActionListener(this);
view.addActionListener(this);
nxt.addActionListener(this);
pre.addActionListener(this);
lst.addActionListener(this);
frt.addActionListener(this);
clr.addActionListener(this);
dlt.addActionListener(this);
update.addActionListener(this);
search.addActionListener(this);
exit.addActionListener(this);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:odbc:employeedsn");
st=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=st.executeQuery("select * from emp");
rs.next();
}catch(Exception e){}
}
public void actionPerformed(ActionEvent a)
{
try
{
if(a.getSource()==search)
{
dbOpen();
String str = JOptionPane.showInputDialog(null, "Enter empno : ");
rs=st.executeQuery("select * from emp where eno="+str+"");
rs.next();
setText();
}
if(a.getSource()==update)
{
dbOpen();
int ueno=Integer.parseInt(tno.getText());
String uname=(tname.getText());
st.executeUpdate("UPDATE emp SET eno=" + ueno +",ename='" + tname.getText() + "' WHERE eno="+ ueno +" ");
JOptionPane.showMessageDialog(null,"Record is updated");
}
if(a.getSource()==dlt)
{
int deno=Integer.parseInt(tno.getText());
st.executeUpdate("DELETE FROM emp WHERE eno="+ deno);
JOptionPane.showMessageDialog(null,"Record is deleted");
dbClose();
dbOpen();
}
if(a.getSource()==clr)
{
tno.setText("");
tname.setText("");
rs.first();
}
if(a.getSource()==frt)
{
rs.first();
setText();
}
if(a.getSource()==lst)
{
rs.last();
setText();
}
if(a.getSource()==nxt)
{
if(!rs.isAfterLast())
{
rs.next();
setText();
}
else
{
JOptionPane.showMessageDialog(null,"Last Record");
rs.previous();
}
}
if(a.getSource()==pre)
{
if(!rs.isBeforeFirst())
{
rs.previous();
setText();
}
else
{
JOptionPane.showMessageDialog(null,"First Record");
rs.first();
}
}
if(a.getSource()==insert)
{
int eno=Integer.parseInt(tno.getText());
String name=(tname.getText());
st.executeUpdate("insert into emp values("+eno+",'"+name+"')");
JOptionPane.showMessageDialog(null,"Record inserted into database");
dbClose();
dbOpen();
}
if(a.getSource()==view)
{
rs.next();
setText();
dbClose();
dbOpen();
}
if(a.getSource()==exit)
{
if(JOptionPane.showConfirmDialog(null,"Are You Sure You Want to Exit?","Confirm",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
System.exit(0);
}
}catch(Exception e)
{
}
}
public void dbOpen()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:odbc:employeedsn");
st=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=st.executeQuery("select * from emp");
rs.next();
}catch(Exception e){}
}
public void setText(){
try{
tno.setText(rs.getString(1));
tname.setText(rs.getString(2));
}catch(Exception ex){}
}
public void dbClose()
{
try{
st.close();
rs.close();
cn.close();
}catch(Exception e){}
}
}
class jdbcdemoframe
{
public static void main(String s[])
{
Myframe f= new Myframe();
f.setVisible(true);
f.setTitle("Employee Information");
f.setSize(350,250);
}
}
import java.awt.event.*;
import java.sql.*;
import java.awt.*;
class Myframe extends JFrame implements ActionListener
{
JLabel lno,lname;
JTextField tno,tname;
JButton insert,view,update,search,nxt,pre,lst,frt,clr,dlt,exit;
Connection cn;
Statement st;
ResultSet rs;
Container conpane;
Myframe()
{
lno=new JLabel("Eno");
lname=new JLabel("Ename");
tno=new JTextField(6);
tname=new JTextField(15);
insert=new JButton("Insert");
view=new JButton("View");
nxt=new JButton("Next");
pre=new JButton("Previous");
lst=new JButton("Last");
frt=new JButton("First");
clr=new JButton("Clear");
dlt=new JButton("Delete");
update=new JButton("Update");
search=new JButton("Search");
exit=new JButton("Exit");
conpane = getContentPane();
JPanel txtPanel,btnPanel;
txtPanel = new JPanel();
btnPanel = new JPanel();
//setLayout(new GridLayout(2,2));
conpane.add(txtPanel,BorderLayout.NORTH);
conpane.add(btnPanel,BorderLayout.CENTER);
txtPanel.add(lno);
txtPanel.add(tno);
txtPanel.add(lname);
txtPanel.add(tname);
btnPanel.add(insert);
btnPanel.add(view);
btnPanel.add(nxt);
btnPanel.add(pre);
btnPanel.add(lst);
btnPanel.add(frt);
btnPanel.add(clr);
btnPanel.add(dlt);
btnPanel.add(update);
btnPanel.add(search);
btnPanel.add(exit);
insert.addActionListener(this);
view.addActionListener(this);
nxt.addActionListener(this);
pre.addActionListener(this);
lst.addActionListener(this);
frt.addActionListener(this);
clr.addActionListener(this);
dlt.addActionListener(this);
update.addActionListener(this);
search.addActionListener(this);
exit.addActionListener(this);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:odbc:employeedsn");
st=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=st.executeQuery("select * from emp");
rs.next();
}catch(Exception e){}
}
public void actionPerformed(ActionEvent a)
{
try
{
if(a.getSource()==search)
{
dbOpen();
String str = JOptionPane.showInputDialog(null, "Enter empno : ");
rs=st.executeQuery("select * from emp where eno="+str+"");
rs.next();
setText();
}
if(a.getSource()==update)
{
dbOpen();
int ueno=Integer.parseInt(tno.getText());
String uname=(tname.getText());
st.executeUpdate("UPDATE emp SET eno=" + ueno +",ename='" + tname.getText() + "' WHERE eno="+ ueno +" ");
JOptionPane.showMessageDialog(null,"Record is updated");
}
if(a.getSource()==dlt)
{
int deno=Integer.parseInt(tno.getText());
st.executeUpdate("DELETE FROM emp WHERE eno="+ deno);
JOptionPane.showMessageDialog(null,"Record is deleted");
dbClose();
dbOpen();
}
if(a.getSource()==clr)
{
tno.setText("");
tname.setText("");
rs.first();
}
if(a.getSource()==frt)
{
rs.first();
setText();
}
if(a.getSource()==lst)
{
rs.last();
setText();
}
if(a.getSource()==nxt)
{
if(!rs.isAfterLast())
{
rs.next();
setText();
}
else
{
JOptionPane.showMessageDialog(null,"Last Record");
rs.previous();
}
}
if(a.getSource()==pre)
{
if(!rs.isBeforeFirst())
{
rs.previous();
setText();
}
else
{
JOptionPane.showMessageDialog(null,"First Record");
rs.first();
}
}
if(a.getSource()==insert)
{
int eno=Integer.parseInt(tno.getText());
String name=(tname.getText());
st.executeUpdate("insert into emp values("+eno+",'"+name+"')");
JOptionPane.showMessageDialog(null,"Record inserted into database");
dbClose();
dbOpen();
}
if(a.getSource()==view)
{
rs.next();
setText();
dbClose();
dbOpen();
}
if(a.getSource()==exit)
{
if(JOptionPane.showConfirmDialog(null,"Are You Sure You Want to Exit?","Confirm",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
System.exit(0);
}
}catch(Exception e)
{
}
}
public void dbOpen()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:odbc:employeedsn");
st=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=st.executeQuery("select * from emp");
rs.next();
}catch(Exception e){}
}
public void setText(){
try{
tno.setText(rs.getString(1));
tname.setText(rs.getString(2));
}catch(Exception ex){}
}
public void dbClose()
{
try{
st.close();
rs.close();
cn.close();
}catch(Exception e){}
}
}
class jdbcdemoframe
{
public static void main(String s[])
{
Myframe f= new Myframe();
f.setVisible(true);
f.setTitle("Employee Information");
f.setSize(350,250);
}
}