• We just launched and are currently in beta. Join us as we build and grow the community.

Creating User Account Information in Java - Adding Records to Database

asdfgmhgfdsadfgh

Machine Learning Attacker
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
220
Likes
15
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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.

data1.png

data2.png


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. JTextField

    txtUser =

    new

    JTextField

    (

    20

    )

    ;
  6. JPasswordField

    txtPass=

    new

    JPasswordField

    (

    20

    )

    ;
  7. JTextField

    txtName1=

    new

    JTextField

    (

    20

    )

    ;
  8. JTextField

    txtName2=

    new

    JTextField

    (

    20

    )

    ;

  9. JButton

    btnNew =

    new

    JButton

    (

    "Add"

    )

    ;

  10. Connection

    cn;
  11. Statement

    st;
  12. 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. JPanel

    pane =

    new

    JPanel

    (

    )

    ;
  4. pane.setLayout

    (

    null

    )

    ;

  5. lblUser.setBounds

    (

    5

    ,50

    ,80

    ,25

    )

    ;
  6. pane.add

    (

    lblUser)

    ;
  7. txtUser.setBounds

    (

    90

    ,50

    ,150

    ,25

    )

    ;
  8. pane.add

    (

    txtUser)

    ;
  9. lblUser.setForeground

    (

    Color

    .white

    )

    ;

  10. lblPass.setBounds

    (

    5

    ,85

    ,80

    ,25

    )

    ;
  11. pane.add

    (

    lblPass)

    ;
  12. txtPass.setBounds

    (

    90

    ,85

    ,150

    ,25

    )

    ;
  13. txtPass.setEchoChar

    (

    '*'

    )

    ;
  14. pane.add

    (

    txtPass)

    ;
  15. lblPass.setForeground

    (

    Color

    .white

    )

    ;

  16. lblName1.setBounds

    (

    5

    ,120

    ,80

    ,25

    )

    ;
  17. pane.add

    (

    lblName1)

    ;
  18. txtName1.setBounds

    (

    90

    ,120

    ,150

    ,25

    )

    ;
  19. pane.add

    (

    txtName1)

    ;
  20. lblName1.setForeground

    (

    Color

    .white

    )

    ;


  21. lblName2.setBounds

    (

    5

    ,155

    ,80

    ,25

    )

    ;
  22. pane.add

    (

    lblName2)

    ;
  23. txtName2.setBounds

    (

    90

    ,155

    ,150

    ,25

    )

    ;
  24. pane.add

    (

    txtName2)

    ;
  25. lblName2.setForeground

    (

    Color

    .white

    )

    ;

  26. btnNew.setBounds

    (

    5

    ,190

    ,75

    ,35

    )

    ;
  27. pane.add

    (

    btnNew)

    ;
  28. btnNew.addActionListener

    (

    this

    )

    ;
  29. pane.setBackground

    (

    Color

    .black

    )

    ;

  30. setContentPane(

    pane)

    ;
  31. setDefaultCloseOperation(

    JFrame

    .DISPOSE_ON_CLOSE

    )

    ;
  32. pane.setBorder

    (

    BorderFactory

    .createTitledBorder

    (
  33. BorderFactory

    .createEtchedBorder

    (

    )

    , "Creating User Account"

    )

    )

    ;

  34. try

    {
  35. Class

    .forName

    (

    "sun.jdbc.odbc.JdbcOdbcDriver"

    )

    ;
  36. cn =

    DriverManager

    .getConnection

    (

    "jdbc:odbc:User"

    )

    ;
  37. }

    catch

    (

    ClassNotFoundException

    e)

    {
  38. System

    .err

    .println

    (

    "Failed to load driver"

    )

    ;
  39. e.printStackTrace

    (

    )

    ;
  40. }
  41. catch

    (

    SQLException

    e)

    {
  42. System

    .err

    .println

    (

    "Unable to connect"

    )

    ;
  43. e.printStackTrace

    (

    )

    ;
  44. }
  45. }

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. }

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:
user.png

