Creating User Account Information in Java - Adding Records to Database

Good day! This tutorial will focus on creating a user account information in java particularly in adding records to the database. Now let's start this tutorial! :) 1. Create your database in ms access and named it sample.mdb with Login table and the following entities below. design design 2. Create your java program now named UserSettings.java. 3. Import the following libraries.
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.sql.*;
  5. import java.lang.*;
4. Initialize the following variables. 3 textfield for username, first name and family name, and 1 passwordfield for the password, 1 button named btnNew, and sql connection variables Connection, Statement, and PreparedStatement.
  1.         JLabel lblUser = new JLabel("Username ",JLabel.RIGHT); 
  2.         JLabel lblPass = new JLabel("Password ",JLabel.RIGHT);
  3.         JLabel lblName1 = new JLabel("First Name",JLabel.RIGHT);
  4.         JLabel lblName2 = new JLabel("Family Name",JLabel.RIGHT);
  5.        
  6.         JTextField txtUser = new JTextField(20);
  7.         JPasswordField txtPass= new JPasswordField(20);
  8.         JTextField txtName1= new JTextField(20);
  9.         JTextField txtName2= new JTextField(20);
  10.        
  11.         JButton btnNew = new JButton("Add");
  12.  
  13.         Connection cn;
  14.         Statement st;
  15.         PreparedStatement ps;
5. Create a constructor named UserSettings() same with your classname for creating the panels to put controls in the form as well as the connection in the database.
  1. public UserSettings() {
  2.                 super("User Account Settings");
  3.        
  4.                 JPanel pane = new JPanel();
  5.                 pane.setLayout(null);
  6.                
  7.                 lblUser.setBounds(5,50,80,25);
  8.                 pane.add(lblUser);
  9.                 txtUser.setBounds(90,50,150,25);
  10.                 pane.add(txtUser);
  11.                 lblUser.setForeground(Color.white);
  12.                
  13.                 lblPass.setBounds(5,85,80,25);
  14.                 pane.add(lblPass);
  15.                 txtPass.setBounds(90,85,150,25);
  16.                 txtPass.setEchoChar('*');
  17.                 pane.add(txtPass);
  18.                 lblPass.setForeground(Color.white);
  19.                
  20.                 lblName1.setBounds(5,120,80,25);
  21.                 pane.add(lblName1);
  22.                 txtName1.setBounds(90,120,150,25);
  23.                 pane.add(txtName1);
  24.                 lblName1.setForeground(Color.white);
  25.                
  26.                
  27.                 lblName2.setBounds(5,155,80,25);
  28.                 pane.add(lblName2);
  29.                 txtName2.setBounds(90,155,150,25);
  30.                 pane.add(txtName2);
  31.                 lblName2.setForeground(Color.white);
  32.                                                                
  33.                 btnNew.setBounds(5,190,75,35);
  34.                 pane.add(btnNew);
  35.                 btnNew.addActionListener(this);
  36.                 pane.setBackground(Color.black);
  37.  
  38.                 setContentPane(pane);
  39.                 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  40.                 pane.setBorder(BorderFactory.createTitledBorder(
  41.            BorderFactory.createEtchedBorder(), "Creating User Account"));
  42.                
  43.                 try{
  44.                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  45.                         cn = DriverManager.getConnection("jdbc:odbc:User");
  46.                 }catch(ClassNotFoundException e)  {
  47.                         System.err.println("Failed to load driver");
  48.                         e.printStackTrace();
  49.                 }
  50.                 catch(SQLException e){
  51.                         System.err.println("Unable to connect");
  52.                         e.printStackTrace();
  53.                 }
  54.         }
