Design Frame to create and manage database for student with basic details (Name, Phone number, Email).
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
class Demo extends JFrame implements
ActionListener
{
JTextField
name,add,eid;
JButton
insert,delete,update,search,next,pre,first,last;
Connection
con;
Statement
st;
ResultSet
rs;
PreparedStatement
ps;
public
Demo()
{
Container
c= this.getContentPane();
c.setLayout(new
FlowLayout());
try
{
con=DriverManager.getConnection("jdbc:derby://localhost:1527/trilok");
st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs=st.executeQuery("SELECT
* FROM APP.ASSINGMENT1");
rs.next();
}
catch(Exception
e)
{
System.out.println(e);
}
name=new
JTextField(20);
add=new
JTextField(20);
eid=new
JTextField(20);
insert=new
JButton("Insert");
delete=new
JButton("Delete");
search=new
JButton("Search");
update=new
JButton("Update");
next=new
JButton("Next");
pre=new
JButton("Previous");
first=new
JButton("Frist");
last=new
JButton("Last");
c.add(name);
c.add(add);
c.add(eid);
c.add(insert);
c.add(delete);
c.add(search);
c.add(update);
c.add(next);
c.add(pre);
c.add(first);
c.add(last);
insert.addActionListener(this);
delete.addActionListener(this);
search.addActionListener(this);
update.addActionListener(this);
next.addActionListener(this);
pre.addActionListener(this);
first.addActionListener(this);
last.addActionListener(this);
}
@Override
public
void actionPerformed(ActionEvent ae)
{
String s=ae.getActionCommand();
if(s.equals("Frist"))
{
try
{
rs.first();
name.setText(rs.getString(1));
add.setText(rs.getString(2));
eid.setText(rs.getString(3));
}
catch(Exception e)
{
System.out.println(e);
}
}
if(s.equals("Last"))
{
try {
rs.last();
name.setText(rs.getString(1));
add.setText(rs.getString(2));
eid.setText(rs.getString(3));
}
catch(Exception e1)
{}
}
if(s.equals("Next"))
{
try
{
if(rs.isLast())
rs.first();
else
rs.next();}
name.setText(rs.getString(1));
add.setText(rs.getString(2));
eid.setText(rs.getString(3));
}
catch(Exception e)
{}
}
if(s.equals("Previous"))
{
try
{
if(rs.isFirst())
rs.last();
else
rs.previous();
name.setText(rs.getString(1));
add.setText(rs.getString(2));
eid.setText(rs.getString(3));
}
catch(Exception e2)
{}
}
if(s.equals("Insert"))
{
try{
ps=con.prepareStatement("INSERT INTO
APP.ASSINGMENT1(NAME,ADDRESS,EID) values(?,?,?)");
ps.setString(1, name.getText());
ps.setString(2, add.getText());
ps.setString(3, eid.getText());
ps.executeUpdate();
}
catch (Exception e4)
{}
}
if(s.equals("Delete"))
{
String q;
q = "DELETE FROM APP.ASSINGMENT1 WHERE NAME =’"+name.getText()+
"' OR ADDRESS = '"+add.getText()+"' OR EID =
'"+eid.getText()+"'";
try{
ps=con.prepareStatement(q);
ps.executeUpdate();
}
catch(Exception e5){}
}
if(s.equals("Search"))
{
String q;
q="SELECT *FROM APP.ASSINGMENT1 WHERE NAME='"+name.getText()+"'";
try{
rs=st.executeQuery(q);
if(rs.next())
{
name.setText(rs.getString(1));
add.setText(rs.getString(2));
eid.setText(rs.getString(3));
}
}
catch(Exception e6){}
}
if(s.equals("Update"))
{
try
{
String
nm = name.getText();
String
ad=add.getText();
String
ei=eid.getText();
String
q;
q =
"UPDATE APP.ASSINGMENT1 SET ADDRESS='"+ad+"',EID='"+ei+
"' WHERE ='"+nm+"'";
ps=con.prepareStatement(q);
ps.executeUpdate();
}
catch(SQLException
se)
{}
}
}
}
public class Demo {
public
static void main(String[] args) {
Demo ob=new Demo();
ob.setSize(300,500);
ob.setVisible(true);
}
}
Comments
Post a Comment