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

Add Account Source Code

The document is a Java Swing application class named AddAccount that allows users to add new accounts to a database. It includes fields for account ID, first name, last name, username, and password, with validation for each field before enabling the add button. The class also manages database connections and operations to insert new account data into the accounts_tbl in a MySQL database.
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 views7 pages

Add Account Source Code

The document is a Java Swing application class named AddAccount that allows users to add new accounts to a database. It includes fields for account ID, first name, last name, username, and password, with validation for each field before enabling the add button. The class also manages database connections and operations to insert new account data into the accounts_tbl in a MySQL database.
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/ 7

import javax.swing.

*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.text.DecimalFormat;

public class AddAccount extends JFrame implements ActionListener


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

DecimalFormat format1 = new DecimalFormat("00000");

JFrame frame;
JLabel lbl_ano, lbl_fname, lbl_lname, lbl_uname, lbl_pword;
JTextField txt_ano, txt_fname, txt_lname, txt_uname;
JPasswordField txt_pword;
JButton btn_add, btn_back;
Icon add, back;

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

public AddAccount(JFrame fr1, Statement st1)


{
super("Add Account");

frame = fr1;
this.st = st1;

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 JPasswordField(100);

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


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

btn_add = new JButton("Add",add);


btn_add.setEnabled(false);
btn_back = new JButton("Back",back);

lbl_ano.setBounds(10,5,100,25);
con.add(lbl_ano);
txt_ano.setBounds(130,5,100,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);

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

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

lbl_pword.setBounds(10,125,100,25);
con.add(lbl_pword);
txt_pword.setBounds(130,125,150,25);
con.add(txt_pword);

btn_add.setBounds(60,175,110,50);
con.add(btn_add);
btn_back.setBounds(180,175,110,50);
con.add(btn_back);
txt_fname.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(txt_fname.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"Enter First Name",
"Error", JOptionPane.ERROR_MESSAGE);
txt_fname.requestFocusInWindow();
}
else
{
txt_lname.requestFocusInWindow();
}
}
});

txt_lname.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(txt_lname.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"Enter Last Name",
"Error", JOptionPane.ERROR_MESSAGE);
txt_lname.requestFocusInWindow();
}
else
{
txt_uname.requestFocusInWindow();
}
}
});

txt_uname.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(txt_uname.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"Enter Username",
"Error", JOptionPane.ERROR_MESSAGE);
txt_uname.requestFocusInWindow();
}
else
{
txt_pword.requestFocusInWindow();
}
}
});

txt_pword.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(txt_pword.getPassword().length==0)
{
JOptionPane.showMessageDialog(null,"Enter Password",
"Error", JOptionPane.ERROR_MESSAGE);
txt_pword.requestFocusInWindow();
}
else
{
btn_add.requestFocusInWindow();
btn_add.setEnabled(true);
}
}
});

btn_add.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
btn_add.doClick();
}
});

btn_back.addActionListener(this);
btn_add.addActionListener(this);

dbOpen();
try
{
rs = st.executeQuery("select ifnull(max(accnt_id),0) as id from
accounts_tbl");
rs.next();
String e = (rs.getString("id"));
int en = Integer.parseInt(e);
en++;
txt_ano.setText(String.valueOf(format1.format(en)));
txt_fname.requestFocusInWindow();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}

pack();
setSize(350,275);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
txt_fname.requestFocusInWindow();
}

public void actionPerformed(ActionEvent ae)


{
if(ae.getSource()==btn_back)
{
this.dispose();
}
if(ae.getSource()==btn_add)
{
String an = txt_ano.getText().trim();
String fn = txt_fname.getText().toUpperCase().trim();
String ln = txt_lname.getText().toUpperCase().trim();
String un = txt_uname.getText().trim();
String pw = new String(txt_pword.getPassword());

try
{
dbOpen();
st.executeUpdate("insert into accounts_tbl
values('"+an+"','"+fn+"','"+ln+"','"+un+"',md5('"+pw+"'))");
JOptionPane.showMessageDialog(null, "Account Added","Add
Account", JOptionPane.INFORMATION_MESSAGE);
btn_add.setEnabled(false);
rs = st.executeQuery("select max(accnt_id) as id1 from
accounts_tbl");
rs.next();
String e1 = (rs.getString("id1"));
int en1 = Integer.parseInt(e1);
en1 = en1 + 1;
txt_ano.setText(String.valueOf(format1.format(en1)));
txt_fname.setText("");
txt_lname.setText("");
txt_uname.setText("");
txt_pword.setText("");
txt_fname.requestFocusInWindow();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
JOptionPane.ERROR_MESSAGE);
}
}
}

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(), "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}

You might also like