7. Create your ActionEvent for clicking the button. This will trigger to add records when clicking our btnNew button.
  1.         public void actionPerformed(ActionEvent e){
  2.                 Object source = e.getSource();
  3.                         if(source == btnNew){          
  4.                                 try{
  5.                                                 String uname=txtUser.getText();
  6.                                                 String pass=txtPass.getText();
  7.                                                 String name1=txtName1.getText();
  8.                                                 String name2=txtName2.getText();
  9.                                         if (!uname.equals("") && !pass.equals("")&& !name1.equals("") && !name2.equals("")) {
  10.                                         st= cn.createStatement();      
  11.                                         ps=cn.prepareStatement("INSERT INTO Login" + " (username,password,name1,name2) " + " VALUES(?,?,?,?)");        
  12.                                         ps.setString(1,txtUser.getText());     
  13.                                         ps.setString(2,txtPass.getText());
  14.                                         ps.setString(3,txtName1.getText());
  15.                                         ps.setString(4,txtName2.getText());
  16.                                         ps.executeUpdate();
  17.                                         JOptionPane.showMessageDialog(null,"New account has been successfully added.","Payroll System: User settings",JOptionPane.INFORMATION_MESSAGE);
  18.                                         txtUser.requestFocus(true);
  19.                                         st.close();
  20.                                         clear();
  21.                                         }
  22.                                         else{
  23.                                                         JOptionPane.showMessageDialog(null,"Please Fill Up The Empty Fields","Warning",JOptionPane.WARNING_MESSAGE);   
  24.                                         }
  25.                                 }catch(SQLException sqlEx){
  26.                                         sqlEx.printStackTrace();
  27.                                         JOptionPane.showMessageDialog(null,"Unable to save!.","Payroll System: User settings",JOptionPane.ERROR_MESSAGE);}
  28.                         }
  29.                                                                
  30.         }
The sql syntax here "ps=cn.prepareStatement("INSERT INTO Login" + " (username,password,name1,name2) " + " VALUES(?,?,?,?)");" inserts the record in the database. 8. Create a method named clear to clear all your textfield.
  1.         public void clear(){   
  2.                 txtUser.setText("");
  3.                 txtPass.setText("");
  4.                 txtName1.setText("");
  5.                 txtName2.setText("");
  6.         }
9. Lastly create your Main. This will create the size and location of your form.
  1. public  static void main(String[]args){
  2.                 UserSettings panel = new UserSettings();
  3.                 panel.setSize(370,350);
  4.                 panel.setVisible(true);
  5.                 panel.setLocation(350,200);
  6.                 panel.setResizable(false);
  7.                 }
