0% found this document useful (0 votes)
4 views6 pages

Delete Account Source Code

The DeleteAccount class is a Java Swing application that allows users to delete an account from a database. It includes fields for account details and buttons for searching and deleting accounts, with functionality to connect to a MySQL database. The application handles user interactions and displays appropriate messages based on the actions performed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Delete Account Source Code

The DeleteAccount class is a Java Swing application that allows users to delete an account from a database. It includes fields for account details and buttons for searching and deleting accounts, with functionality to connect to a MySQL database. The application handles user interactions and displays appropriate messages based on the actions performed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

import javax.swing.

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

public class DeleteAccount extends JFrame implements ActionListener


{
/**
*
*/
private static final long serialVersionUID = 1L;

JLabel lbl_ano, lbl_fname, lbl_lname, lbl_uname, lbl_pword;


JTextField txt_ano, txt_fname, txt_lname, txt_uname, txt_pword;
JFrame frame;
JButton btn_delete, btn_search, btn_exit;
Icon delete, search, back;

Container con;
Connection cn;
Statement st;
ResultSet rs;

public DeleteAccount(JFrame fr3, Statement st3)


{
super("Delete Account");

frame = fr3;
this.st = st3;

con = getContentPane();
con.setLayout(null);

lbl_ano = new JLabel ("Account Id");


lbl_fname = new JLabel ("First Name");
lbl_lname = new JLabel ("Last Name");
lbl_uname = new JLabel ("Username");
lbl_pword = new JLabel ("Password");

txt_ano = new JTextField(10);


txt_fname = new JTextField(50);
txt_lname = new JTextField(50);
txt_uname = new JTextField(50);
txt_pword = new JTextField(100);

delete = new ImageIcon ("delete.png");


search = new ImageIcon ("search.png");
back = new ImageIcon ("exit1.png");

btn_search = new JButton ("Find", search);


btn_delete = new JButton ("Delete", delete);
btn_exit = new JButton ("Exit", back);

lbl_ano.setBounds(10,5,100,25);
con.add(lbl_ano);
txt_ano.setBounds(130,5,75,25);
con.add(txt_ano);
txt_ano.setEditable(false);

lbl_fname.setBounds(10,35,100,25);
con.add(lbl_fname);
txt_fname.setBounds(130,35,150,25);
con.add(txt_fname);
txt_fname.setEditable(false);

lbl_lname.setBounds(10,65,100,25);
con.add(lbl_lname);
txt_lname.setBounds(130,65,150,25);
con.add(txt_lname);
txt_lname.setEditable(false);

lbl_uname.setBounds(10,95,100,25);
con.add(lbl_uname);
txt_uname.setBounds(130,95,150,25);
con.add(txt_uname);
txt_uname.setEditable(false);

lbl_pword.setBounds(10,125,100,25);
con.add(lbl_pword);
txt_pword.setBounds(130,125,300,25);
con.add(txt_pword);
txt_pword.setEditable(false);

btn_search.setBounds(10,175,110,50);
con.add(btn_search);
btn_delete.setBounds(130,175,110,50);
con.add(btn_delete);
btn_exit.setBounds(250,175,110,50);
con.add(btn_exit);

btn_search.addActionListener(this);
btn_delete.addActionListener(this);
btn_exit.addActionListener(this);

btn_delete.setEnabled(false);

pack();
setSize(500,300);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}

public void actionPerformed(ActionEvent evt)


{
if(evt.getSource() == btn_search)
{
try
{
dbOpen();

String str = JOptionPane.showInputDialog(null, "Enter Username:


","Message",JOptionPane.QUESTION_MESSAGE);
rs=st.executeQuery("SELECT * FROM accounts_tbl WHERE username =
'"+str+"'");
if(rs.next()==false)
{
JOptionPane.showMessageDialog(null, "Username Does not Exist",
"Error", JOptionPane.ERROR_MESSAGE);
this.dispose();
}
else
{
setText();
}

btn_delete.setEnabled(true);
btn_search.setEnabled(false);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e.getMessage(),"Message",
JOptionPane.ERROR_MESSAGE);
}
}

if(evt.getSource()==btn_delete)
{
try
{
dbOpen();

String c = txt_ano.getText();

if(JOptionPane.showConfirmDialog(null, "Are you sure you want to delete the


record?","Confirm",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
{
st.executeUpdate("DELETE FROM accounts_tbl WHERE (accnt_id =
'"+c+"')");
JOptionPane.showMessageDialog(null,"Account
Deleted","Message",JOptionPane.INFORMATION_MESSAGE);
btn_delete.setEnabled(false);
btn_search.setEnabled(true);

txt_ano.setText("");
txt_fname.setText("");
txt_lname.setText("");
txt_uname.setText("");
txt_pword.setText("");

dbClose();
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e.getMessage(),"Message",
JOptionPane.ERROR_MESSAGE);
}
}

if (evt.getSource() == btn_exit)
{
this.dispose();
}
}
public void dbOpen()
{
try
{
String url="jdbc:mysql://localhost:3306/javaee_db";
String user="root";
String password="";

cn=DriverManager.getConnection(url,user,password);
st=cn.createStatement();
rs = st.executeQuery("select * from accounts_tbl");
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}
}

public void dbClose()


{
try
{
rs.close();
st.close();
cn.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e.getMessage(),"Message",
JOptionPane.ERROR_MESSAGE);
}
}

public void setText()


{
try
{
txt_ano.setText(rs.getString(1));
txt_fname.setText(rs.getString(2));
txt_lname.setText(rs.getString(3));
txt_uname.setText(rs.getString(4));
txt_pword.setText(rs.getString(5));
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e.getMessage(),"Message",
JOptionPane.ERROR_MESSAGE);
}
}
}

You might also like