Creating User Account Information in Java - Adding Records to Database

Submitted by: 
Language: 
Visitors have accessed this post 487 times.

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.

designdesign

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.  

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. System.err.println("Failed to load driver");
  47. e.printStackTrace();
  48. }
  49. catch(SQLException e){
  50. System.err.println("Unable to connect");
  51. e.printStackTrace();
  52. }
  53. }

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:

outputoutput

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.  
  22.  
  23. public void clear(){
  24. txtUser.setText("");
  25. txtPass.setText("");
  26. txtName1.setText("");
  27. txtName2.setText("");
  28. }
  29. public UserSettings() {
  30. super("User Account Settings");
  31.  
  32. JPanel pane = new JPanel();
  33. pane.setLayout(null);
  34.  
  35. lblUser.setBounds(5,50,80,25);
  36. pane.add(lblUser);
  37. txtUser.setBounds(90,50,150,25);
  38. pane.add(txtUser);
  39. lblUser.setForeground(Color.white);
  40.  
  41. lblPass.setBounds(5,85,80,25);
  42. pane.add(lblPass);
  43. txtPass.setBounds(90,85,150,25);
  44. txtPass.setEchoChar('*');
  45. pane.add(txtPass);
  46. lblPass.setForeground(Color.white);
  47.  
  48. lblName1.setBounds(5,120,80,25);
  49. pane.add(lblName1);
  50. txtName1.setBounds(90,120,150,25);
  51. pane.add(txtName1);
  52. lblName1.setForeground(Color.white);
  53.  
  54.  
  55. lblName2.setBounds(5,155,80,25);
  56. pane.add(lblName2);
  57. txtName2.setBounds(90,155,150,25);
  58. pane.add(txtName2);
  59. lblName2.setForeground(Color.white);
  60.  
  61. btnNew.setBounds(5,190,75,35);
  62. pane.add(btnNew);
  63. btnNew.addActionListener(this);
  64. pane.setBackground(Color.black);
  65.  
  66. setContentPane(pane);
  67. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  68. pane.setBorder(BorderFactory.createTitledBorder(
  69. BorderFactory.createEtchedBorder(), "Creating User Account"));
  70.  
  71. try{
  72. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  73. cn = DriverManager.getConnection("jdbc:odbc:User");
  74. System.err.println("Failed to load driver");
  75. e.printStackTrace();
  76. }
  77. catch(SQLException e){
  78. System.err.println("Unable to connect");
  79. e.printStackTrace();
  80. }
  81. }
  82.  
  83. public void actionPerformed(ActionEvent e){
  84. Object source = e.getSource();
  85. if(source == btnNew){
  86. try{
  87. String uname=txtUser.getText();
  88. String pass=txtPass.getText();
  89. String name1=txtName1.getText();
  90. String name2=txtName2.getText();
  91. if (!uname.equals("") && !pass.equals("")&& !name1.equals("") && !name2.equals("")) {
  92. st= cn.createStatement();
  93. ps=cn.prepareStatement("INSERT INTO Login" + " (username,password,name1,name2) " + " VALUES(?,?,?,?)");
  94. ps.setString(1,txtUser.getText());
  95. ps.setString(2,txtPass.getText());
  96. ps.setString(3,txtName1.getText());
  97. ps.setString(4,txtName2.getText());
  98. ps.executeUpdate();
  99. JOptionPane.showMessageDialog(null,"New account has been successfully added.","Payroll System: User settings",JOptionPane.INFORMATION_MESSAGE);
  100. txtUser.requestFocus(true);
  101. st.close();
  102. clear();
  103. }
  104. else{
  105. JOptionPane.showMessageDialog(null,"Please Fill Up The Empty Fields","Warning",JOptionPane.WARNING_MESSAGE);
  106. }
  107. }catch(SQLException sqlEx){
  108. sqlEx.printStackTrace();
  109. JOptionPane.showMessageDialog(null,"Unable to save!.","Payroll System: User settings",JOptionPane.ERROR_MESSAGE);}
  110. }
  111.  
  112. }
  113. // public void frameUser(){
  114. public static void main(String[]args){
  115. UserSettings panel = new UserSettings();
  116. panel.setSize(370,350);
  117. panel.setVisible(true);
  118. panel.setLocation(350,200);
  119. panel.setResizable(false);
  120. }
  121. }

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.