Output: output output Here's the full code of this tutorial:
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.sql.*;
  5. import java.lang.*;
  6.  
  7.  public class UserSettings extends JFrame implements ActionListener{
  8.        
  9.         JLabel lblUser = new JLabel("Username ",JLabel.RIGHT); 
  10.         JLabel lblPass = new JLabel("Password ",JLabel.RIGHT);
  11.         JLabel lblName1 = new JLabel("First Name",JLabel.RIGHT);
  12.         JLabel lblName2 = new JLabel("Family Name",JLabel.RIGHT);
  13.        
  14.         JTextField txtUser = new JTextField(20);
  15.         JPasswordField txtPass= new JPasswordField(20);
  16.         JTextField txtName1= new JTextField(20);
  17.         JTextField txtName2= new JTextField(20);
  18.        
  19.         JButton btnNew = new JButton("Add");
  20.  
  21.         Connection cn;
  22.         Statement st;
  23.         PreparedStatement ps;
  24.        
  25.        
  26.                 public void clear(){   
  27.                 txtUser.setText("");
  28.                 txtPass.setText("");
  29.                 txtName1.setText("");
  30.                 txtName2.setText("");
  31.         }
  32.         public UserSettings() {
  33.                 super("User Account Settings");
  34.        
  35.                 JPanel pane = new JPanel();
  36.                 pane.setLayout(null);
  37.                
  38.                 lblUser.setBounds(5,50,80,25);
  39.                 pane.add(lblUser);
  40.                 txtUser.setBounds(90,50,150,25);
  41.                 pane.add(txtUser);
  42.                 lblUser.setForeground(Color.white);
  43.                
  44.                 lblPass.setBounds(5,85,80,25);
  45.                 pane.add(lblPass);
  46.                 txtPass.setBounds(90,85,150,25);
  47.                 txtPass.setEchoChar('*');
  48.                 pane.add(txtPass);
  49.                 lblPass.setForeground(Color.white);
  50.                
  51.                 lblName1.setBounds(5,120,80,25);
  52.                 pane.add(lblName1);
  53.                 txtName1.setBounds(90,120,150,25);
  54.                 pane.add(txtName1);
  55.                 lblName1.setForeground(Color.white);
  56.                
  57.                
  58.                 lblName2.setBounds(5,155,80,25);
  59.                 pane.add(lblName2);
  60.                 txtName2.setBounds(90,155,150,25);
  61.                 pane.add(txtName2);
  62.                 lblName2.setForeground(Color.white);
  63.                                                                
  64.                 btnNew.setBounds(5,190,75,35);
  65.                 pane.add(btnNew);
  66.                 btnNew.addActionListener(this);
  67.                 pane.setBackground(Color.black);
  68.  
  69.                 setContentPane(pane);
  70.                 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  71.                 pane.setBorder(BorderFactory.createTitledBorder(
  72.            BorderFactory.createEtchedBorder(), "Creating User Account"));
  73.                
  74.                 try{
  75.                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  76.                         cn = DriverManager.getConnection("jdbc:odbc:User");
  77.                 }catch(ClassNotFoundException e)  {
  78.                         System.err.println("Failed to load driver");
  79.                         e.printStackTrace();
  80.                 }
  81.                 catch(SQLException e){
  82.                         System.err.println("Unable to connect");
  83.                         e.printStackTrace();
  84.                 }
  85.         }
  86.  
  87.         public void actionPerformed(ActionEvent e){
  88.                 Object source = e.getSource();
  89.                         if(source == btnNew){          
  90.                                 try{
  91.                                                 String uname=txtUser.getText();
  92.                                                 String pass=txtPass.getText();
  93.                                                 String name1=txtName1.getText();
  94.                                                 String name2=txtName2.getText();
  95.                                         if (!uname.equals("") && !pass.equals("")&& !name1.equals("") && !name2.equals("")) {
  96.                                         st= cn.createStatement();      
  97.                                         ps=cn.prepareStatement("INSERT INTO Login" + " (username,password,name1,name2) " + " VALUES(?,?,?,?)");        
  98.                                         ps.setString(1,txtUser.getText());     
  99.                                         ps.setString(2,txtPass.getText());
  100.                                         ps.setString(3,txtName1.getText());
  101.                                         ps.setString(4,txtName2.getText());
  102.                                         ps.executeUpdate();
  103.                                         JOptionPane.showMessageDialog(null,"New account has been successfully added.","Payroll System: User settings",JOptionPane.INFORMATION_MESSAGE);
  104.                                         txtUser.requestFocus(true);
  105.                                         st.close();
  106.                                         clear();
  107.                                         }
  108.                                         else{
  109.                                                         JOptionPane.showMessageDialog(null,"Please Fill Up The Empty Fields","Warning",JOptionPane.WARNING_MESSAGE);   
  110.                                         }
  111.                                 }catch(SQLException sqlEx){
  112.                                         sqlEx.printStackTrace();
  113.                                         JOptionPane.showMessageDialog(null,"Unable to save!.","Payroll System: User settings",JOptionPane.ERROR_MESSAGE);}
  114.                         }
  115.                                                                
  116.         }
  117.  //     public void frameUser(){
  118.         public  static void main(String[]args){
  119.                 UserSettings panel = new UserSettings();
  120.                 panel.setSize(370,350);
  121.                 panel.setVisible(true);
  122.                 panel.setLocation(350,200);
  123.                 panel.setResizable(false);
  124.                 }
  125.  }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment