package conpack;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import
java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.PreparedStatement;
import
java.sql.ResultSet;
import
java.sql.Statement;
import
javax.swing.DefaultListModel;
import
javax.swing.JButton;
import
javax.swing.JFrame;
import
javax.swing.JLabel;
import
javax.swing.JList;
import
javax.swing.JOptionPane;
import
javax.swing.JTextField;
import
javax.swing.event.ListSelectionEvent;
import
javax.swing.event.ListSelectionListener;
public class EditContact extends JFrame implements ActionListener,ListSelectionListener{
JButton
b,d;
JTextField
name,number;
JLabel
i,k;
JList
list1;
DefaultListModel model ;
public String str;
EditContact
()
{
setTitle("Edit
Contact");
setLayout(null);
b=new JButton("Update");
b.setBounds(0,240,
100,20);
add(b);
b.addActionListener(this);
d=new JButton("exit");
d.setBounds(190,240,
100,20);
add(d);
d.addActionListener(this);
model = new
DefaultListModel();
list1 = new JList(model);
list1.setBounds(100,70,100,100);
add(list1);
// Initialize
the list with items
list1.addListSelectionListener(this);
name=new JTextField(5);
name.setBounds(50,20,
100, 20);
add(name);
number=new JTextField(5);
number.setBounds(150,
20, 100, 20);
add(number);
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system","system");
Statement
st=con.createStatement();
ResultSet
rs=st.executeQuery("select * from contacts");
int i = 0;
while(rs.next()){
model.add(i,rs.getString("nam"));
i++;
}
}
catch(Exception e1)
{
System.out.println(e1);
}
}
public void
valueChanged(ListSelectionEvent e) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system","system");
Statement
st=con.createStatement();
str = list1.getSelectedValue().toString();
ResultSet
rs=st.executeQuery("select nam,num from contacts where nam = '"+str+"'");
while(rs.next()){
name.setText(rs.getString(1));
number.setText(rs.getString(2));
}
}
catch(Exception e1)
{
System.out.println(e1);
}
}
@Override
public void
actionPerformed(ActionEvent e) {
if(e.getSource()==b)
{
try{
if((JOptionPane.showConfirmDialog(this, "Do you
Want to update?"))==0)
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system","system");
System.out.println("connected");
PreparedStatement
pr=con.prepareStatement("update contacts set nam='"+name.getText()+"',num='"+number.getText()+"' where
nam='"+str+"'");
pr.executeUpdate();
JOptionPane.showMessageDialog(this, "contact
updated");
dispose();
windows
me = new windows();
me.setVisible(true);
me.setLocation(500, 400);
}
else{
JOptionPane.showMessageDialog(this, "operation
failed");
dispose();
windows
me = new windows();
me.setVisible(true);
me.setLocation(500, 400);
}
}
catch(Exception e1)
{
System.out.println(e1);
JOptionPane.showMessageDialog(this,e1);
dispose();
windows
me = new windows();
me.setVisible(true);
me.setLocation(500, 400);
}
}
if(e.getSource()==d)
{
dispose();
windows
me = new windows();
me.setVisible(true);
me.setLocation(500, 400);
}
}
public static void main(String
ar[])
{
EditContact as=new EditContact ();
as.setSize(300,
300);
as.setVisible(true);
as.setLocation(500,
400);
}
}
Post a Comment