useroutput.png



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. public

    class

    UserSettings extends

    JFrame

    implements

    ActionListener

    {

  7. JLabel

    lblUser =

    new

    JLabel

    (

    "Username "

    ,JLabel

    .RIGHT

    )

    ;
  8. JLabel

    lblPass =

    new

    JLabel

    (

    "Password "

    ,JLabel

    .RIGHT

    )

    ;
  9. JLabel

    lblName1 =

    new

    JLabel

    (

    "First Name"

    ,JLabel

    .RIGHT

    )

    ;
  10. JLabel

    lblName2 =

    new

    JLabel

    (

    "Family Name"

    ,JLabel

    .RIGHT

    )

    ;

  11. JTextField

    txtUser =

    new

    JTextField

    (

    20

    )

    ;
  12. JPasswordField

    txtPass=

    new

    JPasswordField

    (

    20

    )

    ;
  13. JTextField

    txtName1=

    new

    JTextField

    (

    20

    )

    ;
  14. JTextField

    txtName2=

    new

    JTextField

    (

    20

    )

    ;

  15. JButton

    btnNew =

    new

    JButton

    (

    "Add"

    )

    ;

  16. Connection

    cn;
  17. Statement

    st;
  18. PreparedStatement

    ps;


  19. public

    void

    clear(

    )

    {
  20. txtUser.setText

    (

    ""

    )

    ;
  21. txtPass.setText

    (

    ""

    )

    ;
  22. txtName1.setText

    (

    ""

    )

    ;
  23. txtName2.setText

    (

    ""

    )

    ;
  24. }
  25. public

    UserSettings(

    )

    {
  26. super

    (

    "User Account Settings"

    )

    ;

  27. JPanel

    pane =

    new

    JPanel

    (

    )

    ;
  28. pane.setLayout

    (

    null

    )

    ;

  29. lblUser.setBounds

    (

    5

    ,50

    ,80

    ,25

    )

    ;
  30. pane.add

    (

    lblUser)

    ;
  31. txtUser.setBounds

    (

    90

    ,50

    ,150

    ,25

    )

    ;
  32. pane.add

    (

    txtUser)

    ;
  33. lblUser.setForeground

    (

    Color

    .white

    )

    ;

  34. lblPass.setBounds

    (

    5

    ,85

    ,80

    ,25

    )

    ;
  35. pane.add

    (

    lblPass)

    ;
  36. txtPass.setBounds

    (

    90

    ,85

    ,150

    ,25

    )

    ;
  37. txtPass.setEchoChar

    (

    '*'

    )

    ;
  38. pane.add

    (

    txtPass)

    ;
  39. lblPass.setForeground

    (

    Color

    .white

    )

    ;

  40. lblName1.setBounds

    (

    5

    ,120

    ,80

    ,25

    )

    ;
  41. pane.add

    (

    lblName1)

    ;
  42. txtName1.setBounds

    (

    90

    ,120

    ,150

    ,25

    )

    ;
  43. pane.add

    (

    txtName1)

    ;
  44. lblName1.setForeground

    (

    Color

    .white

    )

    ;


  45. lblName2.setBounds

    (

    5

    ,155

    ,80

    ,25

    )

    ;
  46. pane.add

    (

    lblName2)

    ;
  47. txtName2.setBounds

    (

    90

    ,155

    ,150

    ,25

    )

    ;
  48. pane.add

    (

    txtName2)

    ;
  49. lblName2.setForeground

    (

    Color

    .white

    )

    ;

  50. btnNew.setBounds

    (

    5

    ,190

    ,75

    ,35

    )

    ;
  51. pane.add

    (

    btnNew)

    ;
  52. btnNew.addActionListener

    (

    this

    )

    ;
  53. pane.setBackground

    (

    Color

    .black

    )

    ;

  54. setContentPane(

    pane)

    ;
  55. setDefaultCloseOperation(

    JFrame

    .DISPOSE_ON_CLOSE

    )

    ;
  56. pane.setBorder

    (

    BorderFactory

    .createTitledBorder

    (
  57. BorderFactory

    .createEtchedBorder

    (

    )

    , "Creating User Account"

    )

    )

    ;

  58. try

    {
  59. Class

    .forName

    (

    "sun.jdbc.odbc.JdbcOdbcDriver"

    )

    ;
  60. cn =

    DriverManager

    .getConnection

    (

    "jdbc:odbc:User"

    )

    ;
  61. }

    catch

    (

    ClassNotFoundException

    e)

    {
  62. System

    .err

    .println

    (

    "Failed to load driver"

    )

    ;
  63. e.printStackTrace

    (

    )

    ;
  64. }
  65. catch

    (

    SQLException

    e)

    {
  66. System

    .err

    .println

    (

    "Unable to connect"

    )

    ;
  67. e.printStackTrace

    (

    )

    ;
  68. }
  69. }

  70. public

    void

    actionPerformed(

    ActionEvent

    e)

    {
  71. Object

    source =

    e.getSource

    (

    )

    ;
  72. if

    (

    source ==

    btnNew)

    {
  73. try

    {
  74. String

    uname=

    txtUser.getText

    (

    )

    ;
  75. String

    pass=

    txtPass.getText

    (

    )

    ;
  76. String

    name1=

    txtName1.getText

    (

    )

    ;
  77. String

    name2=

    txtName2.getText

    (

    )

    ;
  78. if

    (

    !

    uname.equals

    (

    ""

    )

    &&

    !

    pass.equals

    (

    ""

    )

    &&

    !

    name1.equals

    (

    ""

    )

    &&

    !

    name2.equals

    (

    ""

    )

    )

    {
  79. st=

    cn.createStatement

    (

    )

    ;
  80. ps=

    cn.prepareStatement

    (

    "INSERT INTO Login"

    +

    " (username,password,name1,name2) "

    +

    " VALUES(?,?,?,?)"

    )

    ;
  81. ps.setString

    (

    1

    ,txtUser.getText

    (

    )

    )

    ;
  82. ps.setString

    (

    2

    ,txtPass.getText

    (

    )

    )

    ;
  83. ps.setString

    (

    3

    ,txtName1.getText

    (

    )

    )

    ;
  84. ps.setString

    (

    4

    ,txtName2.getText

    (

    )

    )

    ;
  85. ps.executeUpdate

    (

    )

    ;
  86. JOptionPane

    .showMessageDialog

    (

    null

    ,"New account has been successfully added."

    ,"Payroll System: User settings"

    ,JOptionPane

    .INFORMATION_MESSAGE

    )

    ;
  87. txtUser.requestFocus

    (

    true

    )

    ;
  88. st.close

    (

    )

    ;
  89. clear(

    )

    ;
  90. }
  91. else

    {
  92. JOptionPane

    .showMessageDialog

    (

    null

    ,"Please Fill Up The Empty Fields"

    ,"Warning"

    ,JOptionPane

    .WARNING_MESSAGE

    )

    ;
  93. }
  94. }

    catch

    (

    SQLException

    sqlEx)

    {
  95. sqlEx.printStackTrace

    (

    )

    ;
  96. JOptionPane

    .showMessageDialog

    (

    null

    ,"Unable to save!."

    ,"Payroll System: User settings"

    ,JOptionPane

    .ERROR_MESSAGE

    )

    ;

    }
  97. }

  98. }
  99. // public void frameUser(){
  100. public

    static

    void

    main(

    String

    [

    ]

    args)

    {
  101. UserSettings panel =

    new

    UserSettings(

    )

    ;
  102. panel.setSize

    (

    370

    ,350

    )

    ;
  103. panel.setVisible

    (

    true

    )

    ;
  104. panel.setLocation

    (

    350

    ,200

    )

    ;
  105. panel.setResizable

    (

    false

    )

    ;
  106. }
  107. }

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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

449,708

322,558

322,567

Top